Frontend: Graceful shutdown of backend

This commit is contained in:
Natty 2023-09-30 23:02:39 +02:00
parent 3837980e50
commit 2525b3a50a
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
1 changed files with 29 additions and 0 deletions

View File

@ -21,8 +21,10 @@ use std::sync::Arc;
use std::time::Duration;
use tera::Tera;
use thiserror::Error;
use tokio::signal;
use tower_http::services::ServeFile;
use tower_http::trace::TraceLayer;
use tracing::error;
use tracing::log::info;
use tracing_subscriber::EnvFilter;
@ -117,6 +119,33 @@ async fn main() -> miette::Result<()> {
info!("Serving on: {addr}");
axum::Server::bind(&addr)
.serve(app.into_make_service())
.with_graceful_shutdown(shutdown_signal())
.await
.map_err(|e| miette!("Error running server: {}", e))
}
async fn shutdown_signal() {
let ctrl_c = async {
if let Err(e) = signal::ctrl_c().await {
error!("Ctrl+C signal handler error: {}", e);
}
};
#[cfg(unix)]
let terminate = async {
signal::unix::signal(signal::unix::SignalKind::terminate())
.expect("SIGTERM handler error")
.recv()
.await;
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();
tokio::select! {
_ = ctrl_c => {},
_ = terminate => {},
}
info!("Shutting down...");
}