Skip to content

Commit

Permalink
feat: shut down gracefully with ctrl+c (#1364)
Browse files Browse the repository at this point in the history
If you have a long-running process, like `webdav` or `backup` or others,
pressing CTRL+C usually results in a non-zero exit code:

`process didn't exit successfully:
P:\CARGO\.cargo-target-win\debug\rustic.exe -P <PROFILE> webdav (exit
code: 0xc000013a, STATUS_CONTROL_C_EXIT)`

This shouldn't be the case, as it was user initiated and we can shut
down gracefully.

This PR adds this functionality, so CTRL+C shuts down `rustic`
gracefully and exits with a 0 exit-code.

---------

Signed-off-by: simonsan <[email protected]>
Co-authored-by: aawsome <[email protected]>
  • Loading branch information
simonsan and aawsome authored Nov 30, 2024
1 parent c571279 commit 852635e
Show file tree
Hide file tree
Showing 3 changed files with 28 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 @@ -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

0 comments on commit 852635e

Please sign in to comment.