Crate task_supervisor

Crate task_supervisor 

Source
Expand description

§🤖 task-supervisor

Crates.io Docs.rs

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

cargo add task-supervisor

§Quick example

use async_trait::async_trait;
use task_supervisor::{SupervisorBuilder, SupervisedTask, TaskResult};

#[derive(Clone)]
struct Printer;

#[async_trait]
impl SupervisedTask for Printer {
    async fn run(&mut self) -> TaskResult {
        println!("hello");
        Ok(())
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let supervisor = SupervisorBuilder::default()
        .with_task("printer", Printer)
        .build()
        .run();

    supervisor.wait().await?;   // wait until every task finishes or is killed
    Ok(())
}

§What you get

  • Automatic restarts – tasks are relaunched after a crash or hang (exponential back-off).
  • Dynamic control – add, restart, kill, or query tasks through a SupervisorHandle.
  • Configurable – tune health-check interval, restart limits, back-off delay, and shutdown policy.

§API overview

TaskHandle methodPurpose
add_task(name, task)Start a new task while running
restart(name)Force a restart
kill_task(name)Stop a task permanently
get_task_status(name).awaitReturn TaskStatus (Healthy, Failed, Completed, Dead, …)
get_all_task_statuses().awaitMap of all task states
shutdown()Stop every task and exit

Full documentation lives on docs.rs.

§License

MIT

Structs§

Supervisor
Manages a set of tasks, ensuring they remain operational through restarts.
SupervisorBuilder
Builds a Supervisor instance with configurable parameters.
SupervisorHandle
Handle used to interact with the Supervisor.

Enums§

SupervisorError
SupervisorHandleError
TaskStatus
Represents the current state of a supervised task.

Traits§

SupervisedTask

Type Aliases§

TaskError
TaskName
TaskResult