@@ -256,6 +256,7 @@ func (w weeklyJobDefinition) setup(j *internalJob, location *time.Location) erro
256
256
daysOfTheWeek := w .daysOfTheWeek ()
257
257
258
258
slices .Sort (daysOfTheWeek )
259
+ ws .daysOfWeek = daysOfTheWeek
259
260
260
261
atTimesDate , err := convertAtTimesToDateTime (w .atTimes , location )
261
262
switch {
@@ -622,31 +623,31 @@ type dailyJob struct {
622
623
}
623
624
624
625
func (d dailyJob ) next (lastRun time.Time ) time.Time {
625
- return d .nextDay (lastRun )
626
+ firstPass := true
627
+ next := d .nextDay (lastRun , firstPass )
628
+ if ! next .IsZero () {
629
+ return next
630
+ }
631
+ firstPass = false
632
+
633
+ startNextDay := time .Date (lastRun .Year (), lastRun .Month (), lastRun .Day ()+ int (d .interval ), 0 , 0 , 0 , lastRun .Nanosecond (), lastRun .Location ())
634
+ return d .nextDay (startNextDay , firstPass )
626
635
}
627
636
628
- func (d dailyJob ) nextDay (lastRun time.Time ) time.Time {
637
+ func (d dailyJob ) nextDay (lastRun time.Time , firstPass bool ) time.Time {
629
638
for _ , at := range d .atTimes {
630
639
// sub the at time hour/min/sec onto the lastRun's values
631
640
// to use in checks to see if we've got our next run time
632
641
atDate := time .Date (lastRun .Year (), lastRun .Month (), lastRun .Day (), at .Hour (), at .Minute (), at .Second (), lastRun .Nanosecond (), lastRun .Location ())
633
642
634
- if atDate .After (lastRun ) {
643
+ if firstPass && atDate .After (lastRun ) {
635
644
// checking to see if it is after i.e. greater than,
636
645
// and not greater or equal as our lastRun day/time
637
646
// will be in the loop, and we don't want to select it again
638
647
return atDate
639
- }
640
- }
641
- startNextDay := time .Date (lastRun .Year (), lastRun .Month (), lastRun .Day ()+ int (d .interval ), 0 , 0 , 0 , lastRun .Nanosecond (), lastRun .Location ())
642
- for _ , at := range d .atTimes {
643
- // sub the at time hour/min/sec onto the lastRun's values
644
- // to use in checks to see if we've got our next run time
645
- atDate := time .Date (startNextDay .Year (), startNextDay .Month (), startNextDay .Day (), at .Hour (), at .Minute (), at .Second (), startNextDay .Nanosecond (), startNextDay .Location ())
646
-
647
- if ! atDate .Before (startNextDay ) {
648
+ } else if ! firstPass && ! atDate .Before (lastRun ) {
648
649
// now that we're looking at the next day, it's ok to consider
649
- // the same at time that was last run
650
+ // the same at time that was last run (as lastRun has been incremented)
650
651
return atDate
651
652
}
652
653
}
@@ -662,17 +663,19 @@ type weeklyJob struct {
662
663
}
663
664
664
665
func (w weeklyJob ) next (lastRun time.Time ) time.Time {
665
- next := w .nextWeekDayAtTime (lastRun )
666
+ firstPass := true
667
+ next := w .nextWeekDayAtTime (lastRun , firstPass )
666
668
if ! next .IsZero () {
667
669
return next
668
670
}
671
+ firstPass = false
669
672
670
673
startOfTheNextIntervalWeek := (lastRun .Day () - int (lastRun .Weekday ())) + int (w .interval * 7 )
671
674
from := time .Date (lastRun .Year (), lastRun .Month (), startOfTheNextIntervalWeek , 0 , 0 , 0 , 0 , lastRun .Location ())
672
- return w .nextWeekDayAtTime (from )
675
+ return w .nextWeekDayAtTime (from , firstPass )
673
676
}
674
677
675
- func (w weeklyJob ) nextWeekDayAtTime (lastRun time.Time ) time.Time {
678
+ func (w weeklyJob ) nextWeekDayAtTime (lastRun time.Time , firstPass bool ) time.Time {
676
679
for _ , wd := range w .daysOfWeek {
677
680
// checking if we're on the same day or later in the same week
678
681
if wd >= lastRun .Weekday () {
@@ -683,11 +686,15 @@ func (w weeklyJob) nextWeekDayAtTime(lastRun time.Time) time.Time {
683
686
// to use in checks to see if we've got our next run time
684
687
atDate := time .Date (lastRun .Year (), lastRun .Month (), lastRun .Day ()+ int (weekDayDiff ), at .Hour (), at .Minute (), at .Second (), lastRun .Nanosecond (), lastRun .Location ())
685
688
686
- if atDate .After (lastRun ) {
689
+ if firstPass && atDate .After (lastRun ) {
687
690
// checking to see if it is after i.e. greater than,
688
691
// and not greater or equal as our lastRun day/time
689
692
// will be in the loop, and we don't want to select it again
690
693
return atDate
694
+ } else if ! firstPass && ! atDate .Before (lastRun ) {
695
+ // now that we're looking at the next week, it's ok to consider
696
+ // the same at time that was last run (as lastRun has been incremented)
697
+ return atDate
691
698
}
692
699
}
693
700
}
@@ -717,21 +724,23 @@ func (m monthlyJob) next(lastRun time.Time) time.Time {
717
724
}
718
725
slices .Sort (daysList )
719
726
720
- next := m .nextMonthDayAtTime (lastRun , daysList )
727
+ firstPass := true
728
+ next := m .nextMonthDayAtTime (lastRun , daysList , firstPass )
721
729
if ! next .IsZero () {
722
730
return next
723
731
}
732
+ firstPass = false
724
733
725
734
from := time .Date (lastRun .Year (), lastRun .Month ()+ time .Month (m .interval ), 1 , 0 , 0 , 0 , 0 , lastRun .Location ())
726
735
for next .IsZero () {
727
- next = m .nextMonthDayAtTime (from , daysList )
736
+ next = m .nextMonthDayAtTime (from , daysList , firstPass )
728
737
from = from .AddDate (0 , int (m .interval ), 0 )
729
738
}
730
739
731
740
return next
732
741
}
733
742
734
- func (m monthlyJob ) nextMonthDayAtTime (lastRun time.Time , days []int ) time.Time {
743
+ func (m monthlyJob ) nextMonthDayAtTime (lastRun time.Time , days []int , firstPass bool ) time.Time {
735
744
// find the next day in the month that should run and then check for an at time
736
745
for _ , day := range days {
737
746
if day >= lastRun .Day () {
@@ -746,11 +755,15 @@ func (m monthlyJob) nextMonthDayAtTime(lastRun time.Time, days []int) time.Time
746
755
continue
747
756
}
748
757
749
- if atDate .After (lastRun ) {
758
+ if firstPass && atDate .After (lastRun ) {
750
759
// checking to see if it is after i.e. greater than,
751
760
// and not greater or equal as our lastRun day/time
752
761
// will be in the loop, and we don't want to select it again
753
762
return atDate
763
+ } else if ! firstPass && ! atDate .Before (lastRun ) {
764
+ // now that we're looking at the next month, it's ok to consider
765
+ // the same at time that was lastRun (as lastRun has been incremented)
766
+ return atDate
754
767
}
755
768
}
756
769
continue
0 commit comments