From a9e3bcbfc399311415487bf4e8530e45ceae81eb Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Mon, 10 Jul 2023 11:15:24 -0700 Subject: [PATCH] close db connections on shutdown --- migrations/20230426221940_init.up.sql | 4 ++-- src/bin/import_omega.rs | 2 ++ src/bin/import_users.rs | 1 + src/main.rs | 31 +++++++++++++++++++++++++-- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/migrations/20230426221940_init.up.sql b/migrations/20230426221940_init.up.sql index 27ce383..da7493d 100644 --- a/migrations/20230426221940_init.up.sql +++ b/migrations/20230426221940_init.up.sql @@ -64,8 +64,8 @@ create table if not exists watch_notes ( -- indices, not needed for follows create index if not exists user_username_dex on users (username); -create index if not exists user_email_dex on users (email); -create index if not exists watch_title_dex on watches (title); +create index if not exists user_email_dex on users (lower(email)); +create index if not exists watch_title_dex on watches (lower(title)); create index if not exists watch_added_by_dex on watches (added_by); create index if not exists quests_user_dex on watch_quests (user); create index if not exists quests_watch_dex on watch_quests (watch); diff --git a/src/bin/import_omega.rs b/src/bin/import_omega.rs index 378a2e9..3e20746 100644 --- a/src/bin/import_omega.rs +++ b/src/bin/import_omega.rs @@ -36,4 +36,6 @@ async fn main() { .unwrap(); println!("Added {rows} movies in {dur} seconds"); + + w2w_db.close().await; } diff --git a/src/bin/import_users.rs b/src/bin/import_users.rs index dff3b74..b65365b 100644 --- a/src/bin/import_users.rs +++ b/src/bin/import_users.rs @@ -53,6 +53,7 @@ async fn main() { .fetch_one(&w2w_db) .await .unwrap(); + w2w_db.close().await; let dur = (end - start).as_secs_f32(); println!("Added {rows} quests in {dur} seconds"); } diff --git a/src/main.rs b/src/main.rs index d383ab4..350029b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::net::SocketAddr; use rand::{thread_rng, RngCore}; +use tokio::signal; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use what2watch::get_db_pool; @@ -23,13 +24,39 @@ async fn main() { bytes }; - let app = what2watch::app(pool, &secret).await; + let app = what2watch::app(pool.clone(), &secret).await; let addr: SocketAddr = ([0, 0, 0, 0], 3000).into(); tracing::debug!("binding to {addr:?}"); axum::Server::bind(&addr) .serve(app.into_make_service()) + .with_graceful_shutdown(shutdown_signal()) .await - .unwrap(); + .unwrap_or_default(); + + pool.close().await; +} + +async fn shutdown_signal() { + let ctrl_c = async { + signal::ctrl_c() + .await + .expect("failed to install Ctrl+C handler"); + }; + + #[cfg(unix)] + let terminate = async { + signal::unix::signal(signal::unix::SignalKind::terminate()) + .expect("failed to install signal handler") + .recv() + .await; + }; + + tokio::select! { + _ = ctrl_c => {}, + _ = terminate => {}, + } + + println!("signal received, starting graceful shutdown"); }