use crate::{ProcessManager, Runnable};
use std::borrow::Cow;
use std::time::Duration;
type BoxedInitializer = Box<dyn FnOnce(&mut ProcessManager) + Send>;
#[derive(Default)]
pub struct ProcessManagerBuilder {
auto_cleanup: bool,
custom_name: Option<Cow<'static, str>>,
shutdown_grace_period: Option<Duration>,
initializers: Vec<BoxedInitializer>,
}
impl ProcessManagerBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn auto_cleanup(mut self, enabled: bool) -> Self {
self.auto_cleanup = enabled;
self
}
pub fn name<S: Into<Cow<'static, str>>>(mut self, name: S) -> Self {
self.custom_name = Some(name.into());
self
}
pub fn shutdown_grace_period(mut self, duration: Duration) -> Self {
self.shutdown_grace_period = Some(duration);
self
}
pub fn pre_insert(mut self, process: impl Runnable) -> Self {
self.initializers
.push(Box::new(move |mgr: &mut ProcessManager| {
mgr.insert(process);
}));
self
}
pub fn build(self) -> ProcessManager {
let mut mgr = ProcessManager::new();
mgr.auto_cleanup = self.auto_cleanup;
mgr.custom_name = self.custom_name;
if let Some(duration) = self.shutdown_grace_period {
mgr.set_shutdown_grace_period(duration);
}
for init in self.initializers {
init(&mut mgr);
}
mgr
}
}