Handle database restarts

This commit is contained in:
Pierre HUBERT 2023-06-13 15:24:13 +02:00
parent 4bfd30f5dd
commit c979e77c54
2 changed files with 38 additions and 29 deletions

View File

@ -1,6 +1,7 @@
//! # Database connection management //! # Database connection management
use crate::app_config::AppConfig; use crate::app_config::AppConfig;
use diesel::result::{DatabaseErrorKind, Error};
use diesel::{Connection, PgConnection}; use diesel::{Connection, PgConnection};
use std::cell::RefCell; use std::cell::RefCell;
thread_local! { thread_local! {
@ -10,7 +11,7 @@ thread_local! {
/// Execute a request on the database /// Execute a request on the database
pub fn execute<E, I>(cb: E) -> anyhow::Result<I> pub fn execute<E, I>(cb: E) -> anyhow::Result<I>
where where
E: FnOnce(&mut PgConnection) -> anyhow::Result<I>, E: FnOnce(&mut PgConnection) -> diesel::QueryResult<I>,
{ {
// Establish connection if required // Establish connection if required
if POSTGRES_CONNECTION.with(|i| i.borrow().is_none()) { 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| *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())
}
}
} }

View File

@ -14,16 +14,12 @@ use std::io::ErrorKind;
/// Get the information of a user, by its id /// Get the information of a user, by its id
pub async fn get_by_id(id: UserID) -> anyhow::Result<User> { pub async fn get_by_id(id: UserID) -> anyhow::Result<User> {
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 /// Get the information of a user, by its mail
pub async fn get_by_mail(mail: &str) -> anyhow::Result<User> { pub async fn get_by_mail(mail: &str) -> anyhow::Result<User> {
db_connection::execute(|conn| { db_connection::execute(|conn| users::table.filter(users::dsl::email.eq(mail)).first(conn))
Ok(users::table
.filter(users::dsl::email.eq(mail))
.first(conn)?)
})
} }
/// Get the information of a user, by its password reset token /// 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<User> {
} }
db_connection::execute(|conn| { db_connection::execute(|conn| {
Ok(users::table users::table
.filter( .filter(
users::dsl::reset_password_token.eq(token.to_string()).and( users::dsl::reset_password_token.eq(token.to_string()).and(
users::dsl::time_gen_reset_token users::dsl::time_gen_reset_token
.ge(time() as i64 - PASSWORD_RESET_TOKEN_DURATION.as_secs() as i64), .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<User> {
} }
db_connection::execute(|conn| { db_connection::execute(|conn| {
Ok(users::table users::table
.filter( .filter(
users::dsl::delete_account_token.eq(token.to_string()).and( users::dsl::delete_account_token.eq(token.to_string()).and(
users::dsl::time_gen_delete_account_token users::dsl::time_gen_delete_account_token
.ge(time() as i64 - ACCOUNT_DELETE_TOKEN_DURATION.as_secs() as i64), .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 /// Update account information
pub async fn update_account(user: &User) -> anyhow::Result<()> { pub async fn update_account(user: &User) -> anyhow::Result<()> {
db_connection::execute(|conn| { db_connection::execute(|conn| {
Ok( diesel::update(users::dsl::users.filter(users::dsl::id.eq(user.id)))
diesel::update(users::dsl::users.filter(users::dsl::id.eq(user.id))) .set((
.set(( users::dsl::name.eq(user.name.clone()),
users::dsl::name.eq(user.name.clone()), users::dsl::admin.eq(user.admin),
users::dsl::admin.eq(user.admin), users::dsl::active.eq(user.active),
users::dsl::active.eq(user.active), users::dsl::email.eq(user.email.clone()),
users::dsl::email.eq(user.email.clone()), users::dsl::time_gen_reset_token.eq(user.time_gen_reset_token),
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::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::time_gen_delete_account_token users::dsl::delete_account_token.eq(user.delete_account_token.clone()),
.eq(user.time_gen_delete_account_token), users::dsl::time_activate.eq(user.time_activate),
users::dsl::delete_account_token.eq(user.delete_account_token.clone()), users::dsl::password.eq(user.password.clone()),
users::dsl::time_activate.eq(user.time_activate as i64), ))
users::dsl::password.eq(user.password.clone()), .execute(conn)
))
.execute(conn)?,
)
})?; })?;
Ok(()) Ok(())