Skip to main content

espresso_utils/
shutdown.rs

1//! Graceful shutdown on OS termination signals.
2
3use tokio::signal::unix::{SignalKind, signal};
4
5/// Block until the process receives `SIGINT` or `SIGTERM`, log the signal at
6/// `WARN`, and return its name.
7///
8/// Intended for use in a `tokio::select!` alongside a daemon's main future so
9/// the process exits cleanly instead of being hard-killed.
10pub async fn wait_for_shutdown_signal() -> &'static str {
11    let mut interrupt = signal(SignalKind::interrupt()).expect("install SIGINT handler");
12    let mut terminate = signal(SignalKind::terminate()).expect("install SIGTERM handler");
13    let signal = tokio::select! {
14        _ = interrupt.recv() => "SIGINT",
15        _ = terminate.recv() => "SIGTERM",
16    };
17    tracing::warn!(signal, "received shutdown signal; shutting down gracefully");
18    signal
19}