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
58
59
//! # Long-running worker with graceful shutdown
//!
//! Use this pattern for a resident loop that should stop with the supervisor.
//!
//! ```text
//! Ctrl+C ──► runtime shutdown ──► TaskContext cancellation
//! ▼
//! drop-safe sleep ends ──► Canceled ──► stop
//! ```
//!
//! `run_until_cancelled` may wrap only a future that is safe to stop by dropping.
//! Tokio's sleep is safe for this pattern. Cancellation drops the sleep, returns
//! `TaskError::Canceled`, and gives the task a branch in which to release resources.
//!
//! Taskvisor requests a cooperative stop first.
//! After the configured grace period, it may commit `ForceAborted`.
//! Synchronous task code can remain physically active until it returns control to Tokio.
//!
//! `TaskSpec::restartable` retries retryable failures.
//! A clean return or `TaskError::Canceled` stops the worker.
//! This example stops cooperatively when Ctrl+C requests shutdown.
//!
//! `run_with_os_signals` installs process-wide signal handlers.
//! Use `run_until` when the surrounding application owns signal handling.
//!
//! Run with `cargo run --example graceful_worker`, then press Ctrl+C.
use Duration;
use *;
async