ops_core/
lib.rs

1pub use async_trait::async_trait;
2
3/// An interface for something that can be periodically checked.
4#[async_trait]
5pub trait Checker: Send + Sync {
6    /// Runs the check and returns a [`CheckResponse`](struct.CheckResponse.html).
7    async fn check(&self) -> CheckResponse;
8}
9
10/// The response of a check.
11#[derive(Debug)]
12pub struct CheckResponse {
13    health: Health,
14    output: String,
15    action: Option<String>,
16    impact: Option<String>,
17}
18
19impl CheckResponse {
20    /// Creates a healthy [`CheckResponse`](struct.CheckResponse.html).
21    pub fn healthy(output: &str) -> Self {
22        CheckResponse {
23            health: Health::Healthy,
24            output: output.to_owned(),
25            action: None,
26            impact: None,
27        }
28    }
29
30    /// Creates a degraded [`CheckResponse`](struct.CheckResponse.html).
31    pub fn degraded(output: &str, action: &str) -> Self {
32        CheckResponse {
33            health: Health::Degraded,
34            output: output.to_owned(),
35            action: Some(action.to_owned()),
36            impact: None,
37        }
38    }
39
40    /// Creates an unhealthy [`CheckResponse`](struct.CheckResponse.html).
41    pub fn unhealthy(output: &str, action: &str, impact: &str) -> Self {
42        CheckResponse {
43            health: Health::Unhealthy,
44            output: output.to_owned(),
45            action: Some(action.to_owned()),
46            impact: Some(impact.to_owned()),
47        }
48    }
49
50    /// Health status of the check.
51    pub fn health(&self) -> Health {
52        self.health
53    }
54
55    /// Text representation of the current status.
56    pub fn output(&self) -> &str {
57        &self.output
58    }
59
60    /// Action to resolve the issue if non-healthy.
61    pub fn action(&self) -> Option<&str> {
62        self.action.as_ref().map(String::as_ref)
63    }
64
65    /// Impact of not fixing the issue.
66    pub fn impact(&self) -> Option<&str> {
67        self.impact.as_ref().map(String::as_ref)
68    }
69}
70
71/// Health statuses.
72#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
73pub enum Health {
74    Healthy,
75    Degraded,
76    Unhealthy,
77}
78
79impl Into<&'static str> for Health {
80    fn into(self) -> &'static str {
81        match self {
82            Health::Healthy => "healthy",
83            Health::Degraded => "degraded",
84            Health::Unhealthy => "unhealthy",
85        }
86    }
87}