All Agent Engineering

Schedule recoverable work for a Cloudflare Agent

Check the callback, trigger time, persistence, listing, cancellation, and where keepAliveWhile does and does not apply.

AINavWritten / Updated
On this page

At a glance

A Cloudflare Agent stores its schedules in SQLite and runs them from the Lifecycle's job queue, so they survive restarts and evictions. This checklist addresses one concrete problem: given a callback and its task parameters, how do you confirm the schedule was registered correctly, can be found, can be cancelled, and is not confused with a heartbeat that merely prevents idle eviction?

Runtime and applicability

Article type
Engineering practice
Verified
Applies to
  • Cloudflare Agents SDK Schedule recoverable work for a Cloudflare Agent

Execution locations and responsibilities

  • Cloudflare Agent server and connected client Use the workflow below to distinguish responsibilities of the caller, runtime, and external actions.
Engineering workflow order
Engineering workflow order1. Confirm the callback is a method on the Agent → 2. Choose the trigger-time form → 3. Place recurring schedules in onStart → 4. Keep task parameters small → 5. Verify registered schedules through lookup → 6. Design the cancellation path → 7. Separate heartbeats from work that must finish. Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.1. Confirm the callback is a method on the Agent2. Choose the trigger-time form3. Place recurring schedules in onStart4. Keep task parameters small5. Verify registered schedules through lookup6. Design the cancellation path7. Separate heartbeats from work that must finish

Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.

When to use

  • In schedule(when, callback, payload?, options?), when can be a delay in seconds, a Date, or a cron string, and callback must name a method on the Agent. Recurring work can use scheduleEvery(intervalSeconds, callback, payload?, options?).

When not to use

  • The supplied material gives no retry counts, limits or natural-language time parsing details; those belong to the Scheduler capability documentation and are not inferred here.
  • keepAliveWhile only prevents idle eviction; a deploy or crash still interrupts work in flight, so it cannot guarantee completion.
  • getSchedule() and getSchedules() are deprecated and should not be treated as long-term interfaces in the checklist.

Prerequisites and input

  • An Agent subclass, plus a decision on which callback should run on a schedule and what parameters it needs.
  • A decision on whether the task is one-off or recurring, and whether its time is a delay in seconds, a Date, or a cron string.

Engineering checks

Registration: callback name and trigger time — In schedule(when, callback, payload?, options?), when can be a delay in seconds, a Date, or a cron string, and callback must name a method on the Agent. Recurring work can use scheduleEvery(intervalSeconds, callback, payload?, options?).

Persistence and recovery — Schedules are stored in SQLite and run from the Lifecycle's job queue, so they survive restarts and evictions. A recurring schedule registered in onStart is idempotent and adds one row however often the Agent starts. A deploy or crash still stops work that is in flight.

Management: lookup and cancellation — getScheduleById(id) and listSchedules(criteria?) look schedules up, and cancelSchedule(id) cancels one. The older getSchedule() and getSchedules() are deprecated synchronous versions, so a new checklist should not depend on them.

1. Confirm the callback is a method on the Agent

Where: the Agent class. Input: the planned callback names. Action: check that each name matches a real method on the Agent and note the parameter shape the method accepts. Completion check: every callback name resolves to a method in the class. Stop condition: do not register a schedule whose name has no matching method.

2. Choose the trigger-time form

Where: the code that calls schedule or scheduleEvery. Input: the task's timing requirement. Action: use seconds for a one-off delay, a Date for a fixed moment, a cron string for calendar-style repetition, and scheduleEvery for a fixed interval. Completion check: each task's form matches its requirement. Stop condition: if a requirement fits none of these and the supplied material says nothing more, stop and read the Scheduler capability documentation.

3. Place recurring schedules in onStart

Where: onStart. Input: the list of recurring tasks. Action: register recurring schedules in onStart and rely on their idempotence so each start does not add duplicate rows. Completion check: you can explain why the task has one row after several starts. Stop condition: if a recurring schedule sits on a path that runs per request, move it before continuing.

4. Keep task parameters small

Where: the third argument of schedule. Input: the data the callback needs. Action: pass only what the callback requires, read it as the payload inside the callback, and make sure the callback behaves sensibly when no payload is given. Completion check: each callback's parameter shape is written down. Stop condition: if the payload would hold content that should not be persisted, pass an identifier and load the data inside the callback.

5. Verify registered schedules through lookup

Where: management code that calls getScheduleById and listSchedules. Input: the schedule identifiers and the query criteria. Action: confirm the registered entries match expectations and that the deprecated getSchedule() and getSchedules() are not used. Completion check: every expected task can be found. Stop condition: if an expected task is missing or duplicated, do not design cancellation yet.

6. Design the cancellation path

Where: code that calls cancelSchedule. Input: the identifier of the schedule to cancel. Action: define who may trigger cancellation, confirm the identifier by lookup first, and list schedules again afterwards to confirm the entry is gone. Completion check: the lists from before and after cancellation are recorded. Stop condition: do not cancel when you cannot confirm the identifier belongs to the intended task.

7. Separate heartbeats from work that must finish

Where: wherever keepAliveWhile or keepAlive is used. Input: long-running work. Action: note that an idle Durable Object is evicted after about 70 to 140 seconds without requests, messages or alarms, that keepAliveWhile holds a heartbeat only while the function runs, and that the disposer from keepAlive must always be called. Work that must finish should use a scheduled job or a Workflow. Completion check: each long task states which approach it uses. Stop condition: if a heartbeat is treated as protection against deploys or crashes, redesign.

Task example

This is a synthetic manual walkthrough; observations are expectations, not actual verification. Imagine a reminder Agent that sends a digest each morning and lets a user request a one-time reminder after 30 seconds. First confirm both callbacks are methods on the class. Register the recurring digest in onStart, where you would expect repeated starts to leave one row, and register the one-time reminder with a delay and a payload. Then call listSchedules; you would expect both kinds of entry. After cancelSchedule on one reminder and another listing, you would expect that entry to be gone. Finally, a long report job that must finish should not rely on keepAliveWhile alone. Every point still needs checking in a real environment.

Completion checks

  • Every callback name maps to a method on the Agent, and each trigger-time form matches its task requirement.
  • Recurring schedules are registered in onStart, show up once after repeated starts, and can be found with listSchedules or getScheduleById.
  • Lists exist from before and after cancellation, and long work that must finish does not depend only on keepAliveWhile.

Verification scope

This article is based on reading the supplied official documentation and relies only on its description of the scheduling methods, persistence and keep-alive behavior. Installation, login, API execution and output were not tested, and the observations in the example are not verified.

Sources

All links are official pages; verification dates are listed per source, and links point to the version checked.

  1. Cloudflare Agents · Cloudflare Agents documentation: cf-schedule

Found an error? Tell us →