#22911: fix(cron): correct next execution time calculation after gateway restart
size: XS
Cluster:
Cron Job Management Fixes
## Summary
Fix incorrect next execution time calculation for periodic (every N minutes) cron jobs after a gateway restart.
## Problem
When a cron job that runs every 30 minutes restarts the gateway, the "next execution" time shown in the dashboard becomes incorrect. For example, if the last execution was 6 minutes ago, the next execution should be in 24 minutes (30-6), but it was showing 56 minutes instead.
This occurred because the schedule calculation was using the current time as the default anchor point instead of the last execution time, causing it to calculate from an incorrect reference point.
## Solution
1. Modified `computeNextRunAtMs` in `schedule.ts` to accept an optional `lastRunAtMs` parameter
2. Updated the anchor calculation for "every" schedules to prioritize the last execution time: `lastRunAtMs ?? schedule.anchorMs ?? nowMs`
3. Updated all call sites in `service/jobs.ts` to pass `job.state.lastRunAtMs` to the function
## Fixes
- Fixes #22895
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Fixed next execution time calculation for periodic cron jobs after gateway restart by passing `lastRunAtMs` to `computeNextRunAtMs` so it uses the last execution time as the anchor instead of current time.
**Critical Issues Found:**
- Missing semicolons on lines 35 (`schedule.ts`) and 87 (`jobs.ts`)
- Missing closing brace `}` on line 87-88 (`jobs.ts`) - the function closing brace was accidentally deleted
- Line 66 in `jobs.ts` also needs `lastRunAtMs` parameter to be consistent with the fix
**Note:** These syntax errors will cause compilation failures and must be fixed before merging.
<h3>Confidence Score: 0/5</h3>
- This PR cannot be merged - it contains critical syntax errors that will prevent compilation
- The PR has the right approach to fixing the cron scheduling issue, but contains multiple syntax errors: missing semicolons (lines 35, 87) and a deleted closing brace that breaks the function structure (line 87-88). Additionally, line 66 is missing the same fix applied elsewhere, creating inconsistent behavior.
- Both `src/cron/service/jobs.ts` and `src/cron/schedule.ts` require immediate attention to fix syntax errors
<sub>Last reviewed commit: af87151</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#23290: fix(cron): use lastRunAtMs for next schedule of interval jobs after...
by SidQin-cyber · 2026-02-22
84.6%
#22948: fix(cron): every-schedule boundary returns nowMs instead of next sl...
by echoVic · 2026-02-21
82.8%
#9060: Fix: Preserve scheduled cron jobs after gateway restart
by vishaltandale00 · 2026-02-04
82.4%
#12747: fix: catch up missed cron-expression job runs on restart
by obin94-commits · 2026-02-09
82.2%
#7022: fix(cron): prevent schedule drift on gateway restart for 'every' jobs
by marciob · 2026-02-02
81.8%
#12303: fix(cron): correct nextRunAtMs calculation and prevent timer stall
by colddonkey · 2026-02-09
81.7%
#13065: fix(cron): Fix "every" schedule not re-arming after gateway restart
by trevorgordon981 · 2026-02-10
81.2%
#8034: fix(cron): run past-due one-shot jobs immediately on startup
by FelixFoster · 2026-02-03
81.0%
#12443: fix(cron): don't advance past-due jobs that haven't been executed
by rummangeminicode · 2026-02-09
80.5%
#18925: fix(cron): stagger missed jobs on restart to prevent gateway overload
by rexlunae · 2026-02-17
80.1%