summaryrefslogtreecommitdiffstats
path: root/src/bin
diff options
context:
space:
mode:
authorClement Tsang <34804052+ClementTsang@users.noreply.github.com>2020-08-21 13:00:14 -0700
committerGitHub <noreply@github.com>2020-08-21 16:00:14 -0400
commit7475f24a4ebc8c40ba9418684cae08667e1a4a79 (patch)
treed0575985cbf869f09f88835d2e7b8cb3b4d2d901 /src/bin
parenta1766961fe4eb866d013db2c89de0b8ac0a2313a (diff)
feature: Add hook to properly clean up in the case of a kill call
Adds a hook to properly clean up the program in case the program gets SIGTERM'd.
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/main.rs16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/bin/main.rs b/src/bin/main.rs
index fdbe7b51..c443bb1f 100644
--- a/src/bin/main.rs
+++ b/src/bin/main.rs
@@ -9,7 +9,10 @@ use std::{
boxed::Box,
io::{stdout, Write},
panic,
- sync::mpsc,
+ sync::{
+ atomic::{AtomicBool, Ordering},
+ mpsc, Arc,
+ },
thread,
time::Duration,
};
@@ -89,7 +92,16 @@ fn main() -> error::Result<()> {
// Set panic hook
panic::set_hook(Box::new(|info| panic_hook(info)));
- loop {
+ // Set termination hook
+ let is_terminated = Arc::new(AtomicBool::new(false));
+ let ist_clone = is_terminated.clone();
+ ctrlc::set_handler(move || {
+ ist_clone.store(true, Ordering::SeqCst);
+ termination_hook();
+ })
+ .unwrap();
+
+ while !is_terminated.load(Ordering::SeqCst) {
if let Ok(recv) = receiver.recv_timeout(Duration::from_millis(TICK_RATE_IN_MILLISECONDS)) {
match recv {
BottomEvent::KeyInput(event) => {