fix(cron): ensure timer is armed after job creation#11225
fix(cron): ensure timer is armed after job creation#11225git-tao wants to merge 1 commit intoopenclaw:mainfrom
Conversation
Problem: Cron jobs created after gateway start may not execute because: 1. nextRunAtMs might not be computed correctly for the new job 2. armTimer might exit early without setting up a timer 3. No logging made these failures invisible Solution: 1. Call recomputeNextRuns() after adding a job to ensure all jobs have correct nextRunAtMs values (defensive consistency check) 2. Add debug logging in armTimer when timer is not armed (shows why) 3. Add info logging after job add (confirms timer state) This helps diagnose issues like openclaw#11217 where jobs appear valid but never execute. Fixes openclaw#11217
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 87559aff0b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // Defensive: recompute all next-run times to ensure consistency | ||
| // This handles edge cases where job.state.nextRunAtMs might not be set correctly | ||
| recomputeNextRuns(state); |
There was a problem hiding this comment.
Avoid skipping overdue runs when adding jobs
Calling recomputeNextRuns(state) here recalculates nextRunAtMs for all existing jobs using now, which moves any overdue job (e.g., a job whose nextRunAtMs is in the past due to a paused process or time jump) to its next future interval. That means adding a new job can silently clear a due run that would have fired immediately (since armTimer would otherwise schedule with delay 0), so the missed run never executes. If you want to be defensive for the new job only, consider recomputing/setting nextRunAtMs only for that job or deferring recompute until after overdue jobs are handled.
Useful? React with 👍 / 👎.
Problem
Cron jobs created after gateway start may not execute because:
nextRunAtMsmight not be computed correctly for the new jobarmTimermight exit early without setting up a timerUsers report jobs appearing valid (
enabled: true) but havinglastRun: nullindefinitely.Solution
Defensive recomputation: Call
recomputeNextRuns()after adding a job to ensure all jobs have correctnextRunAtMsvaluesDebug logging in armTimer: When timer is not armed, log why:
nextRunAtMs(with counts for debugging)Info logging after job add: Confirms:
nextRunAtMsChanges
src/cron/service/ops.ts: AddedrecomputeNextRuns()call and logging after job creationsrc/cron/service/timer.ts: Added debug logging for early exits inarmTimer()Testing
These changes are additive (logging) and defensive (recompute). They don't change the core logic, just ensure:
Related
Fixes #11217
Greptile Overview
Greptile Summary
Changes update the cron scheduler to be more robust when jobs are created after startup. After adding a job, the service now recomputes
nextRunAtMsacross all jobs, persists the updated store, and logs the newly added job’s computed next run plus whether a timer is armed. The timer arming logic also adds debug logs for early exit cases (scheduler disabled / no jobs with computednextRunAtMs) and logs when a timer is successfully armed (including whether the delay was clamped).Confidence Score: 5/5
(2/5) Greptile learns from your feedback when you react with thumbs up/down!
Context used:
dashboard- CLAUDE.md (source)dashboard- AGENTS.md (source)