Skip to main content

folk_api/
health.rs

1//! Health check registration.
2
3use std::collections::HashMap;
4use std::sync::Arc;
5
6use async_trait::async_trait;
7
8use crate::rpc::BoxFuture;
9
10/// Status returned by a single plugin's health check.
11#[derive(Debug, Clone)]
12pub struct HealthStatus {
13    /// `true` if the plugin is healthy.
14    pub healthy: bool,
15    /// Optional message (typically explains why if `healthy` is `false`).
16    pub message: Option<String>,
17}
18
19impl HealthStatus {
20    pub fn ok() -> Self {
21        Self {
22            healthy: true,
23            message: None,
24        }
25    }
26
27    pub fn degraded(msg: impl Into<String>) -> Self {
28        Self {
29            healthy: false,
30            message: Some(msg.into()),
31        }
32    }
33}
34
35/// Async health check function.
36pub type HealthCheckFn = Arc<dyn Fn() -> BoxFuture<'static, HealthStatus> + Send + Sync>;
37
38/// Plugins register health checks here at boot.
39#[async_trait]
40pub trait HealthRegistry: Send + Sync + 'static {
41    /// Register a health check for the named plugin.
42    async fn register(&self, plugin_name: String, check: HealthCheckFn);
43
44    /// Run all registered checks and return their results, keyed by plugin name.
45    async fn check_all(&self) -> HashMap<String, HealthStatus>;
46}