task_supervisor/lib.rs
1//! # 🤖 task-supervisor
2//!
3//! [](https://crates.io/crates/task-supervisor)
4//! [](https://docs.rs/task-supervisor)
5//!
6//! `task-supervisor` helps you keep Tokio tasks alive.
7//! It watches each task, restarts it if it crashes or stops responding, and lets you add, restart, or kill tasks at runtime.
8//!
9//! ## Install
10//!
11//! ```bash
12//! cargo add task-supervisor
13//! ```
14//!
15//! ## Quick example
16//!
17//! ```rust,no_run
18//! use async_trait::async_trait;
19//! use task_supervisor::{SupervisorBuilder, SupervisedTask, TaskResult};
20//!
21//! #[derive(Clone)]
22//! struct Printer;
23//!
24//! #[async_trait]
25//! impl SupervisedTask for Printer {
26//! async fn run(&mut self) -> TaskResult {
27//! println!("hello");
28//! Ok(())
29//! }
30//! }
31//!
32//! #[tokio::main]
33//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
34//! let supervisor = SupervisorBuilder::default()
35//! .with_task("printer", Printer)
36//! .build()
37//! .run();
38//!
39//! supervisor.wait().await?; // wait until every task finishes or is killed
40//! Ok(())
41//! }
42//! ```
43//!
44//! ## What you get
45//!
46//! * **Automatic restarts** – tasks are relaunched after a crash or hang (exponential back-off).
47//! * **Dynamic control** – add, restart, kill, or query tasks through a `SupervisorHandle`.
48//! * **Configurable** – tune health-check interval, restart limits, back-off delay, and shutdown policy.
49//!
50//! ## API overview
51//!
52//! | TaskHandle method | Purpose |
53//! | ------------------------------- | ----------------------------------------------------------------- |
54//! | `add_task(name, task)` | Start a new task while running |
55//! | `restart(name)` | Force a restart |
56//! | `kill_task(name)` | Stop a task permanently |
57//! | `get_task_status(name).await` | Return `TaskStatus` (`Healthy`, `Failed`, `Completed`, `Dead`, …) |
58//! | `get_all_task_statuses().await` | Map of all task states |
59//! | `shutdown()` | Stop every task and exit |
60//!
61//! Full documentation lives on docs.rs.
62//!
63//! ## License
64//!
65//! [MIT](./LICENSE)
66
67pub use supervisor::{
68 builder::SupervisorBuilder,
69 handle::{SupervisorHandle, SupervisorHandleError},
70 Supervisor, SupervisorError,
71};
72pub use task::{SupervisedTask, TaskError, TaskResult, TaskStatus};
73
74mod supervisor;
75mod task;
76
77pub type TaskName = String;