-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4dfda47
commit 88f4652
Showing
4 changed files
with
126 additions
and
4 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package cron | ||
|
||
import "time" | ||
|
||
type ICronJob struct { | ||
duration time.Duration | ||
stopAfter time.Duration | ||
callback func() | ||
stopChan chan bool | ||
} | ||
|
||
func NewCronJob(duration, stopAfter time.Duration, callback func()) *ICronJob { | ||
return &ICronJob{ | ||
duration: duration, | ||
stopAfter: stopAfter, | ||
callback: callback, | ||
stopChan: make(chan bool), | ||
} | ||
} | ||
|
||
func (cj *ICronJob) Start() { | ||
go func() { | ||
ticker := time.NewTicker(cj.duration) | ||
stopTimer := time.NewTimer(cj.stopAfter) | ||
|
||
defer ticker.Stop() | ||
defer stopTimer.Stop() | ||
|
||
for { | ||
select { | ||
case <-ticker.C: | ||
cj.callback() | ||
case <-stopTimer.C: | ||
cj.stopChan <- true | ||
return | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (cj *ICronJob) Wait() { | ||
<-cj.stopChan | ||
} | ||
|
||
func Run(duration, stopAfter uint64, callback func()) { | ||
cronJob := NewCronJob(time.Duration(duration)*time.Second, time.Duration(stopAfter)*time.Second, callback) | ||
cronJob.Start() | ||
cronJob.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cron | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestNewCronJob(t *testing.T) { | ||
duration := time.Second * 1 | ||
stopAfter := time.Second * 5 | ||
callback := func() {} | ||
|
||
cronJob := NewCronJob(duration, stopAfter, callback) | ||
|
||
if cronJob.duration != duration { | ||
t.Errorf("Expected duration %v, but got %v", duration, cronJob.duration) | ||
} | ||
if cronJob.stopAfter != stopAfter { | ||
t.Errorf("Expected stopAfter %v, but got %v", stopAfter, cronJob.stopAfter) | ||
} | ||
if cronJob.callback == nil { | ||
t.Error("Expected callback to be set, but it was nil") | ||
} | ||
if cronJob.stopChan == nil { | ||
t.Error("Expected stopChan to be set, but it was nil") | ||
} | ||
} | ||
|
||
func TestCronJobStartAndWait(t *testing.T) { | ||
duration := time.Millisecond * 10 | ||
stopAfter := time.Millisecond * 50 | ||
callbackCalled := false | ||
|
||
callback := func() { | ||
callbackCalled = true | ||
} | ||
|
||
cronJob := NewCronJob(duration, stopAfter, callback) | ||
cronJob.Start() | ||
cronJob.Wait() | ||
|
||
if !callbackCalled { | ||
t.Error("Expected callback to be called, but it wasn't") | ||
} | ||
} | ||
|
||
func TestRun(t *testing.T) { | ||
duration := uint64(1) | ||
stopAfter := uint64(2) | ||
callbackCalled := false | ||
|
||
callback := func() { | ||
callbackCalled = true | ||
} | ||
|
||
Run(duration, stopAfter, callback) | ||
|
||
if !callbackCalled { | ||
t.Error("Expected callback to be called, but it wasn't") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cron | ||
|
||
import ( | ||
"github.com/thuongtruong109/gouse/cron" | ||
"fmt" | ||
) | ||
|
||
func SampleCronRun() { | ||
callback := func() { | ||
fmt.Println("Cron job executed") | ||
} | ||
|
||
cron.Run(1, 5, callback) | ||
|
||
fmt.Println("Cron job stopped.") | ||
} |