Skip to content

Commit 9862dd3

Browse files
author
Peter Steinberger
committed
fix(cron): replay missed slots within their first second
1 parent d669e9e commit 9862dd3

5 files changed

Lines changed: 68 additions & 14 deletions

File tree

‎src/cron/service.jobs.test.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,12 +1138,16 @@ describe("computeJobPreviousRunAtOrBeforeMs", () => {
11381138
};
11391139
}
11401140

1141-
it("includes an exact boundary and keeps the prior slot between boundaries", () => {
1142-
const job = createCronJob({ kind: "cron", expr: "* * * * * *", tz: "UTC", staggerMs: 0 });
1141+
it.each([
1142+
["five-field", "* * * * *"],
1143+
["six-field", "* * * * * *"],
1144+
])("includes exact and subsecond boundaries for %s schedules", (_label, expr) => {
1145+
const job = createCronJob({ kind: "cron", expr, tz: "UTC", staggerMs: 0 });
11431146
const boundary = Date.parse("2025-12-13T04:02:00.000Z");
11441147

11451148
expect(computeJobPreviousRunAtOrBeforeMs(job, boundary)).toBe(boundary);
11461149
expect(computeJobPreviousRunAtOrBeforeMs(job, boundary + 500)).toBe(boundary);
1150+
expect(computeJobPreviousRunAtOrBeforeMs(job, boundary + 999)).toBe(boundary);
11471151
});
11481152

11491153
it("includes an exact effective boundary after per-job staggering", () => {
@@ -1158,6 +1162,9 @@ describe("computeJobPreviousRunAtOrBeforeMs", () => {
11581162

11591163
expect(effectiveBoundary).toBeTypeOf("number");
11601164
expect(computeJobPreviousRunAtOrBeforeMs(job, effectiveBoundary!)).toBe(effectiveBoundary);
1165+
expect(computeJobPreviousRunAtOrBeforeMs(job, effectiveBoundary! + 500)).toBe(
1166+
effectiveBoundary,
1167+
);
11611168
});
11621169
});
11631170

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Restart catch-up must include a missed cron slot throughout its first second.
2+
import { describe, expect, it, vi } from "vitest";
3+
import { CronService } from "./service.js";
4+
import { setupCronServiceSuite, writeCronStoreSnapshot } from "./service.test-harness.js";
5+
6+
const { logger, makeStorePath } = setupCronServiceSuite({
7+
prefix: "openclaw-cron-subsecond-",
8+
baseTimeIso: "2025-12-13T04:02:00.500Z",
9+
});
10+
11+
describe("CronService restart catch-up within a cron slot's first second", () => {
12+
it("replays the missed slot when the persisted next run already advanced", async () => {
13+
const restartAtMs = Date.parse("2025-12-13T04:02:00.500Z");
14+
const store = await makeStorePath();
15+
const runCommandJob = vi.fn(async () => ({ status: "ok" as const, summary: "done" }));
16+
17+
await writeCronStoreSnapshot({
18+
storePath: store.storePath,
19+
jobs: [
20+
{
21+
id: "restart-missed-subsecond-slot",
22+
name: "every minute",
23+
enabled: true,
24+
createdAtMs: Date.parse("2025-12-10T12:00:00.000Z"),
25+
updatedAtMs: Date.parse("2025-12-13T04:01:00.000Z"),
26+
schedule: { kind: "cron", expr: "* * * * *", tz: "UTC" },
27+
sessionTarget: "isolated",
28+
wakeMode: "now",
29+
payload: { kind: "command", argv: ["echo", "FIRED"] },
30+
state: {
31+
nextRunAtMs: Date.parse("2025-12-13T04:03:00.000Z"),
32+
lastRunAtMs: Date.parse("2025-12-13T04:01:00.000Z"),
33+
lastStatus: "ok",
34+
},
35+
},
36+
],
37+
});
38+
39+
const cron = new CronService({
40+
storePath: store.storePath,
41+
cronEnabled: true,
42+
log: logger,
43+
enqueueSystemEvent: vi.fn(),
44+
requestHeartbeat: vi.fn(),
45+
runIsolatedAgentJob: vi.fn(async () => ({ status: "ok" as const })),
46+
runCommandJob: runCommandJob as never,
47+
});
48+
49+
try {
50+
await cron.start();
51+
expect(runCommandJob).toHaveBeenCalledTimes(1);
52+
expect(cron.getJob("restart-missed-subsecond-slot")?.state.lastRunAtMs).toBe(restartAtMs);
53+
} finally {
54+
cron.stop();
55+
await store.cleanup();
56+
}
57+
});
58+
});

‎src/cron/service/jobs-scheduling.ts‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,6 @@ export function computeJobNextRunAtMs(job: CronJob, nowMs: number): number | und
341341
return isFiniteTimestamp(next) ? next : undefined;
342342
}
343343

344-
/** Computes the previous effective cron timestamp, including per-job staggering. */
345-
export function computeJobPreviousRunAtMs(job: CronJob, nowMs: number): number | undefined {
346-
if (!isJobEnabled(job) || job.schedule.kind !== "cron") {
347-
return undefined;
348-
}
349-
const previous = computeStaggeredCronPreviousRunAtMs(job, nowMs);
350-
return isFiniteTimestamp(previous) ? previous : undefined;
351-
}
352-
353344
/** Computes the latest effective cron timestamp at or before the supplied time. */
354345
export function computeJobPreviousRunAtOrBeforeMs(job: CronJob, nowMs: number): number | undefined {
355346
if (!isJobEnabled(job) || job.schedule.kind !== "cron") {

‎src/cron/service/jobs.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ export {
6161
findJobOrThrow,
6262
isJobEnabled,
6363
computeJobNextRunAtMs,
64-
computeJobPreviousRunAtMs,
6564
computeJobPreviousRunAtOrBeforeMs,
6665
recordScheduleComputeError,
6766
recomputeNextRuns,

‎src/cron/service/timer-runnable.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { parseAbsoluteTimeMs } from "../parse.js";
22
import type { CronJob } from "../types.js";
33
import {
4-
computeJobPreviousRunAtMs,
54
computeJobPreviousRunAtOrBeforeMs,
65
DEFAULT_ERROR_BACKOFF_SCHEDULE_MS,
76
hasActiveCronRun,
@@ -26,7 +25,7 @@ export function hasMissedCronSlotSinceLastRun(job: CronJob, nowMs: number): bool
2625
}
2726
let previousRunAtMs: number | undefined;
2827
try {
29-
previousRunAtMs = computeJobPreviousRunAtMs(job, nowMs);
28+
previousRunAtMs = computeJobPreviousRunAtOrBeforeMs(job, nowMs);
3029
} catch {
3130
return false;
3231
}

0 commit comments

Comments
 (0)