mirror of https://github.com/go-co-op/gocron.git
fix: multiple calls to shutdown should be no-op
This commit is contained in:
parent
7acb981965
commit
b04e3b9a09
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue