Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes 38 #39

Merged
merged 4 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion job.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Job struct {

// NewJob creates a new Job with the provided interval
func NewJob(interval uint64) *Job {
th := newTimeHelper()
th := newTimeWrapper()
return &Job{
interval: interval,
lastRun: th.Unix(0, 0),
Expand Down
42 changes: 29 additions & 13 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ import (
type Scheduler struct {
jobs []*Job
loc *time.Location
time timeHelper // an instance of timeHelper to interact with the time package

running bool
stopChan chan struct{} // signal to stop scheduling

time timeWrapper // wrapper around time.Time
}

// NewScheduler creates a new Scheduler
func NewScheduler(loc *time.Location) *Scheduler {
return &Scheduler{
jobs: newEmptyJobSlice(),
loc: loc,
time: newTimeHelper(),
jobs: make([]*Job, 0),
loc: loc,
running: false,
stopChan: make(chan struct{}),
time: newTimeWrapper(),
}
}

Expand All @@ -32,22 +38,26 @@ func (s *Scheduler) StartBlocking() {

// 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)
if s.running {
return s.stopChan
}
s.running = true

ticker := s.time.NewTicker(1 * time.Second)
go func() {
for {
select {
case <-ticker.C:
s.RunPending()
case <-stopped:
case <-s.stopChan:
ticker.Stop()
s.running = false
return
}
}
}()

return stopped
return s.stopChan
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Unrated to this PR) Do we really need to expose this chan to the user? We already have a s.Stop() that takes care of stopping the scheduler.

Copy link
Member Author

@Streppel Streppel May 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right, but as this would be a breaking change I decided to leave it for another PR

}

// Jobs returns the list of Jobs from the Scheduler
Expand Down Expand Up @@ -249,14 +259,20 @@ func (s *Scheduler) Scheduled(j interface{}) bool {
return false
}

// Clear delete all scheduled Jobs
// Clear clear all Jobs from this scheduler
func (s *Scheduler) Clear() {
s.jobs = newEmptyJobSlice()
s.jobs = make([]*Job, 0)
}

// Stop stops the scheduler. This is a no-op if the scheduler is already stopped .
func (s *Scheduler) Stop() {
if s.running {
s.stopScheduler()
}
}

func newEmptyJobSlice() []*Job {
const initialCapacity = 256
return make([]*Job, 0, initialCapacity)
func (s *Scheduler) stopScheduler() {
s.stopChan <- struct{}{}
}

// Do specifies the jobFunc that should be called every time the Job runs
Expand Down
13 changes: 13 additions & 0 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,5 +518,18 @@ func TestSetUnit(t *testing.T) {
assert.Equal(t, tc.timeUnit, j.unit)
})
}
}

func TestScheduler_Stop(t *testing.T) {
t.Run("stops a running scheduler", func(t *testing.T) {
sched := NewScheduler(time.UTC)
sched.StartAsync()
sched.Stop()
assert.False(t, sched.running)
})
t.Run("noop on stopped scheduler", func(t *testing.T) {
sched := NewScheduler(time.UTC)
sched.Stop()
assert.False(t, sched.running)
})
}
4 changes: 2 additions & 2 deletions timeHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package gocron

import "time"

type timeHelper interface {
type timeWrapper interface {
Now(*time.Location) time.Time
Unix(int64, int64) time.Time
Sleep(time.Duration)
Date(int, time.Month, int, int, int, int, int, *time.Location) time.Time
NewTicker(time.Duration) *time.Ticker
}

func newTimeHelper() timeHelper {
func newTimeWrapper() timeWrapper {
return &trueTime{}
}

Expand Down