From ec1aa9db0ad380101d1f23e69dec9debba7bdffc Mon Sep 17 00:00:00 2001 From: toast Date: Sat, 3 Aug 2024 12:02:54 +1000 Subject: [PATCH] [no ci] Update controllers folder --- src/controllers/cache.rs | 4 +-- src/controllers/database.rs | 55 +++++++++++++++++++------------------ 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/controllers/cache.rs b/src/controllers/cache.rs index e3cef8a..90a20fc 100644 --- a/src/controllers/cache.rs +++ b/src/controllers/cache.rs @@ -9,8 +9,8 @@ use bb8_redis::{ RedisConnectionManager }; use tokio::time::{ - sleep, - Duration + Duration, + sleep }; #[derive(Debug)] diff --git a/src/controllers/database.rs b/src/controllers/database.rs index b3163b1..c053672 100644 --- a/src/controllers/database.rs +++ b/src/controllers/database.rs @@ -1,22 +1,24 @@ use crate::internals::utils::token_path; -use std::sync::LazyLock; use bb8::{Pool, PooledConnection}; use bb8_postgres::PostgresConnectionManager; +use tokio::time::{ + Duration, + sleep +}; use tokio_postgres::{ Client, NoTls, Error, config::Config }; -use tokio::time::{ - sleep, - Duration -}; use std::{ ops::Deref, str::FromStr, - sync::Mutex + sync::{ + Mutex, + LazyLock + } }; pub static DATABASE: LazyLock>> = LazyLock::new(|| Mutex::new(None)); @@ -28,27 +30,29 @@ pub struct DatabaseController { impl DatabaseController { pub async fn new() -> Result<(), Error> { let manager = PostgresConnectionManager::new(Config::from_str(token_path().await.postgres_uri.as_str())?, NoTls); - let pool = Pool::builder().build(manager).await?; + let pool = bb8::Pool::builder().build(manager).await?; let err_name = "Postgres[Error]"; let pool_clone = pool.clone(); tokio::spawn(async move { - let conn = match Self::attempt_connect(&pool_clone).await { - Ok(conn) => { - println!("Postgres[Info]: Successfully connected"); - conn - }, - Err(y) => { - eprintln!("{}: {}", err_name, y); - return; + loop { + match Self::attempt_connect(&pool_clone).await { + Ok(conn) => { + println!("Postgres[Info]: Successfully connected"); + let client: &Client = conn.deref(); + + /* if let Err(e) = client.batch_execute("").await { + eprintln!("{}: {}", err_name, e); + } */ // Uncomment this if bot is going to use a database + }, + Err(e) => { + eprintln!("{}: {}", err_name, e); + sleep(Duration::from_secs(5)).await; + } } - }; - let client: &Client = conn.deref(); - - /* if let Err(e) = client.batch_execute("").await { - eprintln!("{}: {}", err_name, e); - } */ // Uncomment this if bot is going to use a database - }).await.unwrap(); + break; + } + }); let controller = Self { pool }; *DATABASE.lock().unwrap() = Some(controller); @@ -56,14 +60,13 @@ impl DatabaseController { Ok(()) } - async fn attempt_connect<'a>(pool: &'a Pool>) -> Result>, bb8::RunError> { + async fn attempt_connect<'a>(pool: &'a bb8::Pool>) -> Result>, bb8::RunError> { let mut backoff = 1; - loop { match pool.get().await { Ok(conn) => return Ok(conn), - Err(y) => { - eprintln!("Postgres[ConnError]: {}, retrying in {} seconds", y, backoff); + Err(e) => { + eprintln!("Postgres[ConnError]: {}, retrying in {} seconds", e, backoff); sleep(Duration::from_secs(backoff)).await; if backoff < 64 { backoff *= 2;