close db connections on shutdown

This commit is contained in:
Joe Ardent 2023-07-10 11:15:24 -07:00
parent 3532f3daf7
commit a9e3bcbfc3
4 changed files with 34 additions and 4 deletions

View file

@ -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);

View file

@ -36,4 +36,6 @@ async fn main() {
.unwrap();
println!("Added {rows} movies in {dur} seconds");
w2w_db.close().await;
}

View file

@ -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");
}

View file

@ -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");
}