pub fn boom() -> ChunkExpand description
Immediate Trap — used to show crate::Supervisor restart.
Examples found in repository?
examples/crash_and_restart.rs (line 17)
15fn main() {
16 let rt = match Runtime::with_config(
17 samples::boom(),
18 RuntimeConfig {
19 workers: 1,
20 quantum: 1_000,
21 mailbox: byteflow::MailboxConfig::DEFAULT,
22 ..Default::default()
23 },
24 ) {
25 Ok(rt) => rt,
26 Err(e) => {
27 eprintln!("runtime: {e}");
28 std::process::exit(1);
29 }
30 };
31 let Some(boom) = rt.function_index("boom") else {
32 eprintln!("missing boom");
33 std::process::exit(1);
34 };
35 let sup = match Supervisor::with_config(
36 rt.spawner(),
37 SupervisorConfig {
38 max_restarts: 2,
39 max_period: Duration::from_secs(5),
40 strategy: byteflow::RestartStrategy::OneForOne,
41 },
42 ) {
43 Ok(s) => s,
44 Err(e) => {
45 eprintln!("supervisor: {e}");
46 std::process::exit(1);
47 }
48 };
49 if let Err(e) = sup.start_child(ChildSpec::new("boom", boom).restart(RestartPolicy::OnFailure))
50 {
51 eprintln!("start_child: {e}");
52 std::process::exit(1);
53 }
54
55 let start = std::time::Instant::now();
56 while !sup.intensity_exceeded() {
57 if start.elapsed() > Duration::from_secs(2) {
58 eprintln!("supervisor did not hit intensity in time");
59 std::process::exit(1);
60 }
61 thread::sleep(Duration::from_millis(5));
62 }
63
64 let metrics = rt.metrics();
65 println!("intensity exceeded after {} failures", metrics.processes_failed);
66 println!("{metrics}");
67 sup.shutdown();
68 rt.shutdown();
69}