From 3aba88927b47f686b90b12b7c92ca1a7fd49901b Mon Sep 17 00:00:00 2001 From: Paul-Nicolas Madelaine Date: Sun, 1 Oct 2023 16:00:47 +0200 Subject: [PATCH] server: fix graceful shutdown --- typhon/src/lib.rs | 3 +-- typhon/src/main.rs | 14 +++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/typhon/src/lib.rs b/typhon/src/lib.rs index 2b9dd062..e105a57a 100644 --- a/typhon/src/lib.rs +++ b/typhon/src/lib.rs @@ -286,13 +286,12 @@ pub fn log_event(event: Event) { } pub async fn shutdown() { - let res = tokio::signal::ctrl_c().await; eprintln!("Typhon is shutting down..."); - let _ = res.map_err(|e| log::error!("{}", e)); tokio::join!( EVALUATIONS.shutdown(), JOBS_BUILD.shutdown(), JOBS_BEGIN.shutdown(), JOBS_END.shutdown(), ); + eprintln!("Good bye!"); } diff --git a/typhon/src/main.rs b/typhon/src/main.rs index 71975ce7..00278dd4 100644 --- a/typhon/src/main.rs +++ b/typhon/src/main.rs @@ -30,15 +30,19 @@ async fn main() -> std::io::Result<()> { .expect("failed to run migrations"); // Run actix server - let actix = HttpServer::new(|| App::new().configure(typhon::api::config)) - .bind(("127.0.0.1", 8000))? - .run(); + let actix = tokio::spawn( + HttpServer::new(|| App::new().configure(typhon::api::config)) + .bind(("127.0.0.1", 8000))? + .run(), + ); // Graceful shutdown + let ctrl_c = tokio::signal::ctrl_c(); tokio::select! { - _ = actix => panic!(), - _ = typhon::shutdown() => eprintln!("Good bye!"), + _ = actix => (), + res = ctrl_c => res.map_err(|e| log::error!("{}", e)).unwrap_or(()), } + typhon::shutdown().await; Ok(()) }