From 73391ef88b7630854f9fa992617153f6a8f2bbfc Mon Sep 17 00:00:00 2001 From: leedrum Date: Fri, 29 Dec 2023 03:23:53 +0700 Subject: [PATCH 1/3] refactor: fix indent (#649) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 08e58e0..5c3a301 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ func main() { gocron.NewTask( func(a string, b int) { // do things - }, + }, "hello", 1, ), From 29a2f29e3c08820d83c5756b0d96baa1d99d684d Mon Sep 17 00:00:00 2001 From: John Roesler Date: Tue, 2 Jan 2024 10:32:14 -0600 Subject: [PATCH 2/3] fix to handle when next ends up in the past (#650) * fix to handle when next ends up in the past * add code comments --- scheduler.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/scheduler.go b/scheduler.go index f5c7d26..4477a9b 100644 --- a/scheduler.go +++ b/scheduler.go @@ -268,10 +268,15 @@ func (s *scheduler) selectRemoveJob(id uuid.UUID) { delete(s.jobs, id) } +// Jobs coming back from the executor to the scheduler that +// need to evaluated for rescheduling. func (s *scheduler) selectExecJobIDsOut(id uuid.UUID) { j := s.jobs[id] j.lastRun = j.nextRun + // if the job has a limited number of runs set, we need to + // check how many runs have occurred and stop running this + // job if it has reached the limit. if j.limitRunsTo != nil { j.limitRunsTo.runCount = j.limitRunsTo.runCount + 1 if j.limitRunsTo.runCount == j.limitRunsTo.limit { @@ -288,10 +293,25 @@ func (s *scheduler) selectExecJobIDsOut(id uuid.UUID) { next := j.next(j.lastRun) if next.IsZero() { + // the job's next function will return zero for OneTime jobs. + // since they are one time only, they do not need rescheduling. return } + if next.Before(s.now()) { + // in some cases the next run time can be in the past, for example: + // - the time on the machine was incorrect and has been synced with ntp + // - the machine went to sleep, and woke up some time later + // in those cases, we want to increment to the next run in the future + // and schedule the job for that time. + for next.Before(s.now()) { + next = j.next(next) + } + } j.nextRun = next j.timer = s.clock.AfterFunc(next.Sub(s.now()), func() { + // set the actual timer on the job here and listen for + // shut down events so that the job doesn't attempt to + // run if the scheduler has been shutdown. select { case <-s.shutdownCtx.Done(): return @@ -301,6 +321,7 @@ func (s *scheduler) selectExecJobIDsOut(id uuid.UUID) { }: } }) + // update the job with its new next and last run times and timer. s.jobs[id] = j } From ae366d91ea618d1c19433bc2cd757f250c99a983 Mon Sep 17 00:00:00 2001 From: John Roesler Date: Tue, 2 Jan 2024 10:47:01 -0600 Subject: [PATCH 3/3] make the order of the returned jobs slice deterministic (#652) --- scheduler.go | 11 +++++++++++ scheduler_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/scheduler.go b/scheduler.go index 4477a9b..eebb221 100644 --- a/scheduler.go +++ b/scheduler.go @@ -228,6 +228,17 @@ func (s *scheduler) selectAllJobsOutRequest(out allJobsOutRequest) { outJobs[counter] = s.jobFromInternalJob(j) counter++ } + slices.SortFunc(outJobs, func(a, b Job) int { + aID, bID := a.ID().String(), b.ID().String() + switch { + case aID < bID: + return -1 + case aID > bID: + return 1 + default: + return 0 + } + }) select { case <-s.shutdownCtx.Done(): case out.outChan <- outJobs: diff --git a/scheduler_test.go b/scheduler_test.go index a2a0fb1..3df33bb 100644 --- a/scheduler_test.go +++ b/scheduler_test.go @@ -1635,3 +1635,32 @@ func TestScheduler_OneTimeJob(t *testing.T) { }) } } + +func TestScheduler_Jobs(t *testing.T) { + tests := []struct { + name string + }{ + { + "order is equal", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := newTestScheduler(t) + + for i := 0; i <= 20; i++ { + _, err := s.NewJob( + DurationJob(time.Second), + NewTask(func() {}), + ) + require.NoError(t, err) + } + + jobsFirst := s.Jobs() + jobsSecond := s.Jobs() + + assert.Equal(t, jobsFirst, jobsSecond) + }) + } +}