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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! # task-supervisor
//!
//! [](https://crates.io/crates/task-supervisor)
//! [](https://docs.rs/task-supervisor)
//!
//! `task-supervisor` helps you keep Tokio tasks alive.
//! It watches each task, restarts it if it crashes or stops responding, and lets you add, restart, or kill tasks at runtime.
//!
//! ## Install
//!
//! ```bash
//! cargo add task-supervisor
//! ```
//!
//! ## Quick example
//!
//! ```rust,no_run
//! use task_supervisor::{SupervisorBuilder, SupervisedTask, TaskResult};
//!
//! #[derive(Clone)]
//! struct Printer;
//!
//! impl SupervisedTask for Printer {
//! async fn run(&mut self) -> TaskResult {
//! println!("hello");
//! Ok(())
//! }
//! }
//!
//! #[tokio::main]
//! async fn main() {
//! let supervisor = SupervisorBuilder::default()
//! .with_task("printer", Printer)
//! .build()
//! .run();
//!
//! supervisor.wait().await.unwrap(); // wait until every task finishes or is killed
//! }
//! ```
//!
//! ## What you get
//!
//! * **Automatic restarts** – failed tasks are relaunched with exponential back-off.
//! * **Dynamic control** – add, restart, kill, or query tasks at runtime through a [`SupervisorHandle`].
//! * **Configurable** – health-check interval, restart limits, back-off, dead-task threshold.
//!
//! ## Usage
//!
//! Build a supervisor with [`SupervisorBuilder`], call [`.build()`](SupervisorBuilder::build)
//! to get a [`Supervisor`], then [`.run()`](Supervisor::run) to start it. This returns a
//! [`SupervisorHandle`] you use to control things at runtime:
//!
//! | Method | Description |
//! | ------------------------------- | ---------------------------------------------------- |
//! | `wait().await` | Block until the supervisor exits |
//! | `add_task(name, task)` | Register and start a new task |
//! | `restart(name)` | Force-restart a task |
//! | `kill_task(name)` | Stop a task permanently |
//! | `get_task_status(name).await` | Get a task's [`TaskStatus`] |
//! | `get_all_task_statuses().await` | Get every task's status |
//! | `shutdown()` | Stop all tasks and exit |
//!
//! The handle auto-shuts down the supervisor when all clones are dropped.
//!
//! ## Clone and restart behaviour
//!
//! Every task must implement `Clone`. The supervisor stores the **original**
//! instance and clones it each time the task is started or restarted.
//! Mutations made through `&mut self` in [`SupervisedTask::run`] only affect
//! the running clone and are lost on restart.
//!
//! To share state across restarts, wrap it in an `Arc` (e.g. `Arc<AtomicUsize>`).
//! Plain owned fields will always start from their original value. See the
//! [`SupervisedTask`] docs for a full example.
//!
//! ## License
//!
//! [MIT](./LICENSE)
pub use ;
pub use ;
pub type TaskName = String;