summaryrefslogtreecommitdiffstats
path: root/src/service.rs
blob: 8e0863c59a24d81f453b6bdc2dc03d12e2037483 (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
62
63
64
65
66
67
use std::{collections::HashMap, sync::Arc};

use futures::Stream;
use http::Http;
use serde::Deserialize;
use systemd::Systemd;
use tcp::Tcp;
use tokio::{
    sync::watch::{channel, Receiver, Sender},
    task::JoinHandle,
};
use tokio_stream::wrappers::WatchStream;

use crate::{Error, Status};

pub mod http;
pub mod systemd;
pub mod tcp;

pub type ServiceHandles = Arc<HashMap<String, ServiceHandle>>;

pub trait ServiceSpawner {
    fn spawn(
        self,
        tx: Sender<Status>,
    ) -> impl std::future::Future<Output = Result<(), Error>> + std::marker::Send + 'static;
}

#[derive(Debug)]
pub struct ServiceHandle {
    pub handle: JoinHandle<Result<(), Error>>,
    pub rx: Receiver<Status>,
}

impl ServiceHandle {
    pub fn new(service: impl ServiceSpawner) -> Self {
        let (tx, rx) = channel(Status::Error(None));
        let handle = tokio::spawn(service.spawn(tx));
        Self { handle, rx }
    }

    pub fn status(&self) -> Status {
        self.rx.borrow().clone()
    }

    pub fn into_stream(&self) -> impl Stream<Item = Status> {
        WatchStream::new(self.rx.clone())
    }
}

#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
pub enum Service {
    Http(Http),
    Tcp(Tcp),
    Systemd(Systemd),
}

impl From<Service> for ServiceHandle {
    fn from(value: Service) -> Self {
        match value {
            Service::Http(s) => ServiceHandle::new(s),
            Service::Tcp(s) => ServiceHandle::new(s),
            Service::Systemd(s) => ServiceHandle::new(s),
        }
    }
}