Skip to content

Commit

Permalink
fix: Ensure error status propagates correctly to exit code (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
Isawan committed Mar 1, 2024
2 parents 4815fd8 + 8d925bf commit d455289
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
21 changes: 13 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tracing_subscriber::EnvFilter;
static GLOBAL: Jemalloc = Jemalloc;

#[tokio::main]
async fn main() {
async fn main() -> Result<(), ()> {
let args = Args::parse();

tracing_subscriber::fmt()
Expand All @@ -38,16 +38,21 @@ async fn main() {
let (tx, rx) = tokio::sync::oneshot::channel();
let handle = task::spawn(run(args, Some(metric_handle), cancel.child_token(), tx));

let _handle_signal = task::spawn(async move {
select! {
_ = sigterm.recv() => tracing::info!("Received SIGTERM"),
_ = sigint.recv() => tracing::info!("Received SIGINT"),
}
tracing::info!("Terminating server");
cancel.cancel();
});

// Either wait until the server is ready or complete
if (rx.await).is_ok() {
tracing::info!("Server ready");
}

select! {
_ = handle => tracing::info!("Server shutdown"),
_ = sigterm.recv() => tracing::info!("Received SIGTERM"),
_ = sigint.recv() => tracing::info!("Received SIGINT"),
}
cancel.cancel();
tracing::info!("Terminating server");
tracing::info!("Server shutdown");

handle.await.unwrap()
}
12 changes: 7 additions & 5 deletions src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ pub async fn run_migrate(config: MigrateArgs) -> Result<(), ()> {
}
};

sqlx::migrate!("./migrations").run(&db).await.map_err(|e| {
tracing::error!(?e, "Failed to run migrations");
})?;

Ok(())
match sqlx::migrate!("./migrations").run(&db).await {
Ok(()) => Ok(()),
Err(error) => {
error!(reason = %error, "Could not run migrations, exiting.");
Err(())
}
}
}

0 comments on commit d455289

Please sign in to comment.