Skip to content

fix(scheduler): report real cron runs when a time window is set - #255

Open
ThomasBarth04 wants to merge 1 commit into
marcus:mainfrom
ThomasBarth04:fix/cron-window-next-run
Open

ThomasBarth04 wants to merge 1 commit into
marcus:mainfrom
ThomasBarth04:fix/cron-window-next-run

Conversation

@ThomasBarth04

Copy link
Copy Markdown

Problem

When schedule.cron and schedule.window are both set, NextRun() and NextRuns() move any cron tick that falls outside the window to the window start time. Cron never fires at that time, though. The job runs only on cron ticks, and runJobs skips the ones outside the window. So nightshift preview, nightshift doctor and the daemon's daemon running next_run=… log line show runs that never happen.

Examples:

config shown before actual next run
cron: "0 * * * *", window 22:30–06:00 22:30 23:00
cron: "15 */2 * * *", window 09:00–17:00 09:00 10:15
cron: "0 2 * * *", window 09:00–17:00 09:00 every day never runs

The last case is the worst: the config is silently broken, yet doctor reports schedule OK, next run … 09:00.

Fix

  • Add nextCronRunInWindow, which returns the first cron tick inside the window. When a tick falls outside the window, it jumps straight to the next window opening instead of checking every tick in between. The search stops after 5 years, the same limit robfig/cron uses.
  • In cron mode, NextRuns and updateNextRunLocked now use this function. If the cron never fires inside the window, NextRuns returns a new ErrNoRunInWindow.
  • Interval mode works the same as before. Its loop really does sleep until the window start, so moving the next run there is correct.
  • doctor now shows cron schedule never fires inside time window as a warning instead of "OK". The daemon logs a warning instead of next_run=0001-01-01….

Tests

  • Table-driven TestNextCronRunInWindow with fixed times: tick already inside the window, skipping to the first tick inside it, no snapping to the window start, a weekday-only cron across a weekend, and a cron that never fires inside the window.
  • TestNextRuns_CronWithWindow checks that every returned run is a real cron tick inside the window. TestNextRuns_CronNeverInWindow checks that the new error is returned.
  • I checked that the new tests fail against the old snapping logic. gofmt, go vet ./... and go test ./... all pass.

🤖 Generated with Claude Code

With a cron schedule plus a window, NextRun/NextRuns snapped any tick
outside the window to the window start. Cron never fires at that time,
so `preview`, `doctor` and the daemon's startup log showed runs that
would not happen (e.g. cron "0 * * * *" with window 22:30-06:00 showed
22:30 instead of 23:00; cron "0 2 * * *" with window 09:00-17:00 showed
a daily 09:00 run although the job never runs).

Now cron mode returns the first cron tick inside the window, and
ErrNoRunInWindow when none exists. Interval mode keeps snapping, since
its loop really does wake at the window start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant