Skip to content

Commit

Permalink
creates StartAsync and StartBlocking methods (#25)
Browse files Browse the repository at this point in the history
* creates StartAsync and StartBlocking methods

* mvoes task to it's own function

* renames ExampleScheduler_Start function

* starBlocking on test example

* Update scheduler.go

* Apply suggestions from code review

Co-authored-by: John Roesler <johnrroesler@gmail.com>
  • Loading branch information
Streppel and JohnRoesler committed Apr 11, 2020
1 parent 602934f commit 6866e50
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 20 deletions.
14 changes: 10 additions & 4 deletions README.md
Expand Up @@ -6,7 +6,7 @@ goCron is a Golang job scheduling package which lets you run Go functions period

goCron is a Golang implementation of Ruby module [clockwork](https://github.com/tomykaira/clockwork) and Python job scheduling package [schedule](https://github.com/dbader/schedule).

See also this two great articles:
See also these two great articles:

- [Rethinking Cron](http://adam.herokuapp.com/past/2010/4/13/rethinking_cron/)
- [Replace Cron with Clockwork](http://adam.herokuapp.com/past/2010/6/30/replace_cron_with_clockwork/)
Expand Down Expand Up @@ -34,9 +34,13 @@ func taskWithParams(a int, b string) {
}

func main() {
// defines a new scheduler that schedules and runs jobs
s1 := gocron.NewScheduler(time.UTC)

s1.Every(3).Seconds().Do(task)
<- s1.Start() // starts running (blocks current thread)

// scheduler starts running jobs and current thread continues to execute
s1.StartAsync()

// Do jobs without params
s2 := gocron.NewScheduler(time.UTC)
Expand Down Expand Up @@ -82,8 +86,10 @@ func main() {
// Clear all scheduled jobs
s2.Clear()

// Start all the pending jobs
<- s2.Start()
// executes the scheduler and blocks current thread
s2.StartBlocking()

// this line is never reached
}
```

Expand Down
2 changes: 1 addition & 1 deletion example/lock.go
Expand Up @@ -70,5 +70,5 @@ func main() {

s := gocron.NewScheduler(time.UTC)
s.Every(1).Second().Lock().Do(lockedTask, arg)
<-s.Start()
s.StartBlocking()
}
16 changes: 10 additions & 6 deletions example_test.go
Expand Up @@ -7,21 +7,25 @@ import (
"github.com/go-co-op/gocron"
)

func ExampleScheduler_Start() {
var task = func() {
fmt.Println("I am a task")
}

func ExampleScheduler_StartBlocking() {
s := gocron.NewScheduler(time.UTC)
s.Every(3).Seconds().Do(func() { fmt.Println("I am a task") })
<-s.Start()
s.Every(3).Seconds().Do(task)
s.StartBlocking()
}

func ExampleScheduler_At() {
s := gocron.NewScheduler(time.UTC)
s.Every(1).Day().At("10:30").Do(func() { fmt.Println("I am a task") })
s.Every(1).Monday().At("10:30:01").Do(func() { fmt.Println("I am a task") })
s.Every(1).Day().At("10:30").Do(task)
s.Every(1).Monday().At("10:30:01").Do(task)
}

func ExampleJob_GetScheduledTime() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Day().At("10:30").Do(func() { fmt.Println("I am a task") })
job, _ := s.Every(1).Day().At("10:30").Do(task)
fmt.Println(job.GetScheduledTime())
// Output: 10:30
}
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -9,4 +9,4 @@ require (
github.com/stretchr/testify v1.4.0
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
)
)
6 changes: 3 additions & 3 deletions gocron_test.go
Expand Up @@ -168,9 +168,9 @@ func TestLocker(t *testing.T) {
s3 := NewScheduler(time.UTC)
s3.Every(1).Seconds().Lock().Do(task, "C", i)

stop1 := s1.Start()
stop2 := s2.Start()
stop3 := s3.Start()
stop1 := s1.StartAsync()
stop2 := s2.StartAsync()
stop3 := s3.StartAsync()

time.Sleep(time.Millisecond * 100)

Expand Down
9 changes: 7 additions & 2 deletions scheduler.go
Expand Up @@ -24,8 +24,13 @@ func NewScheduler(loc *time.Location) *Scheduler {
}
}

// Start all the pending Jobs using a per second ticker
func (s *Scheduler) Start() chan struct{} {
// StartBlocking starts all the pending jobs using a second-long ticker and blocks the current thread
func (s *Scheduler) StartBlocking() {
<-s.StartAsync()
}

// StartAsync starts a goroutine that runs all the pending using a second-long ticker
func (s *Scheduler) StartAsync() chan struct{} {
stopped := make(chan struct{})
ticker := s.time.NewTicker(1 * time.Second)

Expand Down
6 changes: 3 additions & 3 deletions scheduler_test.go
Expand Up @@ -39,7 +39,7 @@ func TestExecutionSeconds(t *testing.T) {
}
})

stop := sched.Start()
stop := sched.StartAsync()
<-jobDone // Wait job done
close(stop)

Expand Down Expand Up @@ -90,9 +90,9 @@ func TestAt(t *testing.T) {
nextRun := dayJob.NextScheduledTime()
assert.Equal(t, expectedStartTime, nextRun)

sStop := s.Start()
stop := s.StartAsync()
<-dayJobDone // Wait job done
close(sStop)
close(stop)
time.Sleep(time.Second) // wait for scheduler to reschedule job

// Expected next start time 1 day after
Expand Down

0 comments on commit 6866e50

Please sign in to comment.