drogue_bazaar/health/
mod.rs

1//! Tools for health checks.
2
3use async_trait::async_trait;
4
5#[derive(Debug, thiserror::Error)]
6pub enum HealthCheckError {
7    #[error("Health check failed: {0}")]
8    Failed(#[from] Box<dyn std::error::Error>),
9    #[error("Not OK: {0}")]
10    NotOk(String),
11}
12
13impl HealthCheckError {
14    pub fn from<E>(err: E) -> Self
15    where
16        E: std::error::Error + 'static,
17    {
18        Self::Failed(Box::new(err))
19    }
20
21    pub fn nok<T, S: Into<String>>(reason: S) -> Result<T, Self> {
22        Err(Self::NotOk(reason.into()))
23    }
24}
25
26#[async_trait]
27pub trait HealthChecked: Send + Sync {
28    async fn is_ready(&self) -> Result<(), HealthCheckError> {
29        Ok(())
30    }
31
32    async fn is_alive(&self) -> Result<(), HealthCheckError> {
33        Ok(())
34    }
35}
36
37pub trait BoxedHealthChecked {
38    fn boxed(self) -> Box<dyn HealthChecked>;
39}
40
41impl<T> BoxedHealthChecked for T
42where
43    T: HealthChecked + 'static,
44{
45    fn boxed(self) -> Box<dyn HealthChecked> {
46        Box::new(self)
47    }
48}