From 3a55b0447644b457f346d254ebe281190d0319a8 Mon Sep 17 00:00:00 2001 From: Robby klein Gunnewiek Date: Fri, 7 Apr 2023 08:53:16 +0200 Subject: [PATCH] Restart when receiving the HUP signal (#151) Until we have proper support for config-file (and certificate) reload, this will handle a graceful restart. Resolves #150 for now. --- src/main.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index ba1f6e4..1906012 100644 --- a/src/main.rs +++ b/src/main.rs @@ -673,6 +673,7 @@ where Ok(()) } +#[derive(PartialEq)] struct ExitSignal(pub &'static str); async fn listen_for_signals() -> Result { @@ -684,6 +685,8 @@ async fn listen_for_signals() -> Result { .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 HUP signal: {}", e))?; let sig_name = tokio::select! { Some(_signal) = term_sig.recv() => { @@ -692,6 +695,9 @@ async fn listen_for_signals() -> Result { Some(_signal) = int_sig.recv() => { "SIG_INT" }, + Some(_signal) = hup_sig.recv() => { + "SIG_HUP" + }, }; Ok(ExitSignal(sig_name)) } @@ -778,7 +784,13 @@ 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))?; + // We wait for a signal (HUP, INT, TERM). If the signal is a HUP, + // we restart, otherwise we exit the loop and the program ends. + 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(()) }