Skip to content

Commit

Permalink
added waiting (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilpriver authored Dec 22, 2023
1 parent eb4b196 commit 2a666c5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ pub fn database_token() -> Option<String> {
None
}

pub fn wait_timeout() -> usize {
if let Ok(v) = env::var("DATABASE_WAIT_TIMEOUT") {
if let Ok(v) = v.parse::<usize>() {
return v;
}
}

0
}

pub enum Database {
LibSQL,
Postgres,
Expand Down
1 change: 1 addition & 0 deletions src/database_drivers/libsql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl<'a> LibSQLDriver {
Ok(c) => c,
Err(err) => bail!("{:?}", err),
};

Ok(LibSQLDriver { db: client })
}
}
Expand Down
20 changes: 19 additions & 1 deletion src/database_drivers/maria.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config;
use crate::database_drivers::DatabaseDriver;
use anyhow::Result;
use sqlx::mysql::MySqlRow;
Expand All @@ -11,7 +12,24 @@ pub struct MariaDBDriver {

impl<'a> MariaDBDriver {
pub async fn new<'b>(db_url: &str) -> Result<MariaDBDriver> {
let client = MySqlConnection::connect(db_url).await?;
let mut client = MySqlConnection::connect(db_url).await?;

let wait_timeout = config::wait_timeout();

let mut count = 0;
loop {
if count > wait_timeout {
break;
}

if client.ping().await.is_ok() {
break;
}

count += 1;
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
}

Ok(MariaDBDriver { db: client })
}
}
Expand Down
19 changes: 18 additions & 1 deletion src/database_drivers/mysql.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config;
use crate::database_drivers::DatabaseDriver;
use anyhow::Result;
use sqlx::mysql::MySqlRow;
Expand All @@ -11,7 +12,23 @@ pub struct MySQLDriver {

impl<'a> MySQLDriver {
pub async fn new<'b>(db_url: &str) -> Result<MySQLDriver> {
let client = MySqlConnection::connect(db_url).await?;
let mut client = MySqlConnection::connect(db_url).await?;

let wait_timeout = config::wait_timeout();
let mut count = 0;
loop {
if count > wait_timeout {
break;
}

if client.ping().await.is_ok() {
break;
}

count += 1;
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
}

Ok(MySQLDriver { db: client })
}
}
Expand Down
19 changes: 18 additions & 1 deletion src/database_drivers/postgres.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config;
use crate::database_drivers::DatabaseDriver;
use anyhow::Result;
use sqlx::postgres::PgRow;
Expand All @@ -11,7 +12,23 @@ pub struct PostgresDriver {

impl<'a> PostgresDriver {
pub async fn new<'b>(db_url: &str) -> Result<PostgresDriver> {
let client = PgConnection::connect(db_url).await?;
let mut client = PgConnection::connect(db_url).await?;

let wait_timeout = config::wait_timeout();
let mut count = 0;
loop {
if count > wait_timeout {
break;
}

if client.ping().await.is_ok() {
break;
}

count += 1;
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
}

Ok(PostgresDriver { db: client })
}
}
Expand Down

0 comments on commit 2a666c5

Please sign in to comment.