Skip to content

Commit 31b4eca

Browse files
committed
working to handle edge cases where a job can be run twice, unexpectedly
1 parent bf75107 commit 31b4eca

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

job.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -818,15 +818,15 @@ func (d dailyJob) next(lastRun time.Time) time.Time {
818818
}
819819
firstPass = false
820820

821-
startNextDay := time.Date(lastRun.Year(), lastRun.Month(), lastRun.Day()+int(d.interval), 0, 0, 0, lastRun.Nanosecond(), lastRun.Location())
821+
startNextDay := time.Date(lastRun.Year(), lastRun.Month(), lastRun.Day()+int(d.interval), 0, 0, 0, 0, lastRun.Location())
822822
return d.nextDay(startNextDay, firstPass)
823823
}
824824

825825
func (d dailyJob) nextDay(lastRun time.Time, firstPass bool) time.Time {
826826
for _, at := range d.atTimes {
827827
// sub the at time hour/min/sec onto the lastScheduledRun's values
828828
// to use in checks to see if we've got our next run time
829-
atDate := time.Date(lastRun.Year(), lastRun.Month(), lastRun.Day(), at.Hour(), at.Minute(), at.Second(), lastRun.Nanosecond(), lastRun.Location())
829+
atDate := time.Date(lastRun.Year(), lastRun.Month(), lastRun.Day(), at.Hour(), at.Minute(), at.Second(), 0, lastRun.Location())
830830

831831
if firstPass && atDate.After(lastRun) {
832832
// checking to see if it is after i.e. greater than,
@@ -872,7 +872,7 @@ func (w weeklyJob) nextWeekDayAtTime(lastRun time.Time, firstPass bool) time.Tim
872872
for _, at := range w.atTimes {
873873
// sub the at time hour/min/sec onto the lastScheduledRun's values
874874
// to use in checks to see if we've got our next run time
875-
atDate := time.Date(lastRun.Year(), lastRun.Month(), lastRun.Day()+int(weekDayDiff), at.Hour(), at.Minute(), at.Second(), lastRun.Nanosecond(), lastRun.Location())
875+
atDate := time.Date(lastRun.Year(), lastRun.Month(), lastRun.Day()+int(weekDayDiff), at.Hour(), at.Minute(), at.Second(), 0, lastRun.Location())
876876

877877
if firstPass && atDate.After(lastRun) {
878878
// checking to see if it is after i.e. greater than,
@@ -940,7 +940,7 @@ func (m monthlyJob) nextMonthDayAtTime(lastRun time.Time, days []int, firstPass
940940
for _, at := range m.atTimes {
941941
// sub the day, and the at time hour/min/sec onto the lastScheduledRun's values
942942
// to use in checks to see if we've got our next run time
943-
atDate := time.Date(lastRun.Year(), lastRun.Month(), day, at.Hour(), at.Minute(), at.Second(), lastRun.Nanosecond(), lastRun.Location())
943+
atDate := time.Date(lastRun.Year(), lastRun.Month(), day, at.Hour(), at.Minute(), at.Second(), 0, lastRun.Location())
944944

945945
if atDate.Month() != lastRun.Month() {
946946
// this check handles if we're setting a day not in the current month

scheduler.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ func (s *scheduler) selectExecJobsOutForRescheduling(id uuid.UUID) {
331331
return
332332
}
333333

334-
scheduleFrom := j.lastRun
334+
var scheduleFrom time.Time
335335
if len(j.nextScheduled) > 0 {
336336
// always grab the last element in the slice as that is the furthest
337337
// out in the future and the time from which we want to calculate
@@ -362,6 +362,15 @@ func (s *scheduler) selectExecJobsOutForRescheduling(id uuid.UUID) {
362362
}
363363
}
364364

365+
if slices.Contains(j.nextScheduled, next) {
366+
// if the next value is a duplicate of what's already in the nextScheduled slice, for example:
367+
// - the job is being rescheduled off the same next run value as before
368+
// increment to the next, next value
369+
for slices.Contains(j.nextScheduled, next) {
370+
next = j.next(next)
371+
}
372+
}
373+
365374
// Clean up any existing timer to prevent leaks
366375
if j.timer != nil {
367376
j.timer.Stop()

0 commit comments

Comments
 (0)