-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraceful_shutdown.rs
49 lines (43 loc) · 1.41 KB
/
graceful_shutdown.rs
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
46
47
48
49
use libc::c_int;
use signal_hook::{
consts::signal::{SIGINT, SIGQUIT, SIGTERM},
iterator::Signals,
low_level,
};
use std::sync::{Arc, Mutex};
use std::thread;
const SIGNALS: &[c_int] = &[SIGINT, SIGQUIT, SIGTERM];
fn start_force_shutdown_listener() {
let thread_name = "fn: start_force_shutdown_listener".to_string();
// Do not join
let _ = thread::Builder::new()
.name(thread_name)
.spawn(move || {
for signal in &mut Signals::new(SIGNALS).unwrap() {
println!("Force stopping...");
low_level::emulate_default_handler(signal).unwrap();
}
})
.unwrap();
}
pub fn start_graceful_shutdown_listener() -> Arc<Mutex<bool>> {
let graceful_shutdown = Arc::new(Mutex::new(false));
let graceful_shutdown_2 = Arc::clone(&graceful_shutdown);
let thread_name = "fn: start_graceful_shutdown_listener".to_string();
// Do not join
let _ = thread::Builder::new()
.name(thread_name)
.spawn(move || {
if (&mut Signals::new(SIGNALS).unwrap())
.into_iter()
.next()
.is_some()
{
println!("Gracefully stopping... (press Ctrl+C again to force)");
start_force_shutdown_listener();
*graceful_shutdown_2.lock().unwrap() = true;
}
})
.unwrap();
graceful_shutdown
}