Skip to content

Commit

Permalink
fix: scheduler schedules jobs registration when already running (#61)
Browse files Browse the repository at this point in the history
* fix: scheduler schedules jobs registration when already running
  • Loading branch information
Streppel authored Sep 22, 2020
1 parent 9f9dc08 commit 31e21f6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,11 @@ func (s *Scheduler) Do(jobFun interface{}, params ...interface{}) (*Job, error)
j.fparams[fname] = params
j.jobFunc = fname

// we should not schedule if not running since we cant foresee how long it will take for the scheduler to start
if s.running {
s.scheduleNextRun(j)
}

return j, nil
}

Expand Down
18 changes: 18 additions & 0 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,21 @@ func (f fakeTime) Sleep(duration time.Duration) {
func (f fakeTime) NewTicker(duration time.Duration) *time.Ticker {
panic("implement me")
}

func TestScheduler_Do(t *testing.T) {
t.Run("adding a new job before scheduler starts does not schedule job", func(t *testing.T) {
s := NewScheduler(time.UTC)
s.running = false
job, err := s.Every(1).Second().Do(func() {})
assert.Equal(t, nil, err)
assert.True(t, job.nextRun.IsZero())
})

t.Run("adding a new job when scheduler is running schedules job", func(t *testing.T) {
s := NewScheduler(time.UTC)
s.running = true
job, err := s.Every(1).Second().Do(func() {})
assert.Equal(t, nil, err)
assert.False(t, job.nextRun.IsZero())
})
}

0 comments on commit 31e21f6

Please sign in to comment.