use tokio::signal;
use tokio::sync::watch;
use tokio::task::{self, JoinHandle};
use crate::Error;
pub fn wait_for_shutdown() -> (JoinHandle<Result<(), Error>>, watch::Receiver<bool>) {
let (tx, rx) = watch::channel(false);
let task = task::spawn(async move {
match signal::ctrl_c().await {
Ok(_) => tx.send(true).map_err(|_| {
let message = "unable to notify connections to shutdown.";
Error::new(message.to_string())
}),
Err(error) => {
if cfg!(debug_assertions) {
eprintln!("unable to register the 'Ctrl-C' signal.");
}
Err(error.into())
}
}
});
(task, rx)
}