From c979e77c54f14b95d7f927745d6426da2a2b5ba7 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Tue, 13 Jun 2023 15:24:13 +0200 Subject: [PATCH] Handle database restarts --- .../src/connections/db_connection.rs | 20 +++++++- geneit_backend/src/services/users_service.rs | 47 ++++++++----------- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/geneit_backend/src/connections/db_connection.rs b/geneit_backend/src/connections/db_connection.rs index 7992e41..3827829 100644 --- a/geneit_backend/src/connections/db_connection.rs +++ b/geneit_backend/src/connections/db_connection.rs @@ -1,6 +1,7 @@ //! # Database connection management use crate::app_config::AppConfig; +use diesel::result::{DatabaseErrorKind, Error}; use diesel::{Connection, PgConnection}; use std::cell::RefCell; thread_local! { @@ -10,7 +11,7 @@ thread_local! { /// Execute a request on the database pub fn execute(cb: E) -> anyhow::Result where - E: FnOnce(&mut PgConnection) -> anyhow::Result, + E: FnOnce(&mut PgConnection) -> diesel::QueryResult, { // Establish connection if required if POSTGRES_CONNECTION.with(|i| i.borrow().is_none()) { @@ -21,5 +22,20 @@ where POSTGRES_CONNECTION.with(|i| *i.borrow_mut() = Some(conn)) } - POSTGRES_CONNECTION.with(|i| cb(i.borrow_mut().as_mut().unwrap())) + match POSTGRES_CONNECTION.with(|i| cb(i.borrow_mut().as_mut().unwrap())) { + Ok(res) => Ok(res), + Err(e) => { + if matches!( + e, + Error::DatabaseError(DatabaseErrorKind::ClosedConnection, _) + | Error::BrokenTransactionManager + ) { + log::warn!("Connection to database closed, dropping handle!"); + POSTGRES_CONNECTION.with(|i| *i.borrow_mut() = None) + } + + log::error!("Database query error! {:?}", e); + Err(e.into()) + } + } } diff --git a/geneit_backend/src/services/users_service.rs b/geneit_backend/src/services/users_service.rs index d135d06..93f2c9f 100644 --- a/geneit_backend/src/services/users_service.rs +++ b/geneit_backend/src/services/users_service.rs @@ -14,16 +14,12 @@ use std::io::ErrorKind; /// Get the information of a user, by its id pub async fn get_by_id(id: UserID) -> anyhow::Result { - db_connection::execute(|conn| Ok(users::table.filter(users::dsl::id.eq(id.0)).first(conn)?)) + db_connection::execute(|conn| users::table.filter(users::dsl::id.eq(id.0)).first(conn)) } /// Get the information of a user, by its mail pub async fn get_by_mail(mail: &str) -> anyhow::Result { - db_connection::execute(|conn| { - Ok(users::table - .filter(users::dsl::email.eq(mail)) - .first(conn)?) - }) + db_connection::execute(|conn| users::table.filter(users::dsl::email.eq(mail)).first(conn)) } /// Get the information of a user, by its password reset token @@ -36,14 +32,14 @@ pub async fn get_by_pwd_reset_token(token: &str) -> anyhow::Result { } db_connection::execute(|conn| { - Ok(users::table + users::table .filter( users::dsl::reset_password_token.eq(token.to_string()).and( users::dsl::time_gen_reset_token .ge(time() as i64 - PASSWORD_RESET_TOKEN_DURATION.as_secs() as i64), ), ) - .first(conn)?) + .first(conn) }) } @@ -57,14 +53,14 @@ pub async fn get_by_account_delete_token(token: &str) -> anyhow::Result { } db_connection::execute(|conn| { - Ok(users::table + users::table .filter( users::dsl::delete_account_token.eq(token.to_string()).and( users::dsl::time_gen_delete_account_token .ge(time() as i64 - ACCOUNT_DELETE_TOKEN_DURATION.as_secs() as i64), ), ) - .first(conn)?) + .first(conn) }) } @@ -213,23 +209,20 @@ pub async fn validate_account(user: &mut User) -> anyhow::Result<()> { /// Update account information pub async fn update_account(user: &User) -> anyhow::Result<()> { db_connection::execute(|conn| { - Ok( - diesel::update(users::dsl::users.filter(users::dsl::id.eq(user.id))) - .set(( - users::dsl::name.eq(user.name.clone()), - users::dsl::admin.eq(user.admin), - users::dsl::active.eq(user.active), - users::dsl::email.eq(user.email.clone()), - users::dsl::time_gen_reset_token.eq(user.time_gen_reset_token), - users::dsl::reset_password_token.eq(user.reset_password_token.clone()), - users::dsl::time_gen_delete_account_token - .eq(user.time_gen_delete_account_token), - users::dsl::delete_account_token.eq(user.delete_account_token.clone()), - users::dsl::time_activate.eq(user.time_activate as i64), - users::dsl::password.eq(user.password.clone()), - )) - .execute(conn)?, - ) + diesel::update(users::dsl::users.filter(users::dsl::id.eq(user.id))) + .set(( + users::dsl::name.eq(user.name.clone()), + users::dsl::admin.eq(user.admin), + users::dsl::active.eq(user.active), + users::dsl::email.eq(user.email.clone()), + users::dsl::time_gen_reset_token.eq(user.time_gen_reset_token), + users::dsl::reset_password_token.eq(user.reset_password_token.clone()), + users::dsl::time_gen_delete_account_token.eq(user.time_gen_delete_account_token), + users::dsl::delete_account_token.eq(user.delete_account_token.clone()), + users::dsl::time_activate.eq(user.time_activate), + users::dsl::password.eq(user.password.clone()), + )) + .execute(conn) })?; Ok(())