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