Skip to content

Commit

Permalink
[server] Use proper graceful shutdown #812 (#943)
Browse files Browse the repository at this point in the history
Update main.rs
  • Loading branch information
michaelvlach authored Dec 30, 2023
1 parent c3bea64 commit 022a609
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions agdb_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,31 @@ mod utilities;
use crate::db_pool::DbPool;
use server_error::ServerResult;
use tokio::signal;
use tokio::sync::broadcast;
use tracing::Level;

async fn shutdown_signal(mut shutdown_receiver: broadcast::Receiver<()>) {
tokio::select! {
_ = signal::ctrl_c() => {},
_ = shutdown_receiver.recv() => {},
}
}

#[tokio::main]
async fn main() -> ServerResult {
tracing_subscriber::fmt().with_max_level(Level::INFO).init();

let (shutdown_sender, mut shutdown_receiver) = tokio::sync::broadcast::channel::<()>(1);
let (shutdown_sender, shutdown_receiver) = broadcast::channel::<()>(1);
let config = config::new()?;
let db_pool = DbPool::new(&config)?;
let address = format!("{}:{}", config.host, config.port);
let app = app::app(config, shutdown_sender, db_pool);
tracing::info!("Listening at {address}");
let listener = tokio::net::TcpListener::bind(address).await?;

// Use actual graceful shutdown once it becomes available again...
tokio::select! {
_ = signal::ctrl_c() => {},
_ = shutdown_receiver.recv() => {},
_ = async { axum::serve(listener, app).await } => {},
};
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal(shutdown_receiver))
.await?;

Ok(())
}
Expand Down

0 comments on commit 022a609

Please sign in to comment.