summaryrefslogtreecommitdiffstats
path: root/src/service.rs
blob: 74ecb1d1846b45b1e85915ba0dab926997be01b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use serde::Deserialize;

use crate::Status;

pub mod command;
pub mod http;
pub mod tcp;

pub trait IntoService {
    fn into_service(
        self,
        tx: tokio::sync::watch::Sender<Status>,
    ) -> impl std::future::Future<Output = ()>;
}

impl IntoService for () {
    async fn into_service(self, tx: tokio::sync::watch::Sender<Status>) {
        let mut interval = tokio::time::interval(std::time::Duration::from_secs(3));
        let mut status = Status::Ok;

        loop {
            interval.tick().await;
            status = match tx.send_replace(status) {
                Status::Ok => Status::Error(Some("Test status is in the error state".into())),
                Status::Error(_) => Status::Ok,
            };
        }
    }
}

pub fn default_interval() -> std::time::Duration {
    std::time::Duration::from_secs(5)
}

#[derive(Debug, Clone, Deserialize)]
pub struct ServiceConfig {
    pub name: String,
    #[serde(flatten)]
    pub kind: ServiceKind,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "lowercase")]
#[serde(tag = "kind")]
pub enum ServiceKind {
    Http(http::Http),
    Tcp(tcp::Tcp),
    Exec(command::Command),
    Test(()),
}

impl IntoService for ServiceKind {
    async fn into_service(self, tx: tokio::sync::watch::Sender<Status>) {
        match self {
            ServiceKind::Test(()) => ().into_service(tx).await,
            ServiceKind::Http(h) => h.into_service(tx).await,
            ServiceKind::Tcp(t) => t.into_service(tx).await,
            ServiceKind::Exec(c) => c.into_service(tx).await,
        }
    }
}