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