refactor(cron): dedupe next-run recompute paths

This commit is contained in:
Peter Steinberger
2026-02-16 17:06:10 +00:00
parent 8a67016646
commit b991919755
2 changed files with 39 additions and 52 deletions

View File

@@ -192,6 +192,27 @@ function walkSchedulableJobs(
return changed;
}
function recomputeJobNextRunAtMs(params: { state: CronServiceState; job: CronJob; nowMs: number }) {
let changed = false;
try {
const newNext = computeJobNextRunAtMs(params.job, params.nowMs);
if (params.job.state.nextRunAtMs !== newNext) {
params.job.state.nextRunAtMs = newNext;
changed = true;
}
// Clear schedule error count on successful computation.
if (params.job.state.scheduleErrorCount) {
params.job.state.scheduleErrorCount = undefined;
changed = true;
}
} catch (err) {
if (recordScheduleComputeError({ state: params.state, job: params.job, err })) {
changed = true;
}
}
return changed;
}
export function recomputeNextRuns(state: CronServiceState): boolean {
return walkSchedulableJobs(state, ({ job, nowMs: now }) => {
let changed = false;
@@ -201,21 +222,8 @@ export function recomputeNextRuns(state: CronServiceState): boolean {
const nextRun = job.state.nextRunAtMs;
const isDueOrMissing = nextRun === undefined || now >= nextRun;
if (isDueOrMissing) {
try {
const newNext = computeJobNextRunAtMs(job, now);
if (job.state.nextRunAtMs !== newNext) {
job.state.nextRunAtMs = newNext;
changed = true;
}
// Clear schedule error count on successful computation.
if (job.state.scheduleErrorCount) {
job.state.scheduleErrorCount = undefined;
changed = true;
}
} catch (err) {
if (recordScheduleComputeError({ state, job, err })) {
changed = true;
}
if (recomputeJobNextRunAtMs({ state, job, nowMs: now })) {
changed = true;
}
}
return changed;
@@ -236,21 +244,8 @@ export function recomputeNextRunsForMaintenance(state: CronServiceState): boolea
// If a job was past-due but not found by findDueJobs, recomputing would
// cause it to be silently skipped.
if (job.state.nextRunAtMs === undefined) {
try {
const newNext = computeJobNextRunAtMs(job, now);
if (job.state.nextRunAtMs !== newNext) {
job.state.nextRunAtMs = newNext;
changed = true;
}
// Clear schedule error count on successful computation.
if (job.state.scheduleErrorCount) {
job.state.scheduleErrorCount = undefined;
changed = true;
}
} catch (err) {
if (recordScheduleComputeError({ state, job, err })) {
changed = true;
}
if (recomputeJobNextRunAtMs({ state, job, nowMs: now })) {
changed = true;
}
}
return changed;