Skip to content

Commit

Permalink
server: fix graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
pnmadelaine committed Oct 1, 2023
1 parent 94baff2 commit 3aba889
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
3 changes: 1 addition & 2 deletions typhon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
14 changes: 9 additions & 5 deletions typhon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

0 comments on commit 3aba889

Please sign in to comment.