Skip to content

Commit 97d19f8

Browse files
committed
fix(cron): prevent run waits from failing after clock changes
1 parent 2671eff commit 97d19f8

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

‎src/cli/cron-cli.test.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,9 +620,19 @@ describe("cron cli", () => {
620620
},
621621
);
622622

623-
it("reduces each history RPC timeout as the cron run wait budget elapses", async () => {
623+
it.each([
624+
{ name: "without a system clock jump", clockJumpMs: 0 },
625+
{ name: "after the system clock jumps forward", clockJumpMs: 3_600_000 },
626+
{ name: "after the system clock jumps backward", clockJumpMs: -3_600_000 },
627+
])("reduces each history RPC timeout $name", async ({ clockJumpMs }) => {
624628
vi.useFakeTimers();
625-
vi.setSystemTime(new Date("2026-07-27T12:00:00.000Z"));
629+
const monotonicNow = performance.now.bind(performance);
630+
let monotonicSample = 0;
631+
vi.spyOn(performance, "now").mockImplementation(
632+
() => monotonicNow() + ++monotonicSample / 1_000,
633+
);
634+
const startedAt = new Date("2026-07-27T12:00:00.000Z");
635+
vi.setSystemTime(startedAt);
626636

627637
const pendingRun = runCronRunAndCaptureExit({
628638
enqueued: true,
@@ -644,6 +654,7 @@ describe("cron cli", () => {
644654
expect(callGatewayFromCli.mock.calls.filter(([method]) => method === "cron.runs")).toHaveLength(
645655
1,
646656
);
657+
vi.setSystemTime(new Date(startedAt.getTime() + clockJumpMs));
647658
await vi.advanceTimersByTimeAsync(25);
648659

649660
const { calls, exitSpy, runOpts } = await pendingRun;
@@ -654,6 +665,7 @@ describe("cron cli", () => {
654665
expect(exitSpy).toHaveBeenCalledWith(0);
655666
expect(runOpts.timeout).toBe("600000");
656667
expect(pollTimeouts).toHaveLength(2);
668+
expect(pollTimeouts.every(Number.isSafeInteger)).toBe(true);
657669
expect(pollTimeouts[0]).toBeLessThanOrEqual(100);
658670
expect(pollTimeouts[1]).toBeLessThanOrEqual(75);
659671
expect(pollTimeouts[1]).toBeLessThan(pollTimeouts[0] ?? 0);

‎src/cli/cron-cli/register.cron-simple.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ async function waitForCronRunCompletion(params: {
6767
pollIntervalMs: number;
6868
}): Promise<CronRunLogEntryResult> {
6969
// Poll the task ledger rather than cron.run because completion state is written asynchronously.
70-
const startedAt = Date.now();
70+
const startedAt = performance.now();
7171
let hasPolled = false;
7272
for (;;) {
73-
const elapsedBeforePollMs = Date.now() - startedAt;
73+
const elapsedBeforePollMs = Math.floor(performance.now() - startedAt);
7474
if (hasPolled && elapsedBeforePollMs >= params.timeoutMs) {
7575
throw new Error(`timed out waiting for cron run ${params.runId}`);
7676
}
@@ -91,7 +91,7 @@ async function waitForCronRunCompletion(params: {
9191
if (entry?.status === "ok" || entry?.status === "error" || entry?.status === "skipped") {
9292
return entry;
9393
}
94-
const elapsedMs = Date.now() - startedAt;
94+
const elapsedMs = Math.floor(performance.now() - startedAt);
9595
if (elapsedMs >= params.timeoutMs) {
9696
throw new Error(`timed out waiting for cron run ${params.runId}`);
9797
}

0 commit comments

Comments
 (0)