Skip to content

Commit

Permalink
fix: set multi-weekdays bug about issue-590 (#592)
Browse files Browse the repository at this point in the history
Co-authored-by: jerry.wang <jerry.wang>
Co-authored-by: John Roesler <johnrroesler@gmail.com>
  • Loading branch information
XiXiangFiles and JohnRoesler authored Oct 28, 2023
1 parent cda6e1c commit 5f95210
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 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: 2 additions & 2 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ 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 {
// just count weeks after the first jobs were done
if job.RunCount() < len(job.Weekdays()) {
weekDays := job.Weekdays()
if job.lastRun.Weekday() != weekDays[len(weekDays)-1] {
return daysToWeekday
}
if daysToWeekday > 0 {
Expand Down
11 changes: 6 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

0 comments on commit 5f95210

Please sign in to comment.