Skip to content

Commit

Permalink
React to more shutdown signals
Browse files Browse the repository at this point in the history
  • Loading branch information
Finomnis committed Dec 2, 2023
1 parent a49a7c6 commit bec0885
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/signal_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,37 @@
async fn wait_for_signal_impl() {
use tokio::signal::unix::{signal, SignalKind};

// Infos here:
// https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html
let mut signal_terminate = signal(SignalKind::terminate()).unwrap();
let mut signal_interrupt = signal(SignalKind::interrupt()).unwrap();
let mut signal_hangup = signal(SignalKind::hangup()).unwrap();

tokio::select! {
_ = signal_terminate.recv() => tracing::debug!("Received SIGTERM."),
_ = signal_interrupt.recv() => tracing::debug!("Received SIGINT."),
_ = signal_hangup.recv() => tracing::debug!("Received SIGHUP."),
};
}

/// Waits for a signal that requests a graceful shutdown, Ctrl-C (SIGINT).
#[cfg(windows)]
async fn wait_for_signal_impl() {
use tokio::signal::ctrl_c;
use tokio::signal::windows;

ctrl_c().await.unwrap();
tracing::debug!("Received SIGINT.");
// Infos here:
// https://learn.microsoft.com/en-us/windows/console/handlerroutine
let mut signal_c = windows::ctrl_c().unwrap();
let mut signal_break = windows::ctrl_break().unwrap();
let mut signal_close = windows::ctrl_close().unwrap();
let mut signal_shutdown = windows::ctrl_shutdown().unwrap();

tokio::select! {
_ = signal_c.recv() => tracing::debug!("Received CTRL_C_EVENT."),
_ = signal_break.recv() => tracing::debug!("Received CTRL_BREAK_EVENT."),
_ = signal_close.recv() => tracing::debug!("Received CTRL_CLOSE_EVENT."),
_ = signal_shutdown.recv() => tracing::debug!("Received CTRL_SHUTDOWN_EVENT."),
};
}

/// Registers Ctrl+C and SIGTERM handlers to cause a program shutdown.
Expand Down

0 comments on commit bec0885

Please sign in to comment.