Skip to main content

task_supervisor/
lib.rs

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