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
54
55
56
57
//! Core [`Task`] trait, [`TaskRef`] handle, and [`BoxTaskFuture`] alias.
use ;
use crateTaskError;
use crateTaskContext;
/// Boxed, pinned, `Send` future: the return type of [`Task::spawn`].
pub type BoxTaskFuture = ;
/// Shared task handle: `Arc<dyn Task>`.
pub type TaskRef = ;
/// Async unit of work managed by a [`Supervisor`](crate::Supervisor).
///
/// ## Contract
///
/// [`spawn`](Task::spawn) is called for each attempt.
/// It _must_ create a new future every time.
///
/// The same task may be started many times when a restart policy is used.
/// Shared state is allowed, but the returned future must not be reused.
///
/// ## Cancellation
///
/// Long-running tasks must listen for cancellation.
/// Use [`TaskContext::cancelled`] or [`TaskContext::is_cancelled`].
///
/// When a task stops because of cancellation, it should return `Err(TaskError::Canceled)`.
/// This lets the runtime report it as a graceful cancellation.
///
/// Short or one-shot tasks may ignore cancellation if they finish quickly.
/// Tasks that ignore cancellation during shutdown may be force-aborted after the supervisor grace period.
///
/// | Return value | Meaning | Restarted? |
/// |----------------------------|----------------------|----------------------------------------------------|
/// | `Ok(())` | Completed | Depends on [`RestartPolicy`](crate::RestartPolicy) |
/// | `Err(TaskError::Fail)` | Retryable failure | Per policy, with backoff |
/// | `Err(TaskError::Timeout)` | Attempt timed out* | Per policy, with backoff |
/// | `Err(TaskError::Canceled)` | Graceful cancel | Never |
/// | `Err(TaskError::Fatal)` | Permanent failure | Never |
///
/// # Also
///
/// - For the closure-based implementation see [`TaskFn`](crate::TaskFn).
/// - To configure restart, backoff, and timeout see [`TaskSpec`](crate::TaskSpec).