From 70839d70d86354232f168e63ce4062219acb85c7 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 13 Oct 2021 17:12:13 +0200 Subject: Try to handle termination and closing of stuff properly --- src/util.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/util.rs') diff --git a/src/util.rs b/src/util.rs index ba485bf..e5b57ec 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,7 @@ use serde::Serialize; +use log::info; + use tokio::sync::watch; pub type NodeID = sodiumoxide::crypto::sign::ed25519::PublicKey; @@ -38,3 +40,15 @@ pub async fn await_exit(mut must_exit: watch::Receiver) { } } } + +pub fn watch_ctrl_c() -> watch::Receiver { + let (send_cancel, watch_cancel) = watch::channel(false); + tokio::spawn(async move { + tokio::signal::ctrl_c() + .await + .expect("failed to install CTRL+C signal handler"); + info!("Received CTRL+C, shutting down."); + send_cancel.send(true).unwrap(); + }); + watch_cancel +} -- cgit v1.2.3