Skip to content

Commit

Permalink
Stops all jobs when stopping scheduler (fix #367) (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
pma9 committed Aug 4, 2022
1 parent 22fc237 commit 08a53ef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions scheduler.go
Expand Up @@ -813,10 +813,17 @@ func (s *Scheduler) Stop() {

func (s *Scheduler) stop() {
s.setRunning(false)
s.stopJobs(s.jobs)
s.executor.stop()
s.stopChan <- struct{}{}
}

func (s *Scheduler) stopJobs(jobs []*Job) {
for _, job := range jobs {
job.stop()
}
}

func (s *Scheduler) doCommon(jobFun interface{}, params ...interface{}) (*Job, error) {
job := s.getCurrentJob()

Expand Down
15 changes: 15 additions & 0 deletions scheduler_test.go
Expand Up @@ -834,6 +834,21 @@ func TestScheduler_Stop(t *testing.T) {
s.Stop()
assert.False(t, s.IsRunning())
})
t.Run("stops all jobs", func(t *testing.T) {
t.Parallel()
s := NewScheduler(time.UTC)
job, _ := s.Every(3).Second().Do(func() {
//noop
})
s.StartAsync()
time.Sleep(1 * time.Second) // enough time for job to run
preStopJobTimer := job.timer
s.Stop()
time.Sleep(3 * time.Second) // enough time for job timer to reset
afterStopJobTimer := job.timer

assert.Same(t, preStopJobTimer, afterStopJobTimer)
})
t.Run("waits for jobs to finish processing before returning .Stop()", func(t *testing.T) {
t.Parallel()
i := int32(0)
Expand Down

0 comments on commit 08a53ef

Please sign in to comment.