use tokio::signal::unix::{signal, SignalKind};
use tokio::sync::broadcast;
use tracing::info;
#[derive(Debug)]
pub struct Shutdown {
is_shutdown: bool,
notify: broadcast::Receiver<()>,
}
impl Shutdown {
pub fn new(notify: broadcast::Receiver<()>) -> Shutdown {
Self {
is_shutdown: false,
notify,
}
}
pub fn is_shutdown(&self) -> bool {
self.is_shutdown
}
pub async fn recv(&mut self) {
if self.is_shutdown {
return;
}
let _ = self.notify.recv().await;
self.is_shutdown = true;
}
}
pub async fn shutdown_signal() {
let mut sigint = signal(SignalKind::interrupt()).unwrap();
let mut sigterm = signal(SignalKind::terminate()).unwrap();
let mut sigquit = signal(SignalKind::quit()).unwrap();
tokio::select! {
_ = sigint.recv() => {
info!("received SIGINT, shutting down");
},
_ = sigterm.recv() => {
info!("received SIGTERM, shutting down");
}
_ = sigquit.recv() => {
info!("received SIGQUIT, shutting down");
}
}
}