Skip to content

Commit

Permalink
fix: set multi-weekdays bug about issue-590
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry.wang committed Oct 13, 2023
1 parent 0f6d926 commit 5cfa08f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
3 changes: 3 additions & 0 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ func (j *Job) Weekdays() []time.Weekday {
if len(j.scheduledWeekdays) == 0 {
return []time.Weekday{time.Sunday}
}
sort.Slice(j.scheduledWeekdays, func(i, k int) bool {
return j.scheduledWeekdays[i] < j.scheduledWeekdays[k]
})

return j.scheduledWeekdays
}
Expand Down
4 changes: 3 additions & 1 deletion scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,10 @@ func (s *Scheduler) calculateWeeks(job *Job, lastRun time.Time) nextRun {

func (s *Scheduler) calculateTotalDaysDifference(lastRun time.Time, daysToWeekday int, job *Job) int {
if job.getInterval() > 1 {
weekDays := job.Weekdays()

// just count weeks after the first jobs were done
if job.RunCount() < len(job.Weekdays()) {
if job.lastRun.Weekday() != weekDays[len(weekDays)-1] {
return daysToWeekday
}
if daysToWeekday > 0 {
Expand Down
12 changes: 7 additions & 5 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2322,9 +2322,9 @@ func TestScheduler_CheckEveryWeekHigherThanOne(t *testing.T) {
daysToTest []int
caseTest int
}{
{description: "every two weeks after run the first scheduled task", interval: 2, weekDays: []time.Weekday{time.Thursday}, daysToTest: []int{1, 2}, caseTest: 1},
{description: "every three weeks after run the first scheduled task", interval: 3, weekDays: []time.Weekday{time.Thursday}, daysToTest: []int{1, 2}, caseTest: 2},
{description: "every two weeks after run the first 2 scheduled tasks", interval: 2, weekDays: []time.Weekday{time.Thursday, time.Friday}, daysToTest: []int{1, 2, 3}, caseTest: 3},
{description: "every two weeks after run the first scheduled task", interval: 2, weekDays: []time.Weekday{time.Thursday}, daysToTest: []int{1, 2, 15, 16}, caseTest: 1},
{description: "every three weeks after run the first scheduled task", interval: 3, weekDays: []time.Weekday{time.Thursday}, daysToTest: []int{1, 2, 15, 16}, caseTest: 2},
{description: "every two weeks after run the first 2 scheduled tasks", interval: 2, weekDays: []time.Weekday{time.Friday, time.Thursday}, daysToTest: []int{1, 2, 3, 15, 16, 17}, caseTest: 3},
}

const (
Expand All @@ -2347,13 +2347,14 @@ func TestScheduler_CheckEveryWeekHigherThanOne(t *testing.T) {
}
job, err := s.Do(func() {})
require.NoError(t, err)
for numJob, day := range tc.daysToTest {
for _, day := range tc.daysToTest {
lastRun := januaryDay2020At(day)

job.lastRun = lastRun
got := s.durationToNextRun(lastRun, job).duration

if numJob < len(tc.weekDays) {
jobWeekdays := job.Weekdays()
if lastRun.Weekday() < jobWeekdays[len(jobWeekdays)-1] {
assert.Equal(t, wantTimeUntilNextRunOneDay, got)
} else {
if tc.caseTest == 1 {
Expand Down Expand Up @@ -2487,6 +2488,7 @@ func TestScheduler_MultipleAtTime(t *testing.T) {
_getHours(7) + _getMinutes(0),
_getHours(14) + _getMinutes(10),
}

testCases := []struct {
description string
job *Job
Expand Down

0 comments on commit 5cfa08f

Please sign in to comment.