ash_flare/
lib.rs

1//! Fault-tolerant supervision trees for Rust with distributed capabilities.
2//!
3//! Build resilient systems that automatically recover from failures using supervisor trees,
4//! restart strategies, and distributed supervision inspired by Erlang/OTP.
5//!
6//! # Features
7//!
8//! - **Supervision Trees**: Hierarchical supervision with nested supervisors and workers
9//! - **Restart Strategies**: `OneForOne`, `OneForAll`, and `RestForOne` strategies
10//! - **Restart Policies**: `Permanent`, `Temporary`, and `Transient` restart behaviors
11//! - **Restart Intensity**: Configurable restart limits with sliding time windows
12//! - **Distributed**: Run supervisors across processes or machines via TCP/Unix sockets
13//! - **Generic Workers**: Trait-based worker system for any async workload
14//! - **Dynamic Management**: Add/remove children at runtime
15//! - **Structured Logging**: Built-in support for `slog` structured logging
16//!
17//! # Quick Start
18//!
19//! ```rust
20//! use ash_flare::{SupervisorSpec, SupervisorHandle, RestartPolicy, Worker};
21//! use async_trait::async_trait;
22//!
23//! struct Counter {
24//!     id: u32,
25//!     max: u32,
26//! }
27//!
28//! #[async_trait]
29//! impl Worker for Counter {
30//!     type Error = std::io::Error;
31//!
32//!     async fn run(&mut self) -> Result<(), Self::Error> {
33//!         for i in 0..self.max {
34//!             tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
35//!         }
36//!         Ok(())
37//!     }
38//! }
39//!
40//! # #[tokio::main]
41//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
42//! // Build supervisor tree
43//! let spec = SupervisorSpec::new("root")
44//!     .with_worker("counter-1", || Counter { id: 1, max: 5 }, RestartPolicy::Permanent)
45//!     .with_worker("counter-2", || Counter { id: 2, max: 5 }, RestartPolicy::Permanent);
46//!
47//! // Start supervision tree
48//! let handle = SupervisorHandle::start(spec);
49//!
50//! // Query children
51//! let children = handle.which_children().await?;
52//!
53//! // Graceful shutdown
54//! handle.shutdown().await?;
55//! # Ok(())
56//! # }
57//! ```
58//!
59//! # Restart Strategies
60//!
61//! - **OneForOne**: Restarts only the failed child (default)
62//! - **OneForAll**: Restarts all children if any child fails
63//! - **RestForOne**: Restarts the failed child and all children started after it
64//!
65//! # Examples
66//!
67//! See the [examples directory](https://github.com/gntem/ash-flare/tree/master/examples) for more:
68//!
69//! - `counter.rs` - Basic supervisor with multiple workers
70//! - `distributed.rs` - Network-distributed supervisors
71//! - `super_tree.rs` - Complex nested supervision trees
72
73#![deny(missing_docs)]
74#![warn(clippy::all)]
75#![warn(rust_2018_idioms)]
76
77#[macro_use]
78mod macros;
79
80mod restart;
81mod supervisor;
82mod supervisor_common;
83mod types;
84mod worker;
85
86pub mod distributed;
87pub mod supervisor_stateful;
88
89// Re-export public API
90pub use restart::{RestartIntensity, RestartPolicy, RestartStrategy};
91pub use supervisor::{SupervisorError, SupervisorHandle, SupervisorSpec};
92pub use supervisor_stateful::{
93    StatefulSupervisorError, StatefulSupervisorHandle, StatefulSupervisorSpec,
94};
95pub use types::{ChildExitReason, ChildId, ChildInfo, ChildType, WorkerContext};
96pub use worker::{Worker, WorkerError};