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};
20use optative_derive::Ephemeral;
21
22#[derive(Debug, PartialEq, Eq)]
23pub enum StreamKind {
24    Stdout,
25    Stderr,
26}
27
28#[derive(Debug)]
29pub struct StreamItem {
30    pub key: ProcessIdentity,
31    pub stream: StreamKind,
32    pub line: String,
33}
34
35#[derive(Ephemeral)]
36pub struct ProcessPool {
37    #[reconciler(output = stream_tx)]
38    inner: OptativeSet<ProcessSource>,
39    stream_tx: mpsc::Sender<StreamItem>,
40}
41
42impl ProcessPool {
43    pub fn new(stream_tx: mpsc::Sender<StreamItem>) -> Self {
44        Self {
45            inner: OptativeSet::new(),
46            stream_tx,
47        }
48    }
49    pub fn reconcile(
50        &mut self,
51        desired: Vec<ProcessSource>,
52    ) -> ReconcileErrors<ProcessIdentity, SpawnError> {
53        self.inner.reconcile(desired, &mut (), &mut self.stream_tx)
54    }
55    pub fn get(&self, identity: &ProcessIdentity) -> Option<&ProcessState> {
56        self.inner.get(identity)
57    }
58    pub fn iter(&self) -> impl Iterator<Item = (&ProcessIdentity, &ProcessState)> {
59        self.inner.iter()
60    }
61}