fix(cron): migrate legacy schedule cron fields on load (#28889)

Backfill legacy jobs that still use schedule.cron and jobId so upgraded instances keep firing existing cron schedules instead of failing silently.

Closes #28861
This commit is contained in:
Sid
2026-03-01 20:53:39 +08:00
committed by GitHub
parent d509a81a12
commit 504c1f3607
6 changed files with 147 additions and 6 deletions

View File

@@ -148,4 +148,59 @@ describe("CronService store migrations", () => {
cron.stop();
await store.cleanup();
});
it("migrates legacy cron fields (jobId + schedule.cron) and defaults wakeMode", async () => {
const store = await makeStorePath();
await fs.mkdir(path.dirname(store.storePath), { recursive: true });
await fs.writeFile(
store.storePath,
JSON.stringify(
{
version: 1,
jobs: [
{
jobId: "legacy-cron-field-job",
name: "legacy cron field",
enabled: true,
createdAtMs: Date.parse("2026-02-01T12:00:00.000Z"),
updatedAtMs: Date.parse("2026-02-05T12:00:00.000Z"),
schedule: { kind: "cron", cron: "*/5 * * * *", tz: "UTC" },
payload: { kind: "systemEvent", text: "tick" },
state: {},
},
],
},
null,
2,
),
"utf-8",
);
const cron = await createStartedCron(store.storePath).start();
const jobs = await cron.list({ includeDisabled: true });
const job = jobs.find((entry) => entry.id === "legacy-cron-field-job");
expect(job).toBeDefined();
expect(job?.wakeMode).toBe("now");
expect(job?.schedule.kind).toBe("cron");
if (job?.schedule.kind === "cron") {
expect(job.schedule.expr).toBe("*/5 * * * *");
}
const persisted = JSON.parse(await fs.readFile(store.storePath, "utf-8")) as {
jobs: Array<Record<string, unknown>>;
};
const persistedJob = persisted.jobs.find((entry) => entry.id === "legacy-cron-field-job");
expect(persistedJob).toBeDefined();
expect(persistedJob?.jobId).toBeUndefined();
expect(persistedJob?.wakeMode).toBe("now");
const persistedSchedule =
persistedJob?.schedule && typeof persistedJob.schedule === "object"
? (persistedJob.schedule as Record<string, unknown>)
: null;
expect(persistedSchedule?.cron).toBeUndefined();
expect(persistedSchedule?.expr).toBe("*/5 * * * *");
cron.stop();
await store.cleanup();
});
});