use crate::{panic_hook::set_panic_handler, signal::handle_signals};
#[derive(Debug)]
pub(crate) enum ExitType {
Signal(i32),
Panic,
}
pub fn graceful_shutdown() -> flume::Receiver<()> {
let (tx, rx) = flume::bounded(0);
let (tx_exit, rx_exit) = flume::bounded(1);
set_panic_handler(tx_exit.clone());
#[cfg(not(windows))]
tokio::spawn(handle_signals(tx_exit));
tokio::spawn(async move {
if let Ok(exit_type) = rx_exit.recv_async().await {
match exit_type {
ExitType::Signal(signal_n) => info!("exit by: signal {}!", signal_n),
ExitType::Panic => eprintln!("exit by: panic!"),
}
drop(tx);
}
});
rx
}
pub async fn grpc_serve_listen_term(rx: flume::Receiver<()>) {
let _ = rx.recv_async().await;
info!("grpc server exit!");
}