summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-03-27 22:41:17 +0800
committerKeuin <[email protected]>2022-03-28 00:11:45 +0800
commitab4b8903ee9b009c3e1a188f1cfe09e39272d2ee (patch)
tree47ae9f9fcede53722a7761db00c0bec462aad1c5 /src
parentc4d5507e9c0b16b0d34df9e871be1d76d296ecea (diff)
Close database gracefully when shutting down.
Diffstat (limited to 'src')
-rw-r--r--src/bot.rs2
-rw-r--r--src/main.rs13
2 files changed, 10 insertions, 5 deletions
diff --git a/src/bot.rs b/src/bot.rs
index b1a4ea2..9ea2c3a 100644
--- a/src/bot.rs
+++ b/src/bot.rs
@@ -58,6 +58,6 @@ pub async fn repl(bot: Bot, db: Arc<database::DbPool>) {
teloxide::repls2::commands_repl(
bot.auto_send(),
move |bot, msg, cmd|
- answer(bot, msg, cmd, Arc::clone(&db)), Command::ty(),
+ answer(bot, msg, cmd, db.clone()), Command::ty(),
).await;
}
diff --git a/src/main.rs b/src/main.rs
index 65e01b2..ad5fdf3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -38,15 +38,17 @@ async fn main() {
let bot = Bot::new(config.bot_token);
let db = config.db_file.as_str();
- println!("Database: {}", db);
- let db: DbPool = database::open(db)
- .await.expect(&*format!("cannot open database {}", db));
+ info!(db, "Opening database...");
+ let db: Arc<DbPool> = Arc::new(database::open(db)
+ .await.expect(&*format!("cannot open database {}", db)));
+ info!("Spawning bot coroutine...");
+ let bot = Bot::new(config.bot_token);
let send_message = warp::path("message")
.and(warp::post())
.and(warp::body::content_length_limit(MAX_BODY_LENGTH))
.and(warp::body::json())
- .and(with_db(db.clone()))
+ .and(with_db(db.deref().clone()))
.and(with_bot(bot.clone()))
.and_then(web::handler);
@@ -60,4 +62,7 @@ async fn main() {
tokio::spawn(warp::serve(send_message).run(endpoint));
tokio::signal::ctrl_c().await.unwrap();
+
+ // gracefully shutdown the database connection
+ db.deref().close().await;
}