Skip to content

#166 Schedule task every X hours from a date in the past #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions gocron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestScheduler_WeekdaysTodayAfter(t *testing.T) {
if job.NextScheduledTime().Weekday() != timeToSchedule.Weekday() {
t.Errorf("Job scheduled for current weekday for earlier time, should still be scheduled for current weekday (but next week)")
}
nextWeek := time.Date(now.Year(), now.Month(), now.Day()+7, now.Hour(), now.Minute()-1, 0, 0, time.Local)
nextWeek := timeToSchedule.Add(7 * 24 * time.Hour)
if !job.NextScheduledTime().Equal(nextWeek) {
t.Errorf("Job should be scheduled for the correct time next week.\nGot %+v, expected %+v", job.NextScheduledTime(), nextWeek)
}
Expand Down Expand Up @@ -512,16 +512,15 @@ func TestWeekdayBeforeToday(t *testing.T) {
}

weekJob.scheduleNextRun()
sixDaysFromNow := now.AddDate(0, 0, 6)

exp := time.Date(sixDaysFromNow.Year(), sixDaysFromNow.Month(), sixDaysFromNow.Day(), 0, 0, 0, 0, loc)
exp := weekJob.roundToMidnight(now).Add(6 * 24 * time.Hour)
assert.Equal(t, exp, weekJob.nextRun)

// Simulate job run 7 days before
weekJob.lastRun = weekJob.nextRun.AddDate(0, 0, -7)
// Next run
weekJob.scheduleNextRun()
exp = time.Date(sixDaysFromNow.Year(), sixDaysFromNow.Month(), sixDaysFromNow.Day(), 0, 0, 0, 0, loc)
exp = weekJob.roundToMidnight(now).Add(6 * 24 * time.Hour)
assert.Equal(t, exp, weekJob.nextRun)
}

Expand Down Expand Up @@ -698,3 +697,14 @@ func TestGetWeekday(t *testing.T) {
j := Every(1).Weekday(time.Wednesday)
assert.Equal(t, time.Wednesday, j.GetWeekday())
}

func TestScheduleFromPast(t *testing.T) {
past := time.Now().Add(-1 * time.Hour)

sched := NewScheduler()
job := sched.Every(3).Hour().From(&past)
job.Do(task)
next := job.NextScheduledTime()

assert.Equal(t, past.Hour()+3, next.Hour())
}
7 changes: 7 additions & 0 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ func (j *Job) scheduleNextRun() error {
return err
}

if j.nextRun.Before(now) && j.nextRun != time.Unix(0, 0) {
for j.nextRun.Before(now) {
j.nextRun = j.nextRun.Add(periodDuration)
}
j.lastRun = j.nextRun.Add(-periodDuration)
}

switch j.unit {
case seconds, minutes, hours:
j.nextRun = j.lastRun.Add(periodDuration)
Expand Down