Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added waiting for database drivers. #13

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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