| Crates.io | rust_supervisor |
| lib.rs | rust_supervisor |
| version | 0.1.0 |
| created_at | 2025-03-12 05:28:55.12825+00 |
| updated_at | 2025-03-12 05:28:55.12825+00 |
| description | An Erlang-inspired process supervision library for Rust |
| homepage | |
| repository | https://github.com/roquess/rust_supervisor |
| max_upload_size | |
| id | 1589399 |
| size | 42,232 |
A Rust library inspired by Erlang/OTP's supervision system, allowing automatic process restart when they fail.
rust_supervisor brings the robust process supervision model from Erlang/OTP to the Rust ecosystem. It allows you to define processes (threads), monitor their health, and automatically restart them according to configurable strategies when they fail.
Multiple restart strategies:
OneForOne: Restart only the failed processOneForAll: Restart all processes when one failsRestForOne: Restart the failed process and all processes that depend on itFlexible configuration:
Process monitoring:
Add rust_supervisor to your Cargo.toml:
[dependencies]
rust_supervisor = "0.1.0"
use rust_supervisor::{Supervisor, SupervisorConfig};
use std::thread;
use std::time::Duration;
fn main() {
// Create a supervisor with default configuration
let mut supervisor = Supervisor::new(SupervisorConfig::default());
// Add a simple worker process that prints a message every second
supervisor.add_process("simple_worker", || {
thread::spawn(|| {
loop {
println!("Simple worker is running...");
// Simulate work
thread::sleep(Duration::from_secs(1));
// Uncomment to simulate random failures
// if rand::random::<f32>() < 0.1 {
// panic!("Worker failed unexpectedly!");
// }
}
})
});
// Start the supervision
supervisor.start_monitoring();
println!("Supervisor started. Press Ctrl+C to exit.");
// Keep the main thread alive
loop {
thread::sleep(Duration::from_secs(10));
// Check and print the state of our worker
if let Some(state) = supervisor.get_process_state("simple_worker") {
println!("Worker state: {:?}", state);
}
}
}
use rust_supervisor::{Supervisor, SupervisorConfig};
use std::thread;
use std::time::Duration;
fn main() {
// Create a supervisor with default configuration
let mut supervisor = Supervisor::new(SupervisorConfig::default());
// Add a process to supervise
supervisor.add_process("worker1", || {
thread::spawn(|| {
// Worker code that might fail
loop {
println!("Worker 1 running...");
thread::sleep(Duration::from_secs(1));
// Simulating a potential failure
if rand::random::<f32>() < 0.01 {
panic!("Worker 1 crashed!");
}
}
})
});
// Start monitoring the processes
supervisor.start_monitoring();
// Keep the main thread alive
loop {
thread::sleep(Duration::from_secs(1));
}
}
use rust_supervisor::{Supervisor, SupervisorConfig, RestartStrategy};
use std::thread;
use std::time::Duration;
fn main() {
// Create a supervisor with RestForOne strategy
let mut config = SupervisorConfig::default();
config.restart_strategy = RestartStrategy::RestForOne;
let mut supervisor = Supervisor::new(config);
// Add processes
supervisor.add_process("database", || {
thread::spawn(|| {
// Database connection code...
})
});
supervisor.add_process("worker", || {
thread::spawn(|| {
// Worker that depends on database...
})
});
// Define the dependency
supervisor.add_dependency("worker", "database");
// Start monitoring
supervisor.start_monitoring();
}
SupervisorConfigConfiguration for the supervisor behavior:
max_restarts: Maximum number of restarts allowedmax_time: Time period over which to count restartsrestart_strategy: Strategy to use when restarting processesSupervisorMain supervisor structure:
new(config): Create a new supervisoradd_process(name, factory): Add a process to monitoradd_dependency(process, depends_on): Declare a dependency between processesstart_monitoring(): Start monitoring processesstop_process(name): Manually stop a processget_process_state(name): Get the current state of a processRestartStrategyDefines the strategy to use when a process fails:
OneForOne: Restart only the failed processOneForAll: Restart all processes when one failsRestForOne: Restart the failed process and all processes that depend on itProcessStateRepresents the current state of a process:
Running: Process is runningFailed: Process has failedRestarting: Process is being restartedStopped: Process is stopped (will not be restarted)Contributions via pull requests are welcome! Feel free to:
Please ensure your code follows Rust best practices and includes appropriate tests.
This project is licensed under the MIT License - see the LICENSE file for details.
This library is inspired by Erlang/OTP's supervisor behavior, adapting the concept to Rust's threading model.