2024-11-20 00:16:31 +03:00
|
|
|
use std::{sync::LazyLock, time::Duration};
|
2024-11-20 00:16:31 +03:00
|
|
|
|
2024-11-20 00:16:31 +03:00
|
|
|
use surrealdb::{
|
|
|
|
engine::remote::ws::{Client, Ws},
|
|
|
|
Surreal,
|
|
|
|
};
|
2024-11-20 00:16:31 +03:00
|
|
|
use tokio::time::sleep;
|
2024-11-20 00:16:31 +03:00
|
|
|
|
2024-11-20 00:16:31 +03:00
|
|
|
static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init);
|
|
|
|
|
|
|
|
pub async fn establish_connection() -> Result<(), surrealdb::Error> {
|
|
|
|
DB.connect::<Ws>("localhost:8000").await
|
2024-11-20 00:16:31 +03:00
|
|
|
}
|
2024-11-20 00:16:31 +03:00
|
|
|
|
2024-11-20 00:16:31 +03:00
|
|
|
pub async fn is_alive() -> bool {
|
2024-11-20 00:16:31 +03:00
|
|
|
tokio::select! {
|
2024-11-20 00:16:31 +03:00
|
|
|
db_result = DB.health() => { match db_result {
|
2024-11-20 00:16:31 +03:00
|
|
|
Ok(_) => true,
|
|
|
|
Err(_) => false,
|
|
|
|
} },
|
|
|
|
_ = sleep(Duration::from_secs(1)) => false
|
|
|
|
}
|
|
|
|
}
|