Skip to content

Commit

Permalink
fix: evaluate if current day of week is valid correctly (#1763)
Browse files Browse the repository at this point in the history
2 issues found
- our cron implementation did not evaluate if the current time matched
the day of the week properly
- discovered in this PR:
https://github.com/TBD54566975/ftl/pull/1733/files
- this revealed that the new shorthand day of the week logic was doing
the equivalent of `* * * * * x *` (every second of the chosen day)
rather than `0 0 0 * * x *` (midnight of the chosen day)

fyi, plan is to replace with an actual cron lib anyway...
  • Loading branch information
matt2e authored Jun 13, 2024
1 parent 5372a00 commit 9f49895
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions internal/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ func isCurrentValueAllowedForDayOfWeekStep(step Step, values componentValues, t
}

results := slices.Map(days, func(day int) bool {
if values[t] < start || values[t] > end {
if value < start || value > end {
return false
}
if (values[t]-start)%incr != 0 {
if (value-start)%incr != 0 {
return false
}
return true
Expand Down
29 changes: 13 additions & 16 deletions internal/cron/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,19 @@ func TestNext(t *testing.T) {
time.Date(2025, 6, 6, 0, 0, 0, 0, time.UTC),
},
}},
// TODO: These two are failing on the NextAfter with inclusive=true
/*
// Every wednesday
{"0 0 0 * * 3 *", [][]time.Time{
{ // 2024-06-09 is a Sunday
time.Date(2024, 6, 9, 0, 0, 0, 0, time.UTC),
time.Date(2024, 6, 12, 0, 0, 0, 0, time.UTC),
},
}},
{"Wednesday", [][]time.Time{
{ // 2024-06-09 is a Sunday
time.Date(2024, 6, 9, 0, 0, 0, 0, time.UTC),
time.Date(2024, 6, 12, 0, 0, 0, 0, time.UTC),
},
}},
*/
// Every wednesday
{"0 0 0 * * 3 *", [][]time.Time{
{ // 2024-06-09 is a Sunday
time.Date(2024, 6, 9, 0, 0, 0, 0, time.UTC),
time.Date(2024, 6, 12, 0, 0, 0, 0, time.UTC),
},
}},
{"Wed", [][]time.Time{
{ // 2024-06-09 is a Sunday
time.Date(2024, 6, 9, 0, 0, 0, 0, time.UTC),
time.Date(2024, 6, 12, 0, 0, 0, 0, time.UTC),
},
}},
} {
t.Run(fmt.Sprintf("CronSeries:%s", tt.str), func(t *testing.T) {
pattern, err := Parse(tt.str)
Expand Down
3 changes: 3 additions & 0 deletions internal/cron/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func (p Pattern) standardizedComponents() ([]Component, error) {
}

components := newComponentsFilled()
components[0] = newComponentWithValue(0) // seconds
components[1] = newComponentWithValue(0) // minutes
components[2] = newComponentWithValue(0) // hours
components[5] = newComponentWithValue(dayOfWeekInt)
return components, nil
}
Expand Down

0 comments on commit 9f49895

Please sign in to comment.