Schedules
A schedule is a run of kind schedule. When it fires, it starts a new run of a target definition. Deploys can add, change, pause, or retire schedules without minting a new run id.
Define a schedule#
cron, every, and once are the three specs. The default input callback runs at fire time, so it sees the deployment that is active then, not the one that created the schedule. Pass the module next to the module that owns the target.
import { createLooms } from '@swirls/looms/runtime'import { cron, defineSchedule, schedule } from '@swirls/looms/schedule'import { defineWorkflow, workflow } from '@swirls/looms/workflow'const report = defineWorkflow({ name: 'report', nodes: [{ id: 'write', run: (ctx) => ctx.input ?? null }],})const nightly = defineSchedule({ name: 'nightly-report', target: report, spec: cron('0 2 * * *', { tz: 'UTC' }), input: ({ at }) => ({ at }),})const looms = createLooms({ modules: [ workflow({ definitions: [report] }), schedule({ definitions: [nightly] }), ],})await looms.start(nightly, null, { runId: 'nightly-report' })every('15m') repeats from an optional anchor. once(at) fires one absolute timestamp. A cron or interval tighter than minIntervalMs (60 seconds unless you pass a lower floor) is rejected and the schedule does not arm.
When a fire is skipped#
misfire: 'fire-once' (the default) fires a late slot once, then plans the next future slot. 'skip' drops that slot. A late alarm does not catch up a backlog.overlap: 'skip' drops a fire whose previous target run is still going; 'allow' starts another one. Either skip consumes the slot and records schedule.skipped.
beforeFire can hold a fire for a reason you choose, such as a billing hold. The reason is on the skipped event, so a hold is visible instead of a silent miss. Resume plans the next occurrence after now and does not replay what was missed.
Change a running schedule#
Send update, pause, resume, retire, or trigger. An update that matches the current spec is a no-op. retire parks a removed schedule without cancelling it, so the same run id can be resumed. ensureSchedule starts a missing schedule, updates a changed one, and resumes a paused or retired one.
import { ensureSchedule, update } from '@swirls/looms/schedule'await ensureSchedule(looms, nightly, 'nightly-report', { spec: cron('0 3 * * 1-5', { tz: 'UTC' }),})await looms.signal('nightly-report', [update({ spec: cron('0 3 * * *', { tz: 'UTC' }) })])The fired run id is the schedule id, fire, and the slot time. A manual fire uses the trigger id instead of the slot. Repeating the same slot starts nothing again.
Audit fires#
Each fire appends schedule.fired with the id of the run it started, or schedule.skipped with a reason. When the target accepts the start, the schedule's log also gets runtime.run.launched with the run id, definition, and effect id, so listing what a schedule started reads one log. The started run's runtime.run.started payload records launchedBy: the schedule run, its thread, the fire's effect id, and the id and seq of the event that requested the fire. The event's origin is { type: 'run' }, and its correlationId is the schedule's run id, or the schedule's own correlation id when another run launched the schedule. Join launchedBy.effectId to the effectId on schedule.fired; no run id needs parsing. Every run an effect starts through RunLauncher carries the same fields, and the runtime sets them, so a handler cannot omit or forge them.
Determinism#
The fold does not read the clock. Planning and firing are effects, and each one records the next time on schedule.planned, schedule.fired, or schedule.skipped. Replaying the log produces the same state. The schedule thread forgets completed effect ids, so a long-lived schedule does not accumulate one id per fire.
Cloudflare#
On Cloudflare, give each schedule its own Durable Object and pass durableObjectLauncher so a fire can start the target object. Idle objects hibernate and are not billed for duration. A steady fire writes about eight rows and sets one alarm. Pass modules to routeToDurableObject when the debugger should list schedule definitions.
An open server-sent event stream keeps the object awake, because the runtime heartbeats every few seconds. Do not attach a remote projector to a schedule object. Pause and retire delete the alarm. A one-minute cron is the expensive case: the floor is 60 seconds so that cadence is opt-in, and ten thousand of them is the line item to budget, not the idle time.
The examples include a one-shot that fires during start and exits. The same definition shape takes a cron spec in production.