diff --git a/src/cron/schedule.ts b/src/cron/schedule.ts index 1be95acaa..252d29bab 100644 --- a/src/cron/schedule.ts +++ b/src/cron/schedule.ts @@ -4,7 +4,18 @@ import { parseAbsoluteTimeMs } from "./parse.js"; export function computeNextRunAtMs(schedule: CronSchedule, nowMs: number): number | undefined { if (schedule.kind === "at") { - const atMs = parseAbsoluteTimeMs(schedule.at); + // Handle both canonical `at` (string) and legacy `atMs` (number) fields. + // The store migration should convert atMs→at, but be defensive in case + // the migration hasn't run yet or was bypassed. + const sched = schedule as { at?: string; atMs?: number | string }; + const atMs = + typeof sched.atMs === "number" && Number.isFinite(sched.atMs) && sched.atMs > 0 + ? sched.atMs + : typeof sched.atMs === "string" + ? parseAbsoluteTimeMs(sched.atMs) + : typeof sched.at === "string" + ? parseAbsoluteTimeMs(sched.at) + : null; if (atMs === null) { return undefined; } diff --git a/src/cron/service/jobs.ts b/src/cron/service/jobs.ts index a9eda476c..a01475224 100644 --- a/src/cron/service/jobs.ts +++ b/src/cron/service/jobs.ts @@ -52,7 +52,18 @@ export function computeJobNextRunAtMs(job: CronJob, nowMs: number): number | und if (job.state.lastStatus === "ok" && job.state.lastRunAtMs) { return undefined; } - const atMs = parseAbsoluteTimeMs(job.schedule.at); + // Handle both canonical `at` (string) and legacy `atMs` (number) fields. + // The store migration should convert atMs→at, but be defensive in case + // the migration hasn't run yet or was bypassed. + const schedule = job.schedule as { at?: string; atMs?: number | string }; + const atMs = + typeof schedule.atMs === "number" && Number.isFinite(schedule.atMs) && schedule.atMs > 0 + ? schedule.atMs + : typeof schedule.atMs === "string" + ? parseAbsoluteTimeMs(schedule.atMs) + : typeof schedule.at === "string" + ? parseAbsoluteTimeMs(schedule.at) + : null; return atMs !== null ? atMs : undefined; } return computeNextRunAtMs(job.schedule, nowMs);