#[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(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(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 {
#[cfg(any(feature = "db-postgres", feature = "db-mysql"))]
{
Self { pool: None }
}
#[cfg(not(any(feature = "db-postgres", feature = "db-mysql")))]
{
Self { _priv: () }
}
}
#[cfg(any(feature = "db-postgres", feature = "db-mysql"))]
pub fn with_pool(pool: dbnexus::DbPool) -> Self {
Self {
pool: Some(std::sync::Arc::new(pool)),
}
}
}
#[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>>
{
#[cfg(any(feature = "db-postgres", feature = "db-mysql"))]
let pool = self.pool.clone();
Box::pin(async move {
if GarrisonManager::logic().is_err() {
return Ok(HealthStatus::Unhealthy);
}
let Some(pool) = pool.as_ref() else {
tracing::warn!(
"DbHealthCheck: no pool injected (use DbHealthCheck::with_pool); \
reporting Degraded instead of Healthy"
);
return Ok(HealthStatus::Degraded);
};
let ping = async {
match pool.get_session("admin").await {
Ok(session) => session.execute_raw("SELECT 1").await.is_ok(),
Err(_) => false,
}
};
match tokio::time::timeout(HEALTH_PROBE_TIMEOUT, ping).await {
Ok(true) => Ok(HealthStatus::Healthy),
_ => Ok(HealthStatus::Unhealthy),
}
})
}
}