1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
//! Graceful shutdown helpers. /// Waits for OS shutdown signals. pub struct GracefulShutdown { timeout_secs: u64, } impl GracefulShutdown { /// Creates a new graceful shutdown helper. pub fn new(timeout_secs: u64) -> Self { Self { timeout_secs } } /// Waits for CTRL-C or SIGTERM. pub async fn wait_for_signal(&self) { let _ = self.timeout_secs; #[cfg(unix)] { if let Ok(mut term) = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) { tokio::select! { _ = tokio::signal::ctrl_c() => {} _ = term.recv() => {} } } else { let _ = tokio::signal::ctrl_c().await; } } #[cfg(not(unix))] { let _ = tokio::signal::ctrl_c().await; } } }