Skip to main content

malbox_plugin_sdk/
health.rs

1//! Plugin health status.
2//!
3//! The daemon polls [`HealthStatus`] via [`Plugin::health_check`](crate::plugin::Plugin::health_check)
4//! to decide whether a plugin is ready to receive tasks.
5
6/// Reports whether a plugin is ready to accept tasks.
7///
8/// Constructed via [`HealthStatus::ready`] or [`HealthStatus::not_ready`].
9/// The daemon polls this periodically and will not dispatch tasks to a
10/// plugin that reports not-ready.
11#[derive(Debug, Clone, PartialEq)]
12#[non_exhaustive]
13pub struct HealthStatus {
14    pub(crate) ready: bool,
15    pub(crate) reason: String,
16}
17
18impl HealthStatus {
19    /// Create a status indicating the plugin is ready to accept tasks.
20    pub fn ready() -> Self {
21        Self {
22            ready: true,
23            reason: String::new(),
24        }
25    }
26
27    /// Create a status indicating the plugin is **not** ready, with a reason.
28    pub fn not_ready(reason: impl Into<String>) -> Self {
29        Self {
30            ready: false,
31            reason: reason.into(),
32        }
33    }
34
35    /// Return whether the plugin is ready to accept tasks.
36    pub fn is_ready(&self) -> bool {
37        self.ready
38    }
39
40    /// Return the reason string (empty if ready).
41    pub fn reason(&self) -> &str {
42        &self.reason
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn health_status_ready() {
52        let status = HealthStatus::ready();
53        assert!(status.is_ready());
54        assert!(status.reason().is_empty());
55    }
56
57    #[test]
58    fn health_status_not_ready() {
59        let status = HealthStatus::not_ready("loading rules");
60        assert!(!status.is_ready());
61        assert_eq!(status.reason(), "loading rules");
62    }
63
64    #[test]
65    fn health_status_exposes_getters() {
66        let ready = HealthStatus::ready();
67        assert!(ready.is_ready());
68        assert_eq!(ready.reason(), "");
69
70        let busy = HealthStatus::not_ready("loading rules");
71        assert!(!busy.is_ready());
72        assert_eq!(busy.reason(), "loading rules");
73    }
74}