fix(cron): restore interval cadence after restart

This commit is contained in:
Peter Steinberger
2026-02-22 20:09:15 +01:00
parent aa4c250eb8
commit 9cf445e37c
3 changed files with 61 additions and 1 deletions

View File

@@ -113,11 +113,19 @@ export function computeJobNextRunAtMs(job: CronJob, nowMs: number): number | und
return undefined;
}
if (job.schedule.kind === "every") {
const everyMs = Math.max(1, Math.floor(job.schedule.everyMs));
const lastRunAtMs = job.state.lastRunAtMs;
if (typeof lastRunAtMs === "number" && Number.isFinite(lastRunAtMs)) {
const nextFromLastRun = Math.floor(lastRunAtMs) + everyMs;
if (nextFromLastRun > nowMs) {
return nextFromLastRun;
}
}
const anchorMs = resolveEveryAnchorMs({
schedule: job.schedule,
fallbackAnchorMs: job.createdAtMs,
});
return computeNextRunAtMs({ ...job.schedule, anchorMs }, nowMs);
return computeNextRunAtMs({ ...job.schedule, everyMs, anchorMs }, nowMs);
}
if (job.schedule.kind === "at") {
// One-shot jobs stay due until they successfully finish.