feat(chat-server): implement graceful shutdown with OS signal handling
This commit is contained in:
parent
bee87f051c
commit
a89e1e92bd
1 changed files with 28 additions and 0 deletions
|
|
@ -304,6 +304,7 @@ async fn main() -> Result<(), ChatError> {
|
||||||
info!(" - GET /ws - WebSocket Chat (🆕)");
|
info!(" - GET /ws - WebSocket Chat (🆕)");
|
||||||
|
|
||||||
axum::serve(listener, app)
|
axum::serve(listener, app)
|
||||||
|
.with_graceful_shutdown(shutdown_signal())
|
||||||
.await
|
.await
|
||||||
.map_err(|e| ChatError::configuration_error(&format!("Server error: {e}")))?;
|
.map_err(|e| ChatError::configuration_error(&format!("Server error: {e}")))?;
|
||||||
|
|
||||||
|
|
@ -447,3 +448,30 @@ async fn get_stats(State(_state): State<AppState>) -> Json<ApiResponse<HashMap<S
|
||||||
|
|
||||||
Json(ApiResponse::success(stats))
|
Json(ApiResponse::success(stats))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gestionnaire de signal d'arrêt (Graceful Shutdown)
|
||||||
|
async fn shutdown_signal() {
|
||||||
|
let ctrl_c = async {
|
||||||
|
tokio::signal::ctrl_c()
|
||||||
|
.await
|
||||||
|
.expect("failed to install Ctrl+C handler");
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
let terminate = async {
|
||||||
|
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
|
||||||
|
.expect("failed to install signal handler")
|
||||||
|
.recv()
|
||||||
|
.await;
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(not(unix))]
|
||||||
|
let terminate = std::future::pending::<()>();
|
||||||
|
|
||||||
|
tokio::select! {
|
||||||
|
_ = ctrl_c => {},
|
||||||
|
_ = terminate => {},
|
||||||
|
}
|
||||||
|
|
||||||
|
info!("🛑 Signal d'arrêt reçu, fermeture gracieuse...");
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue