use std::thread;
use std::time::Duration;
use rust_supervisor::{Supervisor, SupervisorConfig, ChildType};
fn main() {
println!("Starting supervision system...");
let mut supervisor = Supervisor::new(SupervisorConfig::default());
supervisor.add_process("unstable_process", ChildType::Permanent, || {
thread::spawn(|| {
println!("Unstable process started");
let duration = Duration::from_secs(2);
thread::sleep(duration);
println!("Unstable process failing!");
panic!("Simulated error in unstable process");
})
});
supervisor.add_process("stable_process", ChildType::Permanent, || {
thread::spawn(|| {
println!("Stable process started");
let mut counter = 0;
loop {
thread::sleep(Duration::from_secs(1));
counter += 1;
println!("Stable process running (iteration {})", counter);
}
})
});
supervisor.add_dependency("stable_process", "unstable_process");
let supervisor = supervisor.start_monitoring();
println!("Supervision started. Observing activity for 20 seconds...");
for i in 1..=20 {
thread::sleep(Duration::from_secs(1));
if i % 5 == 0 {
if let Some(state) = supervisor.get_process_state("unstable_process") {
println!("Unstable process state after {} seconds: {:?}", i, state);
}
if let Some(state) = supervisor.get_process_state("stable_process") {
println!("Stable process state after {} seconds: {:?}", i, state);
}
}
}
println!("Demo ended");
supervisor.shutdown();
}