1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Core [`Task`] trait, [`TaskRef`] handle, and [`BoxTaskFuture`] alias.
use ;
use CancellationToken;
use crateTaskError;
/// Boxed, pinned, `Send` future: the return type of [`Task::spawn`].
pub type BoxTaskFuture = ;
/// Shared handle to a task: `Arc<dyn Task>`.
pub type TaskRef = ;
/// Async, cancelable unit of work managed by a [`Supervisor`](crate::Supervisor).
///
/// ## Contract
///
/// [`spawn`](Task::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`](crate::RuntimeError::GraceExceeded)).
///
/// | Return value | Meaning | Restarted? |
/// |----------------------------|-------------------------|----------------------------------------------------|
/// | `Ok(())` | Task completed normally | Depends on [`RestartPolicy`](crate::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
///
/// - For the closure-based implementation see [`TaskFn`](crate::TaskFn).
/// - To configure restart, backoff, and timeout see [`TaskSpec`](crate::TaskSpec).