ash_flare/supervisor/
spec.rs1use crate::restart::{RestartIntensity, RestartPolicy, RestartStrategy};
4use crate::worker::{Worker, WorkerSpec};
5use std::sync::Arc;
6
7pub(crate) enum ChildSpec<W: Worker> {
9 Worker(WorkerSpec<W>),
10 Supervisor(Arc<SupervisorSpec<W>>),
11}
12
13pub struct SupervisorSpec<W: Worker> {
15 pub(crate) name: String,
16 pub(crate) children: Vec<ChildSpec<W>>,
17 pub(crate) restart_strategy: RestartStrategy,
18 pub(crate) restart_intensity: RestartIntensity,
19}
20
21impl<W: Worker> Clone for SupervisorSpec<W> {
22 fn clone(&self) -> Self {
23 Self {
24 name: self.name.clone(),
25 children: self.children.clone(),
26 restart_strategy: self.restart_strategy,
27 restart_intensity: self.restart_intensity,
28 }
29 }
30}
31
32impl<W: Worker> Clone for ChildSpec<W> {
33 fn clone(&self) -> Self {
34 match self {
35 ChildSpec::Worker(w) => ChildSpec::Worker(w.clone()),
36 ChildSpec::Supervisor(s) => ChildSpec::Supervisor(Arc::clone(s)),
37 }
38 }
39}
40
41impl<W: Worker> SupervisorSpec<W> {
42 pub fn new(name: impl Into<String>) -> Self {
44 Self {
45 name: name.into(),
46 children: Vec::new(),
47 restart_strategy: RestartStrategy::default(),
48 restart_intensity: RestartIntensity::default(),
49 }
50 }
51
52 #[must_use]
54 pub fn with_restart_strategy(mut self, strategy: RestartStrategy) -> Self {
55 self.restart_strategy = strategy;
56 self
57 }
58
59 #[must_use]
61 pub fn with_restart_intensity(mut self, intensity: RestartIntensity) -> Self {
62 self.restart_intensity = intensity;
63 self
64 }
65
66 #[must_use]
69 pub fn with_worker(
70 mut self,
71 id: impl Into<String>,
72 factory: impl Fn() -> W + Send + Sync + 'static,
73 restart_policy: RestartPolicy,
74 ) -> Self {
75 self.children.push(ChildSpec::Worker(WorkerSpec::new(
76 id,
77 factory,
78 restart_policy,
79 )));
80 self
81 }
82
83 #[must_use]
85 pub fn with_supervisor(mut self, supervisor: SupervisorSpec<W>) -> Self {
86 self.children
87 .push(ChildSpec::Supervisor(Arc::new(supervisor)));
88 self
89 }
90}