Files
Moltbot/src/cron/schedule.test.ts
Xinhua Gu dd6047d998 fix(cron): prevent duplicate fires when multiple jobs trigger simultaneously (#14256)
The `computeNextRunAtMs` function used `nowSecondMs - 1` as the
reference time for croner's `nextRun()`, which caused it to return the
current second as a valid next-run time. When a job fired at e.g.
11:00:00.500, computing the next run still yielded 11:00:00.000 (same
second, already elapsed), causing the scheduler to immediately re-fire
the job in a tight loop (15-21x observed in the wild).

Fix: use `nowSecondMs` directly (no `-1` lookback) and change the
return guard from `>=` to `>` so next-run is always strictly after
the current second.

Fixes #14164
2026-02-11 22:04:17 -06:00

71 lines
2.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { computeNextRunAtMs } from "./schedule.js";
describe("cron schedule", () => {
it("computes next run for cron expression with timezone", () => {
// Saturday, Dec 13 2025 00:00:00Z
const nowMs = Date.parse("2025-12-13T00:00:00.000Z");
const next = computeNextRunAtMs(
{ kind: "cron", expr: "0 9 * * 3", tz: "America/Los_Angeles" },
nowMs,
);
// Next Wednesday at 09:00 PST -> 17:00Z
expect(next).toBe(Date.parse("2025-12-17T17:00:00.000Z"));
});
it("computes next run for every schedule", () => {
const anchor = Date.parse("2025-12-13T00:00:00.000Z");
const now = anchor + 10_000;
const next = computeNextRunAtMs({ kind: "every", everyMs: 30_000, anchorMs: anchor }, now);
expect(next).toBe(anchor + 30_000);
});
it("computes next run for every schedule when anchorMs is not provided", () => {
const now = Date.parse("2025-12-13T00:00:00.000Z");
const next = computeNextRunAtMs({ kind: "every", everyMs: 30_000 }, now);
// Should return nowMs + everyMs, not nowMs (which would cause infinite loop)
expect(next).toBe(now + 30_000);
});
it("advances when now matches anchor for every schedule", () => {
const anchor = Date.parse("2025-12-13T00:00:00.000Z");
const next = computeNextRunAtMs({ kind: "every", everyMs: 30_000, anchorMs: anchor }, anchor);
expect(next).toBe(anchor + 30_000);
});
describe("cron with specific seconds (6-field pattern)", () => {
// Pattern: fire at exactly second 0 of minute 0 of hour 12 every day
const dailyNoon = { kind: "cron" as const, expr: "0 0 12 * * *", tz: "UTC" };
const noonMs = Date.parse("2026-02-08T12:00:00.000Z");
it("advances past current second when nowMs is exactly at the match", () => {
// Fix #14164: must NOT return the current second — that caused infinite
// re-fires when multiple jobs triggered simultaneously.
const next = computeNextRunAtMs(dailyNoon, noonMs);
expect(next).toBe(noonMs + 86_400_000); // next day
});
it("advances past current second when nowMs is mid-second (.500) within the match", () => {
// Fix #14164: returning the current second caused rapid duplicate fires.
const next = computeNextRunAtMs(dailyNoon, noonMs + 500);
expect(next).toBe(noonMs + 86_400_000); // next day
});
it("advances past current second when nowMs is late in the matching second (.999)", () => {
const next = computeNextRunAtMs(dailyNoon, noonMs + 999);
expect(next).toBe(noonMs + 86_400_000); // next day
});
it("advances to next day once the matching second is fully past", () => {
const next = computeNextRunAtMs(dailyNoon, noonMs + 1000);
expect(next).toBe(noonMs + 86_400_000); // next day
});
it("returns today when nowMs is before the match", () => {
const next = computeNextRunAtMs(dailyNoon, noonMs - 500);
expect(next).toBe(noonMs);
});
});
});