Skip to content

Commit

Permalink
feat: shut down gracefully with ctrl+c
Browse files Browse the repository at this point in the history
Signed-off-by: simonsan <[email protected]>
  • Loading branch information
simonsan committed Nov 23, 2024
1 parent d79e392 commit 0c9f412
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ open = "5.3.0"
self_update = { version = "0.39.0", default-features = false, optional = true, features = ["rustls", "archive-tar", "compression-flate2"] } # FIXME: Downgraded to 0.39.0 due to https://github.com/jaemk/self_update/issues/136
tar = "0.4.42"
toml = "0.8"
ctrlc = { version = "3.4.5", features = ["termination"] }

[dev-dependencies]
abscissa_core = { version = "0.8.1", default-features = false, features = ["testing"] }
Expand Down
18 changes: 17 additions & 1 deletion src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use std::fmt::Debug;
use std::fs::File;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::mpsc::channel;

#[cfg(feature = "mount")]
use crate::commands::mount::MountCmd;
Expand Down Expand Up @@ -63,8 +64,9 @@ use clap::builder::{
Styles,
};
use convert_case::{Case, Casing};
use ctrlc;
use human_panic::setup_panic;
use log::{log, Level};
use log::{info, log, Level};
use simplelog::{CombinedLogger, LevelFilter, TermLogger, TerminalMode, WriteLogger};

use self::find::FindCmd;
Expand Down Expand Up @@ -179,6 +181,20 @@ impl Runnable for EntryPoint {
// Set up panic hook for better error messages and logs
setup_panic!();

// Set up Ctrl-C handler
let (tx, rx) = channel();

ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel."))
.expect("Error setting Ctrl-C handler");

_ = std::thread::spawn(move || {
// Wait for Ctrl-C
rx.recv().expect("Could not receive from channel.");
info!("Ctrl-C received, shutting down...");
RUSTIC_APP.shutdown(Shutdown::Graceful)
});

// Run the subcommand
self.commands.run();
RUSTIC_APP.shutdown(Shutdown::Graceful)
}
Expand Down

0 comments on commit 0c9f412

Please sign in to comment.