Skip to main content

boom

Function boom 

Source
pub fn boom() -> Chunk
Expand 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        },
41    ) {
42        Ok(s) => s,
43        Err(e) => {
44            eprintln!("supervisor: {e}");
45            std::process::exit(1);
46        }
47    };
48    if let Err(e) = sup.start_child(ChildSpec::new("boom", boom).restart(RestartPolicy::OnFailure))
49    {
50        eprintln!("start_child: {e}");
51        std::process::exit(1);
52    }
53
54    let start = std::time::Instant::now();
55    while !sup.intensity_exceeded() {
56        if start.elapsed() > Duration::from_secs(2) {
57            eprintln!("supervisor did not hit intensity in time");
58            std::process::exit(1);
59        }
60        thread::sleep(Duration::from_millis(5));
61    }
62
63    let metrics = rt.metrics();
64    println!("intensity exceeded after {} failures", metrics.processes_failed);
65    println!("{metrics}");
66    sup.shutdown();
67    rt.shutdown();
68}