Skip to content

Commit

Permalink
daily job next logic failed to consider 1 midnight attime (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRoesler authored Dec 13, 2023
1 parent 1177ef0 commit b0fdf55
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
19 changes: 13 additions & 6 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,12 +622,7 @@ type dailyJob struct {
}

func (d dailyJob) next(lastRun time.Time) time.Time {
next := d.nextDay(lastRun)
if !next.IsZero() {
return next
}
startNextDay := time.Date(lastRun.Year(), lastRun.Month(), lastRun.Day()+int(d.interval), 0, 0, 0, lastRun.Nanosecond(), lastRun.Location())
return d.nextDay(startNextDay)
return d.nextDay(lastRun)
}

func (d dailyJob) nextDay(lastRun time.Time) time.Time {
Expand All @@ -643,6 +638,18 @@ func (d dailyJob) nextDay(lastRun time.Time) time.Time {
return atDate
}
}
startNextDay := time.Date(lastRun.Year(), lastRun.Month(), lastRun.Day()+int(d.interval), 0, 0, 0, lastRun.Nanosecond(), lastRun.Location())
for _, at := range d.atTimes {
// sub the at time hour/min/sec onto the lastRun's values
// to use in checks to see if we've got our next run time
atDate := time.Date(startNextDay.Year(), startNextDay.Month(), startNextDay.Day(), at.Hour(), at.Minute(), at.Second(), startNextDay.Nanosecond(), startNextDay.Location())

if !atDate.Before(startNextDay) {
// now that we're looking at the next day, it's ok to consider
// the same at time that was last run
return atDate
}
}
return time.Time{}
}

Expand Down
10 changes: 10 additions & 0 deletions job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ func TestDailyJob_next(t *testing.T) {
expectedNextRun time.Time
expectedDurationToNextRun time.Duration
}{
{
"daily at midnight",
1,
[]time.Time{
time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC),
},
time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC),
24 * time.Hour,
},
{
"daily multiple at times",
1,
Expand Down

0 comments on commit b0fdf55

Please sign in to comment.