Skip to content

Commit

Permalink
Restart when receiving the HUP signal
Browse files Browse the repository at this point in the history
Until we have a proper support for config-file (and certificate)
reload, this will handle a graceful restart.

Resolves #150 for now.
  • Loading branch information
robklg committed Mar 29, 2023
1 parent a73c542 commit d1fda84
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ where
Ok(())
}

#[derive(PartialEq)]
struct ExitSignal(pub &'static str);

async fn listen_for_signals() -> Result<ExitSignal, String> {
Expand All @@ -684,6 +685,8 @@ async fn listen_for_signals() -> Result<ExitSignal, String> {
.map_err(|e| format!("could not listen for TERM signals: {}", e))?;
let mut int_sig = signal(SignalKind::interrupt())
.map_err(|e| format!("Could not listen for INT signal: {}", e))?;
let mut hup_sig = signal(SignalKind::hangup())
.map_err(|e| format!("Could not listen for INT signal: {}", e))?;

let sig_name = tokio::select! {
Some(_signal) = term_sig.recv() => {
Expand All @@ -692,6 +695,9 @@ async fn listen_for_signals() -> Result<ExitSignal, String> {
Some(_signal) = int_sig.recv() => {
"SIG_INT"
},
Some(_signal) = hup_sig.recv() => {
"SIG_HUP"
},
};
Ok(ExitSignal(sig_name))
}
Expand Down Expand Up @@ -778,7 +784,11 @@ fn run(arg_matches: ArgMatches) -> Result<(), String> {
);

let runtime = Runtime::new().map_err(|e| format!("could not construct runtime: {}", e))?;
let _ = runtime.block_on(main_task(arg_matches, &log, &root_logger))?;
while runtime.block_on(main_task(arg_matches.clone(), &log, &root_logger))?
== ExitSignal("SIG_HUP")
{
info!(log, "Received SIG_HUP, restarting");
}
info!(log, "Exiting...");
Ok(())
}
Expand Down

0 comments on commit d1fda84

Please sign in to comment.