Skip to main content

optative_process_pool/
lib.rs

1mod process;
2
3pub use process::{ProcessIdentity, ProcessSource, ProcessState, SpawnError};
4
5use std::sync::mpsc;
6
7use optative::reconcile::ReconcileErrors;
8use optative::{OptativeSet, Reconcile};
9
10#[derive(Debug, PartialEq, Eq)]
11pub enum StreamKind {
12    Stdout,
13    Stderr,
14}
15
16#[derive(Debug)]
17pub struct StreamItem {
18    pub key: ProcessIdentity,
19    pub stream: StreamKind,
20    pub line: String,
21}
22
23pub struct ProcessPool {
24    inner: OptativeSet<ProcessSource>,
25    stream_tx: mpsc::Sender<StreamItem>,
26}
27
28impl ProcessPool {
29    pub fn new(stream_tx: mpsc::Sender<StreamItem>) -> Self {
30        Self {
31            inner: OptativeSet::new(),
32            stream_tx,
33        }
34    }
35    pub fn reconcile(
36        &mut self,
37        desired: Vec<ProcessSource>,
38    ) -> ReconcileErrors<ProcessIdentity, SpawnError> {
39        self.inner.reconcile(desired, &mut (), &mut self.stream_tx)
40    }
41    pub fn get(&self, identity: &ProcessIdentity) -> Option<&ProcessState> {
42        self.inner.get(identity)
43    }
44    pub fn iter(&self) -> impl Iterator<Item = (&ProcessIdentity, &ProcessState)> {
45        self.inner.iter()
46    }
47}
48
49impl Drop for ProcessPool {
50    fn drop(&mut self) {
51        self.inner
52            .reconcile(Vec::new(), &mut (), &mut self.stream_tx);
53    }
54}