Expand description
§MAD - Lifecycle Manager for Rust Applications
MAD is a robust lifecycle manager designed for long-running Rust operations. It provides a simple, composable way to manage multiple concurrent services within your application, handling graceful startup and shutdown automatically.
§Overview
MAD helps you build applications composed of multiple long-running components that need to be orchestrated together. It handles:
- Concurrent execution of multiple components
- Graceful shutdown with cancellation tokens
- Error aggregation from multiple components
- Lifecycle management with setup, run, and close phases
§Quick Start
use notmad::{Component, Mad};
use async_trait::async_trait;
use tokio_util::sync::CancellationToken;
struct MyService {
name: String,
}
#[async_trait]
impl Component for MyService {
fn name(&self) -> Option<String> {
Some(self.name.clone())
}
async fn run(&self, cancellation: CancellationToken) -> Result<(), notmad::MadError> {
// Your service logic here
while !cancellation.is_cancelled() {
// Do work...
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
Ok(())
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
Mad::builder()
.add(MyService { name: "service-1".into() })
.add(MyService { name: "service-2".into() })
.run()
.await?;
Ok(())
}§Component Lifecycle
Components go through three phases:
- Setup: Optional initialization phase before components start running
- Run: Main execution phase where components perform their work
- Close: Optional cleanup phase after components stop
§Error Handling
MAD provides comprehensive error handling through MadError, which can:
- Wrap errors from individual components
- Aggregate multiple errors when several components fail
- Automatically convert from
anyhow::Error
§Shutdown Behavior
MAD handles shutdown gracefully:
- Responds to SIGTERM and Ctrl+C signals
- Propagates cancellation tokens to all components
- Waits for components to finish cleanup
- Configurable cancellation timeout
Structs§
- Aggregate
Error - Container for multiple errors from different components.
- Mad
- The main lifecycle manager for running multiple components.
Enums§
- MadError
- Error type for MAD operations.
Traits§
- Component
- Trait for implementing MAD components.
- Into
Component - Trait for converting types into components.