Gracefully handle subproces errors/shutdowns

This commit is contained in:
Paul van Tilburg 2020-10-17 00:41:55 +02:00
parent 9191b36940
commit 402b71333b
Signed by: paul
GPG Key ID: C6DE073EDA9EEC4D
1 changed files with 20 additions and 8 deletions

View File

@ -1,4 +1,4 @@
use color_eyre::Result; use color_eyre::{eyre::eyre, Result};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use rocket::{get, routes, Rocket}; use rocket::{get, routes, Rocket};
use rocket_contrib::json::Json; use rocket_contrib::json::Json;
@ -166,7 +166,7 @@ fn rocket() -> Rocket {
} }
#[rocket::main] #[rocket::main]
async fn main() { async fn main() -> Result<()> {
color_eyre::install()?; color_eyre::install()?;
let driver_proc = let driver_proc =
@ -175,12 +175,24 @@ async fn main() {
let (tx, rx) = tokio::sync::oneshot::channel(); let (tx, rx) = tokio::sync::oneshot::channel();
let updater = tokio::spawn(update_loop(rx)); let updater = tokio::spawn(update_loop(rx));
let result = rocket().launch().await; let mut rocket = rocket();
result.expect("Server failed unexpectedly"); let shutdown_handle = rocket.inspect().await.shutdown();
tx.send(()) tokio::select! {
.expect("Could not send update loop shutdown signal"); result = driver_proc => {
let _result = updater.await; shutdown_handle.shutdown();
tx.send(()).map_err(|_| eyre!("Could not send shutdown signal"))?;
result?;
},
result = rocket.launch() => {
tx.send(()).map_err(|_| eyre!("Could not send shutdown signal"))?;
result?;
},
result = updater => {
shutdown_handle.shutdown();
result??;
}
}
drop(driver_proc); Ok(())
} }