fix: gracefully shutdown on CTRL+C and SIGTERM

Fixes #205
This commit is contained in:
internationalcrisis 2024-10-01 03:59:48 -05:00
parent e57eaa0b78
commit 2fabc975b7

View file

@ -267,8 +267,19 @@ impl Server {
// Bind server to address specified above. Gracefully shut down if CTRL+C is pressed
let server = HyperServer::bind(address).serve(make_svc).with_graceful_shutdown(async {
#[cfg(windows)]
// Wait for the CTRL+C signal
tokio::signal::ctrl_c().await.expect("Failed to install CTRL+C signal handler");
#[cfg(unix)]
{
// Wait for CTRL+C or SIGTERM signals
let mut signal_terminate = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).expect("Failed to install SIGTERM signal handler");
tokio::select! {
_ = tokio::signal::ctrl_c() => (),
_ = signal_terminate.recv() => ()
}
}
});
server.boxed()