use crate::restart::{RestartIntensity, RestartPolicy, RestartStrategy};
use crate::worker::{Worker, WorkerSpec};
use std::sync::Arc;
pub(crate) enum ChildSpec<W: Worker> {
Worker(WorkerSpec<W>),
Supervisor(Arc<SupervisorSpec<W>>),
}
pub struct SupervisorSpec<W: Worker> {
pub(crate) name: String,
pub(crate) children: Vec<ChildSpec<W>>,
pub(crate) restart_strategy: RestartStrategy,
pub(crate) restart_intensity: RestartIntensity,
}
impl<W: Worker> Clone for SupervisorSpec<W> {
fn clone(&self) -> Self {
Self {
name: self.name.clone(),
children: self.children.clone(),
restart_strategy: self.restart_strategy,
restart_intensity: self.restart_intensity,
}
}
}
impl<W: Worker> Clone for ChildSpec<W> {
fn clone(&self) -> Self {
match self {
ChildSpec::Worker(w) => ChildSpec::Worker(w.clone()),
ChildSpec::Supervisor(s) => ChildSpec::Supervisor(Arc::clone(s)),
}
}
}
impl<W: Worker> SupervisorSpec<W> {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
children: Vec::new(),
restart_strategy: RestartStrategy::default(),
restart_intensity: RestartIntensity::default(),
}
}
#[must_use]
pub fn with_restart_strategy(mut self, strategy: RestartStrategy) -> Self {
self.restart_strategy = strategy;
self
}
#[must_use]
pub fn with_restart_intensity(mut self, intensity: RestartIntensity) -> Self {
self.restart_intensity = intensity;
self
}
#[must_use]
pub fn with_worker(
mut self,
id: impl Into<String>,
factory: impl Fn() -> W + Send + Sync + 'static,
restart_policy: RestartPolicy,
) -> Self {
self.children.push(ChildSpec::Worker(WorkerSpec::new(
id,
factory,
restart_policy,
)));
self
}
#[must_use]
pub fn with_supervisor(mut self, supervisor: SupervisorSpec<W>) -> Self {
self.children
.push(ChildSpec::Supervisor(Arc::new(supervisor)));
self
}
}