server: gracefully shut down database connection pool

This commit is contained in:
Copilot
2025-09-04 09:19:03 +02:00
committed by GitHub
parent 89cb59919b
commit 775c5c716d
+14 -6
View File
@@ -125,7 +125,7 @@ async fn setup_sql_tables(database_url: &DatabaseUrl) -> Result<DatabaseConnecti
} }
#[instrument(skip_all)] #[instrument(skip_all)]
async fn set_up_server(config: Configuration) -> Result<ServerBuilder> { async fn set_up_server(config: Configuration) -> Result<(ServerBuilder, DatabaseConnection)> {
info!("Starting LLDAP version {}", env!("CARGO_PKG_VERSION")); info!("Starting LLDAP version {}", env!("CARGO_PKG_VERSION"));
let sql_pool = setup_sql_tables(&config.database_url).await?; let sql_pool = setup_sql_tables(&config.database_url).await?;
@@ -214,9 +214,9 @@ async fn set_up_server(config: Configuration) -> Result<ServerBuilder> {
.await .await
.context("while binding the TCP server")?; .context("while binding the TCP server")?;
// Run every hour. // Run every hour.
let scheduler = Scheduler::new("0 0 * * * * *", sql_pool); let scheduler = Scheduler::new("0 0 * * * * *", sql_pool.clone());
scheduler.start(); scheduler.start();
Ok(server_builder) Ok((server_builder, sql_pool))
} }
async fn run_server_command(opts: RunOpts) -> Result<()> { async fn run_server_command(opts: RunOpts) -> Result<()> {
@@ -225,9 +225,14 @@ async fn run_server_command(opts: RunOpts) -> Result<()> {
let config = configuration::init(opts)?; let config = configuration::init(opts)?;
logging::init(&config)?; logging::init(&config)?;
let server = set_up_server(config).await?.workers(1); let (server, sql_pool) = set_up_server(config).await?;
let server = server.workers(1);
server.run().await.context("while starting the server") let result = server.run().await.context("while starting the server");
if let Err(e) = sql_pool.close().await {
error!("Error closing database connection pool: {}", e);
}
result
} }
async fn send_test_email_command(opts: TestEmailOpts) -> Result<()> { async fn send_test_email_command(opts: TestEmailOpts) -> Result<()> {
@@ -275,8 +280,11 @@ async fn create_schema_command(opts: RunOpts) -> Result<()> {
debug!("CLI: {:#?}", &opts); debug!("CLI: {:#?}", &opts);
let config = configuration::init(opts)?; let config = configuration::init(opts)?;
logging::init(&config)?; logging::init(&config)?;
setup_sql_tables(&config.database_url).await?; let sql_pool = setup_sql_tables(&config.database_url).await?;
info!("Schema created successfully."); info!("Schema created successfully.");
if let Err(e) = sql_pool.close().await {
error!("Error closing database connection pool: {}", e);
}
Ok(()) Ok(())
} }