Scheduled tasks and heartbeat

Declare recurring work, verify a manual run, and understand pausing and deployment triggers.

Use a schedule for clock-driven work, and hand longer processing to a job. Cron defines when the work is due, the heartbeat finds it, and a run record tracks execution.

Read an existing recurring task

In packages/service/features/chat/background.ts, chat.activity.rollup uses defineSchedule with 15 * * * *, scheduling work at minute 15 of every UTC hour. defineScheduledJobHandler passes the scheduled time to the chat.activity.close-periods job. The scheduled time identifies the scheduled run. The handler uses its actual execution time to determine which reporting periods have closed.

The feature registers the schedule through the background assembly. Definition sync projects schedules into the database. Node standalone and container deployments run a heartbeat every 60 seconds by default. Workers uses a one-minute Cron trigger. On heartbeat, the application finds due work and executes its handler or dispatches its job.

How a scheduled instant becomes a persistent job

Stage What the chat rollup does
Registration The feature contributes a schedule and a job through defineBackgroundContribution. The app loads the feature from packages/service/app/background-modules.ts
Trigger defineScheduledJobHandler puts the schedule’s scheduledAt in the job payload
Idempotency enqueueScheduledJob uses the schedule key plus scheduled instant as the idempotency key and forwards the trace ID
Execution The job obtains its declared database and telemetry capabilities, cleans expired previews, and closes reporting periods against the execution clock

The existing test triggers both chat and site analytics. Each initially dispatches one job. Period closeout starts only when the jobs are consumed. Submitting the same scheduled instant again produces no extra messages. This explains why the intent uses the scheduled instant while settlement uses execution time.

Trigger a run before waiting for Cron

Inspect scheduled tasks and runs in Admin. In code, obtain the capability through app.schedule() and call trigger with the schedule key, scheduled time, trigger source, and trace ID. When accepted with a runId, check the run record and verify the resulting write.

Use the Admin clock switch to pause an individual recurring schedule. A pause leaves already claimed runs alone. A resume starts at the next Cron time. Before deployment, verify the time zone, Node heartbeat, and consumer. For an existing Workers deployment, check the Cron trigger configuration. Then exercise pause and resume. Queues and jobs · Deployment