rust_supervisor
A Rust library inspired by Erlang/OTP's supervision system, allowing automatic process restart when they fail.
Overview
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.
Features
-
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 it
-
Flexible configuration:
- Configurable maximum restart attempts
- Time window for counting restart attempts
- Process dependency management
-
Process monitoring:
- Automatic state tracking (Running, Failed, Restarting, Stopped)
- Process health monitoring
Installation
Add rust_supervisor
to your Cargo.toml
:
[]
= "0.1.0"
Usage
Simple Example
use ;
use thread;
use Duration;
Basic Example
use ;
use thread;
use Duration;
Dependency Example
use ;
use thread;
use Duration;
API Reference
SupervisorConfig
Configuration 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 processes
Supervisor
Main 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 process
RestartStrategy
Defines 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 it
ProcessState
Represents the current state of a process:
Running
: Process is runningFailed
: Process has failedRestarting
: Process is being restartedStopped
: Process is stopped (will not be restarted)
Contributing
Contributions via pull requests are welcome! Feel free to:
- Report bugs
- Suggest new features or enhancements
- Improve documentation
- Submit code improvements
Please ensure your code follows Rust best practices and includes appropriate tests.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
This library is inspired by Erlang/OTP's supervisor behavior, adapting the concept to Rust's threading model.