You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Tasks are defined by implementing the [`Task`] trait:
140
+
Tasks are defined by implementing the [`Task`] trait. The `State` type parameter allows passing application state (e.g., HTTP clients, database pools) to your tasks. Use `()` if you don't need state.
Return user errors with structured data using `TaskError::user()`:
@@ -169,14 +214,15 @@ The error data is serialized to JSON and stored in the database for debugging an
169
214
170
215
The [`TaskContext`] provides methods for durable execution:
171
216
172
-
-**`step(name, params, closure)`** - Execute a checkpointed operation. The closure receives `(params, state)`. If the step completed in a previous run with the same name and params, returns the cached result.
217
+
-**`step(name, params, f)`** - Execute a checkpointed operation. The `f` is a `fn(P, StepState<State>) -> Future` (function pointer, not a closure that captures). If the step completed in a previous run with the same name and params hash, returns the cached result.
173
218
-**`spawn::<T>(name, params, options)`** - Spawn a subtask and return a handle.
174
219
-**`spawn_by_name(name, task_name, params, options)`** - Spawn a subtask by task name (dynamic version).
175
220
-**`join(handle)`** - Wait for a subtask to complete and get its result.
176
221
-**`sleep_for(name, duration)`** - Suspend the task for a duration.
177
222
-**`await_event(name, timeout)`** - Wait for an external event.
178
223
-**`emit_event(name, payload)`** - Emit an event to wake waiting tasks.
179
-
-**`heartbeat(duration)`** - Extend the task lease for long operations.
224
+
-**`heartbeat(duration)`** - Extend the task lease for long operations. Takes `Option<Duration>`.
225
+
-**`heartbeat_handle()`** - Get a cloneable `HeartbeatHandle` for use inside step closures.
180
226
-**`rand()`** - Generate a durable random value in [0, 1). Checkpointed.
181
227
-**`now()`** - Get the current time as a durable checkpoint.
182
228
-**`uuid7()`** - Generate a durable UUIDv7. Checkpointed.
@@ -186,12 +232,14 @@ The [`TaskContext`] provides methods for durable execution:
186
232
Steps provide "at-least-once" execution. To achieve "exactly-once" semantics for side effects, use the `task_id` as an idempotency key:
The step closure receives `(params, StepState<State>)`. Since `step` takes a function pointer (`fn`), the closure cannot capture variables from the surrounding scope — pass any needed data through the `params` argument.
242
+
195
243
### Events
196
244
197
245
Tasks can wait for and emit events:
@@ -216,7 +264,7 @@ client.emit_event(
216
264
Tasks can spawn subtasks and wait for their results using `spawn()` and `join()`:
When a task is cancelled, all its subtasks are automatically cancelled as well.
416
+
359
417
### Transactional Spawning
360
418
361
419
You can atomically enqueue a task as part of a larger database transaction. This ensures that either both your write and the task spawn succeed, or neither does:
@@ -382,6 +440,7 @@ This is useful when you need to guarantee that a task is only enqueued if relate
382
440
-`spawn_with(executor, params)` - Spawn with default options
383
441
-`spawn_with_options_with(executor, params, options)` - Spawn with custom options
384
442
-`spawn_by_name_with(executor, task_name, params, options)` - Dynamic spawn by name
0 commit comments