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