Skip to content

Commit 5b2fe32

Browse files
authored
fix: WithStartDateTimePast now correctly calculates from past time (#897)
1 parent 9b37b3f commit 5b2fe32

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

job.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,16 +1022,14 @@ type weeklyJob struct {
10221022
}
10231023

10241024
func (w weeklyJob) next(lastRun time.Time) time.Time {
1025-
firstPass := true
1026-
next := w.nextWeekDayAtTime(lastRun, firstPass)
1025+
next := w.nextWeekDayAtTime(lastRun, true)
10271026
if !next.IsZero() {
10281027
return next
10291028
}
1030-
firstPass = false
10311029

10321030
startOfTheNextIntervalWeek := (lastRun.Day() - int(lastRun.Weekday())) + int(w.interval*7)
10331031
from := time.Date(lastRun.Year(), lastRun.Month(), startOfTheNextIntervalWeek, 0, 0, 0, 0, lastRun.Location())
1034-
return w.nextWeekDayAtTime(from, firstPass)
1032+
return w.nextWeekDayAtTime(from, false)
10351033
}
10361034

10371035
func (w weeklyJob) nextWeekDayAtTime(lastRun time.Time, firstPass bool) time.Time {

scheduler.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,12 @@ func (s *scheduler) selectNewJob(in newJobIn) {
508508
next = j.next(s.now())
509509
}
510510

511+
if next.Before(s.now()) {
512+
for next.Before(s.now()) {
513+
next = j.next(next)
514+
}
515+
}
516+
511517
id := j.id
512518
j.timer = s.exec.clock.AfterFunc(next.Sub(s.now()), func() {
513519
select {
@@ -559,6 +565,11 @@ func (s *scheduler) selectStart() {
559565
if next.IsZero() {
560566
next = j.next(s.now())
561567
}
568+
if next.Before(s.now()) {
569+
for next.Before(s.now()) {
570+
next = j.next(next)
571+
}
572+
}
562573

563574
jobID := id
564575
j.timer = s.exec.clock.AfterFunc(next.Sub(s.now()), func() {

scheduler_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2876,3 +2876,34 @@ func TestScheduler_WithMonitor(t *testing.T) {
28762876
})
28772877
}
28782878
}
2879+
2880+
func TestScheduler_WithStartAtDateTimePast(t *testing.T) {
2881+
defer verifyNoGoroutineLeaks(t)
2882+
2883+
// Monday
2884+
testTime := time.Date(2024, time.January, 1, 9, 0, 0, 0, time.UTC)
2885+
2886+
fakeClock := clockwork.NewFakeClockAt(testTime)
2887+
2888+
s := newTestScheduler(t, WithClock(fakeClock))
2889+
j, err := s.NewJob(
2890+
WeeklyJob(2, NewWeekdays(time.Sunday), NewAtTimes(NewAtTime(10, 0, 0))),
2891+
NewTask(func() {}),
2892+
WithStartAt(
2893+
// The start time is in the past (Dec 30, 2023 9am) which is a Saturday
2894+
WithStartDateTimePast(testTime.Add(-time.Hour*24*2)),
2895+
),
2896+
)
2897+
require.NoError(t, err)
2898+
2899+
s.Start()
2900+
2901+
nextRun, err := j.NextRun()
2902+
require.NoError(t, err)
2903+
2904+
require.NoError(t, s.Shutdown())
2905+
2906+
// Because the start time was in the past - we expect it to schedule 2 intervals ahead, pasing the first available Sunday
2907+
// which was in the past Dec 31, 2023, so the next is Jan 7, 2024
2908+
assert.Equal(t, time.Date(2024, time.January, 7, 10, 0, 0, 0, time.UTC), nextRun)
2909+
}

0 commit comments

Comments
 (0)