A Task job is a user-controlled durable state machine. Unlike Continuous jobs (which run on a fixed schedule) or Debounce jobs (which flush after a delay), Task jobs give you complete control: you decide when to schedule execution, how to handle each event, and when the job is done. Each Task instance runs in its own Durable Object. Events update state and optionally schedule an alarm. When the alarm fires,Documentation Index
Fetch the complete documentation index at: https://mintlify.com/backpine/durable-effect/llms.txt
Use this file to discover all available pages before exploring further.
execute runs. You control what happens next — reschedule, terminate, or wait for another event.
Use Task jobs for multi-step workflows: order processing, document review pipelines, or any pattern where the next step depends on external events.
Defining a task job
Configuration
Effect Schema for validating and serializing the task’s persistent state. State is
null until first set via ctx.setState.Effect Schema for validating incoming events. Each event sent via
client.task(...).send() is validated against this schema before being passed to onEvent.Handler called for each incoming event. The event is passed as the first argument (not on
ctx) — it is a direct value, not an Effect. Update state and optionally schedule execution.Handler called when the scheduled alarm fires. Process state and schedule the next execution or call
ctx.terminate() when done.Optional handler called after
onEvent or execute completes with no alarm scheduled. Use it to schedule delayed cleanup or trigger maintenance.Optional error handler for
onEvent and execute failures. If not provided, errors are logged and the task continues. Use to track errors in state and schedule retries.Control logging verbosity.
false (default) logs only errors. true enables debug-level logging.onEvent context
The event value is passed as the first argument to onEvent, separate from ctx. This makes it clear the event is a plain value, not an Effect.
| Property | Type | Description |
|---|---|---|
state | Effect<S | null> | Current state. null if no state has been set yet. |
setState(s) | Effect<void> | Replace the entire state. |
updateState(fn) | Effect<void> | Transform state. No-op if state is null. |
schedule(when) | Effect<void> | Schedule the next execution. |
cancelSchedule() | Effect<void> | Cancel any scheduled execution. |
getScheduledTime() | Effect<number | null> | Get the currently scheduled time (ms), or null if none. |
terminate() | Effect<never> | Cancel alarms and delete all state. Short-circuits execution. |
instanceId | string | Unique Durable Object instance ID. |
jobName | string | Job name as registered. |
isFirstEvent | boolean | true if this is the first event (state was null). |
eventCount | Effect<number> | Total events received. |
createdAt | Effect<number> | When this task instance was created (ms). |
Execute context
| Property | Type | Description |
|---|---|---|
state | Effect<S | null> | Current state. |
setState(s) | Effect<void> | Replace the entire state. |
updateState(fn) | Effect<void> | Transform state. |
schedule(when) | Effect<void> | Schedule the next execution. |
cancelSchedule() | Effect<void> | Cancel any scheduled execution. |
getScheduledTime() | Effect<number | null> | Get the currently scheduled time (ms). |
terminate() | Effect<never> | Cancel alarms and delete all state. Short-circuits execution. |
instanceId | string | Unique Durable Object instance ID. |
jobName | string | Job name as registered. |
executeCount | Effect<number> | Number of times execute has been called (1-indexed). |
eventCount | Effect<number> | Total events received. |
createdAt | Effect<number> | When this task instance was created (ms). |
Scheduling execution
ctx.schedule(when) accepts flexible time inputs:
Canceling and inspecting a schedule
Terminating a task
ctx.terminate() cancels any scheduled alarm, deletes all state from storage, and short-circuits the current handler. The instance ID can be reused to start a new task from scratch.
