diff --git a/src/net/mod.rs b/src/net/mod.rs index ed6ab3ce..d895718d 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -11,6 +11,7 @@ mod output; mod params; mod rpc; mod session; +mod signals; mod signin; mod signup; mod sql; @@ -74,21 +75,29 @@ pub async fn init() -> Result<(), Error> { .cert_path(c) .key_path(k) .bind_with_graceful_shutdown(opt.bind, async move { - tokio::signal::ctrl_c().await.expect("Failed to listen to shutdown signal"); + // Capture the shutdown signals and log that the graceful shutdown has started + let result = signals::listen().await.expect("Failed to listen to shutdown signal"); + info!(target: LOG, "{} received. Start graceful shutdown...", result); }); // Log the server startup status info!(target: LOG, "Started web server on {}", &adr); // Run the server forever - srv.await + srv.await; + // Log the server shutdown event + info!(target: LOG, "Shutdown complete. Bye!") } else { // Bind the server to the desired port let (adr, srv) = warp::serve(net).bind_with_graceful_shutdown(opt.bind, async move { - tokio::signal::ctrl_c().await.expect("Failed to listen to shutdown signal"); + // Capture the shutdown signals and log that the graceful shutdown has started + let result = signals::listen().await.expect("Failed to listen to shutdown signal"); + info!(target: LOG, "{} received. Start graceful shutdown...", result); }); // Log the server startup status info!(target: LOG, "Started web server on {}", &adr); // Run the server forever - srv.await + srv.await; + // Log the server shutdown event + info!(target: LOG, "Shutdown complete. Bye!") }; Ok(()) diff --git a/src/net/signals.rs b/src/net/signals.rs new file mode 100644 index 00000000..028560f6 --- /dev/null +++ b/src/net/signals.rs @@ -0,0 +1,23 @@ +use crate::err::Error; + +#[cfg(unix)] +use tokio::signal::unix as signal_os; +#[cfg(windows)] +use tokio::signal::windows as signal_os; + +pub async fn listen() -> Result { + // Get the operating system signal types + let mut interrupt = signal_os::signal(signal_os::SignalKind::interrupt())?; + let mut terminate = signal_os::signal(signal_os::SignalKind::terminate())?; + // Wait until we receive a shutdown signal + tokio::select! { + // Wait for an interrupt signal + _ = interrupt.recv() => { + Ok(String::from("SIGINT")) + } + // Wait for a terminate signal + _ = terminate.recv() => { + Ok(String::from("SIGTERM")) + } + } +}