fix: multiple calls to shutdown should be no-op

This commit is contained in:
John Roesler 2025-10-08 16:10:01 -05:00
parent 7acb981965
commit b04e3b9a09
No known key found for this signature in database
GPG Key ID: 3AA260B9FCA0A6E1
2 changed files with 19 additions and 0 deletions

View File

@ -233,6 +233,12 @@ func NewScheduler(options ...SchedulerOption) (Scheduler, error) {
func (s *scheduler) stopScheduler() {
s.logger.Debug("gocron: stopping scheduler")
if !s.started {
s.logger.Debug("gocron: scheduler already stopped")
s.stopErrCh <- nil
return
}
if s.started {
s.exec.stopCh <- struct{}{}
}
@ -820,6 +826,9 @@ func (s *scheduler) StopJobs() error {
}
func (s *scheduler) Shutdown() error {
if !s.started {
return nil
}
s.shutdownCancel()
t := time.NewTimer(s.exec.stopTimeout + 2*time.Second)

View File

@ -567,6 +567,16 @@ func TestScheduler_Shutdown(t *testing.T) {
_, err = j.NextRun()
assert.ErrorIs(t, err, ErrJobNotFound)
})
t.Run("calling shutdown multiple times including before start is a no-op", func(t *testing.T) {
s := newTestScheduler(t)
assert.NoError(t, s.Shutdown())
s.Start()
assert.NoError(t, s.Shutdown())
assert.NoError(t, s.Shutdown())
})
}
func TestScheduler_NewJob(t *testing.T) {