Skip to content
Français

Schedule recurring runs

Publish workflow jobs on a cron schedule with an explicit time zone.

Create a schedule with createCronSchedule() and give it a cron expression and time zone. runSchedules() publishes a queue job at each matching time until you abort its signal; a worker runs the job separately.

import { createCronSchedule } from "@elie-laloum/outpost/queues";

export const timeZone = "Europe/Paris";
export const day = (slot: Date) =>
  slot.toLocaleDateString("en-CA", { timeZone });
export const schedules = [
  {
    name: "nightly-audit",
    cron: createCronSchedule("0 2 * * 1-5", { timeZone }),
    handler: "audit",
    runId: (slot: Date) => `audit-${day(slot)}`,
    input: (slot: Date) => ({ day: day(slot) }),
  },
];
import {
  createSqliteTaskQueue,
  runSchedules,
} from "@elie-laloum/outpost/queues";
import { schedules } from "./audit-schedule.ts";

export const queue = await createSqliteTaskQueue(".outpost/jobs.sqlite");
export const stop = new AbortController();
process.once("SIGINT", () => stop.abort());
try {
  await runSchedules({ queue, signal: stop.signal, schedules });
} finally {
  queue.close();
}

At 02:00 Paris time, Monday to Friday, the scheduler publishes a job for the audit handler with a runId such as audit-2026-09-30. Ctrl+C aborts the signal and runSchedules() resolves.

node scheduler.ts

API reference: CronOptions.

A worker opens the same queue and registers audit with defineWorkflowJob(), which runs one checkpointed workflow per runId: see Job queues and workers. The Nightly maintenance recipe shows the scheduler and the worker together.

Use five fields in minute, hour, day-of-month, month and day-of-week order. For example, 0 2 * * 1-5 schedules 02:00 on weekdays; @daily schedules midnight. Set timeZone explicitly when the local time matters. The createCronSchedule contract lists names, ranges, steps, macros and the rule for combining both day fields.

slot.toISOString().slice(0, 10) gives the UTC date. At 01:00 in Paris, that is still the previous day.

Use slot.toLocaleDateString("en-CA", { timeZone }) with the schedule’s time zone, as in the first snippet, to get the local date as YYYY-MM-DD.

Slots are wall-clock times in the schedule’s time zone.

  • Skipped time: A time that does not exist on the spring-forward day does not fire that day.
  • Repeated time: A time that occurs twice on the fall-back day fires once, at its first occurrence.

Each slot publishes the job schedule:<name>:<slot ISO time>. A restarted scheduler, or several replicas sharing one queue, publish the same job ID, and the queue keeps a single job.

runId and input must depend only on the slot. The queue rejects a second publication of the same ID with a different request.

Two schedules can return the same runId for one day: the second job then reuses the same checkpointed run if its input is the same (a different input fails with an incompatible checkpoint), like the 07:00 resume in Nightly maintenance.

A slot is published only if at most maxLateMs has passed since it (60 000 ms by default). After a restart or a suspended process, only the latest missed slot within that window is published; older ones are skipped.

Raise maxLateMs to catch up a slot missed during a longer outage, for example maxLateMs: 6 * 60 * 60_000 for six hours.

Without onError, the first failed publication stops every schedule and rejects runSchedules(). With it, you receive the error with the schedule name and slot, and scheduling continues with the next slot.

import { runSchedules } from "@elie-laloum/outpost/queues";
import type { TaskQueue, TriggerSchedule } from "@elie-laloum/outpost/queues";

function schedule(
  queue: TaskQueue,
  schedules: TriggerSchedule[],
  signal: AbortSignal,
) {
  return runSchedules({
    queue,
    schedules,
    signal,
    onError: (error, { schedule, slot }) =>
      console.error(`${schedule} ${slot.toISOString()}`, error),
  });
}

A failed slot is not published again. Errors thrown by onError are ignored.

next(after) returns the first slot strictly after a date, previous(at) the latest slot at or before it. Neither publishes anything.

import { createCronSchedule } from "@elie-laloum/outpost/queues";

const nightly = createCronSchedule("30 2 * * *", { timeZone: "Europe/Paris" });
console.log(nightly.next(new Date("2026-03-28T12:00:00Z")).toISOString());
// Example output: 2026-03-30T00:30:00.000Z

This prints 2026-03-30T00:30:00.000Z: 02:30 does not exist in Paris on 29 March 2026.

createCronSchedule() throws on an invalid field, an unknown time zone or an expression with no occurrence, such as 0 0 30 2 *.

Without a long-running process, a scheduled CI job, such as a GitHub Actions schedule workflow, can start the workflow directly with a checkpoint. See Run in CI.

A file recipe uses the same schedule-to-queue contract: a timer only publishes its deterministic job, and the worker allocates the workspace. See file workspaces and recipe services.

  • The finest resolution is one minute.
  • After downtime, only the latest missed slot within maxLateMs is published.
  • A schedule name is unique and uses letters, digits, ., _ and - (128 characters at most). A runId has at most 256 characters.

API: createCronSchedule · runSchedules · TriggerSchedule · CronSchedule · defineWorkflowJob.

Wall-clock deduplication also applies to wildcard expressions such as 0 * * * *: the repeated local hour at the autumn DST transition fires once, leaving a two-hour real-time gap. Use UTC for a constant hourly cadence. Time zones are interpreted by Intl, including its accepted aliases; prefer explicit IANA regions rather than abbreviations such as EST. maxLateMs must be positive. A slow queue publication counts as lateness too: only the latest eligible slot is published after it finishes, and intermediate slots are intentionally coalesced. onError reports publication failures, not these coalesced slots.