workflow_egui/runtime/
signals.rs

1use crate::imports::*;
2
3pub struct Signals {
4    runtime: Runtime,
5    iterations: AtomicU64,
6}
7
8impl Signals {
9    pub fn bind(runtime: &Runtime) {
10        let signals = Arc::new(Signals {
11            runtime: runtime.clone(),
12            iterations: AtomicU64::new(0),
13        });
14
15        ctrlc::set_handler(move || {
16            let v = signals.iterations.fetch_add(1, Ordering::SeqCst);
17
18            match v {
19                0 => {
20                    // post a graceful exit event to the main event loop
21                    println!("^SIGTERM - shutting down...");
22                    signals
23                        .runtime
24                        .try_send_runtime_event(RuntimeEvent::Exit)
25                        .unwrap_or_else(|e| {
26                            println!("Error sending exit event: {:?}", e);
27                        });
28                }
29                1 => {
30                    // start runtime abort sequence
31                    // (attempt to gracefully shutdown kaspad if running)
32                    // this will execute process::exit(1) after 5 seconds
33                    println!("^SIGTERM - aborting...");
34                    Runtime::abort();
35                }
36                _ => {
37                    // exit the process immediately
38                    println!("^SIGTERM - halting");
39                    std::process::exit(1);
40                }
41            }
42        })
43        .expect("Error setting signal handler");
44    }
45}