saluki_app/
util.rs

1//! General-purpose application utilities.
2
3use tracing::info;
4
5/// Waits for a shutdown signal.
6///
7/// On Unix, this waits for either `SIGINT` or `SIGTERM`, either of which are used to request a graceful shutdown:
8/// `SIGINT` interactively (`Ctrl+C`), and `SIGTERM` by process supervisors (systemd, container runtimes,
9/// Kubernetes) during rollouts, evictions, node drains, and container shutdown.
10///
11/// On Windows, this waits for either `CTRL_C_EVENT` (interactively) or `CTRL_BREAK_EVENT`, the latter being what a
12/// parent process supervisor sends via `GenerateConsoleCtrlEvent` to request a graceful stop.
13pub async fn wait_for_shutdown_signal() {
14    #[cfg(unix)]
15    {
16        use tokio::signal::unix::{signal, SignalKind};
17
18        let mut sigterm = signal(SignalKind::terminate()).expect("failed to install SIGTERM handler");
19
20        tokio::select! {
21            _ = tokio::signal::ctrl_c() => info!("Received SIGINT, shutting down..."),
22            _ = sigterm.recv() => info!("Received SIGTERM, shutting down..."),
23        }
24    }
25
26    #[cfg(windows)]
27    {
28        let mut ctrl_break = tokio::signal::windows::ctrl_break().expect("failed to install CTRL_BREAK handler");
29
30        tokio::select! {
31            _ = tokio::signal::ctrl_c() => info!("Received CTRL_C, shutting down..."),
32            _ = ctrl_break.recv() => info!("Received CTRL_BREAK, shutting down..."),
33        }
34    }
35
36    #[cfg(not(any(unix, windows)))]
37    {
38        let _ = tokio::signal::ctrl_c().await;
39
40        info!("Received SIGINT, shutting down...");
41    }
42}