Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: shut down gracefully with ctrl+c #1364

Merged
merged 8 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -103,6 +103,7 @@ clap = { version = "4", features = ["derive", "env", "wrap_help"] }
clap_complete = "4"
conflate = "0.3.3"
convert_case = "0.6.0"
ctrlc = { version = "3.4.5", features = ["termination"] }
dateparser = "0.2.1"
derive_more = { version = "1", features = ["debug"] }
dialoguer = "0.11.0"
Expand Down
17 changes: 16 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 @@ -64,7 +65,7 @@ use clap::builder::{
};
use convert_case::{Case, Casing};
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 +180,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