From 87559a56ff51bf0a4a5f886889d2a6a64ad7f947 Mon Sep 17 00:00:00 2001 From: Paweł Dybiec Date: Tue, 27 Dec 2022 18:21:32 +0000 Subject: Proper handling of signals --- src/main.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index bc3b8b7..6ea162b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod mattermost; use std::path::Path; use anyhow::anyhow; +use tokio::signal::unix; use tokio::{self}; use tracing::{debug, warn}; @@ -118,12 +119,15 @@ async fn main() -> Result<(), Box> { .handle_websocket_stream(Vav::new(db), shutdown_recv) .await }); - match tokio::signal::ctrl_c().await { - Ok(()) => {} - Err(err) => { - eprintln!("Unable to listen for shutdown signal: {}", err); - // we also shut down in case of error - } + { + let mut sigterm = unix::signal(unix::SignalKind::terminate())?; + let mut sigint = unix::signal(unix::SignalKind::interrupt())?; + let mut sigquit = unix::signal(unix::SignalKind::quit())?; + tokio::select! { + signal = sigterm.recv() => signal, + signal = sigint.recv() => signal, + signal = sigquit.recv() => signal, + }; } shutdown_send.send(())?; websocket_task.await??; -- cgit 1.4.1