pub trait Task:
Send
+ Sync
+ 'static {
// Required methods
fn name(&self) -> &str;
fn spawn(&self, ctx: CancellationToken) -> BoxTaskFuture;
}Expand description
Async, cancelable unit of work managed by a Supervisor.
§Contract
spawn is called once per attempt and must return a fresh, independent future.
Implementations must observe ctx.cancelled() and return Err(TaskError::Canceled) promptly.
Non-cooperative tasks will be force-terminated after the grace period (GraceExceeded).
| Return value | Meaning | Restarted? |
|---|---|---|
Ok(()) | Task completed normally | Depends on RestartPolicy |
Err(TaskError::Canceled) | Cooperative shutdown | Never |
Err(TaskError::Fail) | Transient failure | Per policy, with backoff |
Err(TaskError::Timeout) | Attempt timed out | Per policy, with backoff |
Err(TaskError::Fatal) | Permanent failure | Never |
§Cancellation
The CancellationToken ctx is cancelled by the supervisor during shutdown or when the task is removed at runtime.
- Long-running tasks should poll
ctx.cancelled(): tasks that ignore the token will block graceful shutdown until the grace period expires. - Short-lived, one-shot tasks that complete quickly may omit this check.
A task that loops forever and only exits via ctx.cancelled() should return Err(TaskError::Canceled);
A one-shot task that finishes its work should return Ok(()).
§Also
Required Methods§
Sourcefn spawn(&self, ctx: CancellationToken) -> BoxTaskFuture
fn spawn(&self, ctx: CancellationToken) -> BoxTaskFuture
Creates a new future that runs the task until completion or cancellation.
Called once per attempt. Takes &self - each call must return an
independent future with no side effects from previous runs.