From 560a9dc9e43d79f93e5ac97c872be3eb34935b84 Mon Sep 17 00:00:00 2001 From: John Roesler Date: Wed, 11 Dec 2024 10:43:27 -0600 Subject: [PATCH] stop timeout timers when no longer needed (#803) --- job.go | 4 +++- scheduler.go | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/job.go b/job.go index 890889ed..2e5058ae 100644 --- a/job.go +++ b/job.go @@ -1099,12 +1099,14 @@ func (j job) RunNow() error { defer cancel() resp := make(chan error, 1) + t := time.NewTimer(100 * time.Millisecond) select { case j.runJobRequest <- runJobRequest{ id: j.id, outChan: resp, }: - case <-time.After(100 * time.Millisecond): + t.Stop() + case <-t.C: return ErrJobRunNowFailed } var err error diff --git a/scheduler.go b/scheduler.go index 90ff5212..81940f80 100644 --- a/scheduler.go +++ b/scheduler.go @@ -241,9 +241,11 @@ func (s *scheduler) stopScheduler() { } var err error if s.started { + t := time.NewTimer(s.exec.stopTimeout + 1*time.Second) select { case err = <-s.exec.done: - case <-time.After(s.exec.stopTimeout + 1*time.Second): + t.Stop() + case <-t.C: err = ErrStopExecutorTimedOut } } @@ -741,20 +743,27 @@ func (s *scheduler) StopJobs() error { return nil case s.stopCh <- struct{}{}: } + + t := time.NewTimer(s.exec.stopTimeout + 2*time.Second) select { case err := <-s.stopErrCh: + t.Stop() return err - case <-time.After(s.exec.stopTimeout + 2*time.Second): + case <-t.C: return ErrStopSchedulerTimedOut } } func (s *scheduler) Shutdown() error { s.shutdownCancel() + + t := time.NewTimer(s.exec.stopTimeout + 2*time.Second) select { case err := <-s.stopErrCh: + + t.Stop() return err - case <-time.After(s.exec.stopTimeout + 2*time.Second): + case <-t.C: return ErrStopSchedulerTimedOut } }