fix(web): ensure prompt shutdown on Ctrl+C and SIGTERM
This commit is contained in:
1 parent
0b1d3fa5c0
commit
6a2bcdaf65
1 file changed
+72
-14
+72
-14
@@ -1437,22 +1437,80 @@ async fn serve() {
|
|||||||
|
|
||||||
let listener = tokio::net::TcpListener::bind(addr).await.expect("Failed to bind address");
|
let listener = tokio::net::TcpListener::bind(addr).await.expect("Failed to bind address");
|
||||||
let shutdown_state = web_state.app.clone();
|
let shutdown_state = web_state.app.clone();
|
||||||
axum::serve(listener, app)
|
let server_shutdown = tokio_util::sync::CancellationToken::new();
|
||||||
.with_graceful_shutdown(async move {
|
let server_shutdown_trigger = server_shutdown.clone();
|
||||||
#[cfg(unix)]
|
tokio::spawn(async move {
|
||||||
{
|
#[cfg(unix)]
|
||||||
let mut terminate = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
|
{
|
||||||
.expect("Failed to listen for SIGTERM");
|
let mut terminate: Option<tokio::signal::unix::Signal> =
|
||||||
tokio::select! { _ = tokio::signal::ctrl_c() => {}, _ = terminate.recv() => {} }
|
match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
|
||||||
|
Ok(s) => Some(s),
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Failed to install SIGTERM handler: {e}; continuing with Ctrl+C only");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
tokio::select! {
|
||||||
|
res = tokio::signal::ctrl_c() => {
|
||||||
|
if let Err(e) = res {
|
||||||
|
tracing::error!("Failed to listen for Ctrl+C: {e}");
|
||||||
|
} else {
|
||||||
|
tracing::info!("Shutdown signal received (Ctrl+C)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ = async {
|
||||||
|
match terminate.as_mut() {
|
||||||
|
Some(signal) => {
|
||||||
|
signal.recv().await;
|
||||||
|
}
|
||||||
|
None => std::future::pending::<()>().await,
|
||||||
|
}
|
||||||
|
} => {
|
||||||
|
tracing::info!("Shutdown signal received (SIGTERM)");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#[cfg(not(unix))]
|
}
|
||||||
let _ = tokio::signal::ctrl_c().await;
|
#[cfg(not(unix))]
|
||||||
backup_stop.cancel();
|
{
|
||||||
})
|
if let Err(e) = tokio::signal::ctrl_c().await {
|
||||||
.await
|
tracing::error!("Failed to listen for Ctrl+C: {e}");
|
||||||
.expect("Server error");
|
} else {
|
||||||
let _ = tokio::time::timeout(std::time::Duration::from_secs(60), backup_worker).await;
|
tracing::info!("Shutdown signal received (Ctrl+C)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
backup_stop.cancel();
|
||||||
|
server_shutdown_trigger.cancel();
|
||||||
|
});
|
||||||
|
|
||||||
|
let shutdown_wait = server_shutdown.clone();
|
||||||
|
let serve_future = axum::serve(listener, app).with_graceful_shutdown(async move {
|
||||||
|
shutdown_wait.cancelled().await;
|
||||||
|
});
|
||||||
|
|
||||||
|
// If graceful shutdown of HTTP connections takes longer than 5 seconds, abort to ensure prompt termination on Ctrl+C.
|
||||||
|
// Non-zero exit codes keep systemd's Restart=on-failure meaningful: a serve error or an
|
||||||
|
// undrained backup worker must not look like a clean stop.
|
||||||
|
let mut exit_code = 0i32;
|
||||||
|
tokio::select! {
|
||||||
|
res = serve_future => {
|
||||||
|
if let Err(e) = res {
|
||||||
|
tracing::error!("Server error: {e}");
|
||||||
|
exit_code = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ = async {
|
||||||
|
server_shutdown.cancelled().await;
|
||||||
|
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
|
||||||
|
} => {
|
||||||
|
tracing::warn!("Graceful HTTP shutdown timed out after 5s; proceeding with teardown");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if tokio::time::timeout(std::time::Duration::from_secs(5), backup_worker).await.is_err() {
|
||||||
|
tracing::warn!("Scheduled backup worker did not drain within 5s; exiting with failure status");
|
||||||
|
exit_code = 1;
|
||||||
|
}
|
||||||
shutdown_state.shutdown(std::time::Duration::from_secs(3)).await;
|
shutdown_state.shutdown(std::time::Duration::from_secs(3)).await;
|
||||||
|
std::process::exit(exit_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
Reference in new issue
Block a user