capture signal handling in a function

This commit is contained in:
2024-02-16 14:47:32 +01:00
parent bd33f22b30
commit 5560d31638
2 changed files with 25 additions and 21 deletions

21
src/signal.rs Normal file
View File

@@ -0,0 +1,21 @@
use tokio::{
select,
signal::unix::{signal, SignalKind},
sync::watch,
};
pub(crate) fn signal_handler() -> watch::Receiver<()> {
let (stop_tx, stop_rx) = watch::channel(());
tokio::spawn(async move {
let mut sigterm = signal(SignalKind::terminate()).unwrap();
let mut sigint = signal(SignalKind::interrupt()).unwrap();
loop {
select! {
_ = sigterm.recv() => println!("Receive SIGTERM"),
_ = sigint.recv() => println!("Receive SIGTERM"),
};
stop_tx.send(()).unwrap();
}
});
stop_rx
}