mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-01 02:57:02 +08:00
feat: Add graceful shutdown in drainer (#498)
This commit is contained in:
38
crates/common_utils/src/signals.rs
Normal file
38
crates/common_utils/src/signals.rs
Normal file
@ -0,0 +1,38 @@
|
||||
//! Provide Interface for worker services to handle signals
|
||||
|
||||
use futures::StreamExt;
|
||||
use router_env::logger;
|
||||
pub use tokio::sync::oneshot;
|
||||
|
||||
///
|
||||
/// This functions is meant to run in parallel to the application.
|
||||
/// It will send a signal to the receiver when a SIGTERM or SIGINT is received
|
||||
///
|
||||
pub async fn signal_handler(mut sig: signal_hook_tokio::Signals, sender: oneshot::Sender<()>) {
|
||||
if let Some(signal) = sig.next().await {
|
||||
logger::info!(
|
||||
"Received signal: {:?}",
|
||||
signal_hook::low_level::signal_name(signal)
|
||||
);
|
||||
match signal {
|
||||
signal_hook::consts::SIGTERM | signal_hook::consts::SIGINT => match sender.send(()) {
|
||||
Ok(_) => {
|
||||
logger::info!("Request for force shutdown received")
|
||||
}
|
||||
Err(_) => {
|
||||
logger::error!(
|
||||
"The receiver is closed, a termination call might already be sent"
|
||||
)
|
||||
}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// This function is used to generate a list of signals that the signal_handler should listen for
|
||||
///
|
||||
pub fn get_allowed_signals() -> Result<signal_hook_tokio::SignalsInfo, std::io::Error> {
|
||||
signal_hook_tokio::Signals::new([signal_hook::consts::SIGTERM, signal_hook::consts::SIGINT])
|
||||
}
|
||||
Reference in New Issue
Block a user