Skip to main content

optative_process_pool/
lib.rs

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