#[cfg(any(feature = "cache-memory", feature = "cache-redis"))]
use super::CacheHealthCheck;
#[cfg(any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql"))]
use super::DbHealthCheck;
use super::{ConfigHealthCheck, HealthCheck, HealthResult, HealthStatus};
use crate::config::GarrisonConfig;
#[cfg(any(feature = "db-postgres", feature = "db-mysql", feature = "cache-redis"))]
use crate::dao::GarrisonDao;
#[cfg(any(feature = "db-postgres", feature = "db-mysql", feature = "cache-redis"))]
use crate::manager::GarrisonManager;
use std::sync::Arc;
#[cfg(any(feature = "db-postgres", feature = "db-mysql", feature = "cache-redis"))]
use std::time::Duration;
#[cfg(any(feature = "db-postgres", feature = "db-mysql", feature = "cache-redis"))]
const HEALTH_PROBE_TIMEOUT: Duration = Duration::from_secs(2);
#[cfg(any(feature = "db-postgres", feature = "db-mysql", feature = "cache-redis"))]
const HEALTH_PROBE_KEY: &str = "__garrison_health_probe__";
impl ConfigHealthCheck {
pub fn new(config: Arc<GarrisonConfig>) -> Self {
Self { config }
}
}
impl HealthCheck for ConfigHealthCheck {
fn name(&self) -> &str {
"config"
}
fn check(
&self,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = HealthResult<HealthStatus>> + Send>>
{
let config = self.config.clone();
Box::pin(async move {
config.validate()?;
Ok(HealthStatus::Healthy)
})
}
}
#[cfg(any(feature = "cache-memory", feature = "cache-redis"))]
impl CacheHealthCheck {
pub fn new() -> Self {
Self
}
}
#[cfg(any(feature = "cache-memory", feature = "cache-redis"))]
impl Default for CacheHealthCheck {
fn default() -> Self {
Self::new()
}
}
#[cfg(all(feature = "cache-memory", not(feature = "cache-redis")))]
impl HealthCheck for CacheHealthCheck {
fn name(&self) -> &str {
"cache"
}
fn check(
&self,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = HealthResult<HealthStatus>> + Send>>
{
Box::pin(async { Ok(HealthStatus::Healthy) })
}
}
#[cfg(feature = "cache-redis")]
impl HealthCheck for CacheHealthCheck {
fn name(&self) -> &str {
"cache"
}
fn check(
&self,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = HealthResult<HealthStatus>> + Send>>
{
Box::pin(async {
let logic = match GarrisonManager::logic() {
Ok(l) => l,
Err(_) => return Ok(HealthStatus::Unhealthy),
};
let dao: Arc<dyn GarrisonDao> = Arc::clone(logic.session.dao());
match tokio::time::timeout(HEALTH_PROBE_TIMEOUT, dao.get(HEALTH_PROBE_KEY)).await {
Ok(Ok(_)) => Ok(HealthStatus::Healthy),
Ok(Err(_)) => Ok(HealthStatus::Unhealthy),
Err(_) => Ok(HealthStatus::Unhealthy),
}
})
}
}
#[cfg(any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql"))]
impl DbHealthCheck {
pub fn new() -> Self {
Self
}
}
#[cfg(any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql"))]
impl Default for DbHealthCheck {
fn default() -> Self {
Self::new()
}
}
#[cfg(all(
feature = "db-sqlite",
not(any(feature = "db-postgres", feature = "db-mysql"))
))]
impl HealthCheck for DbHealthCheck {
fn name(&self) -> &str {
"database"
}
fn check(
&self,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = HealthResult<HealthStatus>> + Send>>
{
Box::pin(async { Ok(HealthStatus::Healthy) })
}
}
#[cfg(any(feature = "db-postgres", feature = "db-mysql"))]
impl HealthCheck for DbHealthCheck {
fn name(&self) -> &str {
"database"
}
fn check(
&self,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = HealthResult<HealthStatus>> + Send>>
{
Box::pin(async {
let logic = match GarrisonManager::logic() {
Ok(l) => l,
Err(_) => return Ok(HealthStatus::Unhealthy),
};
let dao: Arc<dyn GarrisonDao> = Arc::clone(logic.session.dao());
match tokio::time::timeout(HEALTH_PROBE_TIMEOUT, dao.get(HEALTH_PROBE_KEY)).await {
Ok(Ok(_)) => Ok(HealthStatus::Healthy),
Ok(Err(_)) => Ok(HealthStatus::Unhealthy),
Err(_) => Ok(HealthStatus::Unhealthy),
}
})
}
}