mirror of https://github.com/go-co-op/gocron.git
fix: WithStartDateTimePast now correctly calculates from past time
This commit is contained in:
parent
9b37b3fa08
commit
ce65012a14
6
job.go
6
job.go
|
|
@ -1022,16 +1022,14 @@ type weeklyJob struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w weeklyJob) next(lastRun time.Time) time.Time {
|
func (w weeklyJob) next(lastRun time.Time) time.Time {
|
||||||
firstPass := true
|
next := w.nextWeekDayAtTime(lastRun, true)
|
||||||
next := w.nextWeekDayAtTime(lastRun, firstPass)
|
|
||||||
if !next.IsZero() {
|
if !next.IsZero() {
|
||||||
return next
|
return next
|
||||||
}
|
}
|
||||||
firstPass = false
|
|
||||||
|
|
||||||
startOfTheNextIntervalWeek := (lastRun.Day() - int(lastRun.Weekday())) + int(w.interval*7)
|
startOfTheNextIntervalWeek := (lastRun.Day() - int(lastRun.Weekday())) + int(w.interval*7)
|
||||||
from := time.Date(lastRun.Year(), lastRun.Month(), startOfTheNextIntervalWeek, 0, 0, 0, 0, lastRun.Location())
|
from := time.Date(lastRun.Year(), lastRun.Month(), startOfTheNextIntervalWeek, 0, 0, 0, 0, lastRun.Location())
|
||||||
return w.nextWeekDayAtTime(from, firstPass)
|
return w.nextWeekDayAtTime(from, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w weeklyJob) nextWeekDayAtTime(lastRun time.Time, firstPass bool) time.Time {
|
func (w weeklyJob) nextWeekDayAtTime(lastRun time.Time, firstPass bool) time.Time {
|
||||||
|
|
|
||||||
11
scheduler.go
11
scheduler.go
|
|
@ -508,6 +508,12 @@ func (s *scheduler) selectNewJob(in newJobIn) {
|
||||||
next = j.next(s.now())
|
next = j.next(s.now())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if next.Before(s.now()) {
|
||||||
|
for next.Before(s.now()) {
|
||||||
|
next = j.next(next)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
id := j.id
|
id := j.id
|
||||||
j.timer = s.exec.clock.AfterFunc(next.Sub(s.now()), func() {
|
j.timer = s.exec.clock.AfterFunc(next.Sub(s.now()), func() {
|
||||||
select {
|
select {
|
||||||
|
|
@ -559,6 +565,11 @@ func (s *scheduler) selectStart() {
|
||||||
if next.IsZero() {
|
if next.IsZero() {
|
||||||
next = j.next(s.now())
|
next = j.next(s.now())
|
||||||
}
|
}
|
||||||
|
if next.Before(s.now()) {
|
||||||
|
for next.Before(s.now()) {
|
||||||
|
next = j.next(next)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
jobID := id
|
jobID := id
|
||||||
j.timer = s.exec.clock.AfterFunc(next.Sub(s.now()), func() {
|
j.timer = s.exec.clock.AfterFunc(next.Sub(s.now()), func() {
|
||||||
|
|
|
||||||
|
|
@ -2876,3 +2876,34 @@ func TestScheduler_WithMonitor(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestScheduler_WithStartAtDateTimePast(t *testing.T) {
|
||||||
|
defer verifyNoGoroutineLeaks(t)
|
||||||
|
|
||||||
|
// Monday
|
||||||
|
testTime := time.Date(2024, time.January, 1, 9, 0, 0, 0, time.UTC)
|
||||||
|
|
||||||
|
fakeClock := clockwork.NewFakeClockAt(testTime)
|
||||||
|
|
||||||
|
s := newTestScheduler(t, WithClock(fakeClock))
|
||||||
|
j, err := s.NewJob(
|
||||||
|
WeeklyJob(2, NewWeekdays(time.Sunday), NewAtTimes(NewAtTime(10, 0, 0))),
|
||||||
|
NewTask(func() {}),
|
||||||
|
WithStartAt(
|
||||||
|
// The start time is in the past (Dec 30, 2023 9am) which is a Saturday
|
||||||
|
WithStartDateTimePast(testTime.Add(-time.Hour*24*2)),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
s.Start()
|
||||||
|
|
||||||
|
nextRun, err := j.NextRun()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.NoError(t, s.Shutdown())
|
||||||
|
|
||||||
|
// Because the start time was in the past - we expect it to schedule 2 intervals ahead, pasing the first available Sunday
|
||||||
|
// which was in the past Dec 31, 2023, so the next is Jan 7, 2024
|
||||||
|
assert.Equal(t, time.Date(2024, time.January, 7, 10, 0, 0, 0, time.UTC), nextRun)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue