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 pub fn with_restart_strategy(mut self, strategy: RestartStrategy) -> Self {
54 self.restart_strategy = strategy;
55 self
56 }
57
58 pub fn with_restart_intensity(mut self, intensity: RestartIntensity) -> Self {
60 self.restart_intensity = intensity;
61 self
62 }
63
64 pub fn with_worker(
67 mut self,
68 id: impl Into<String>,
69 factory: impl Fn() -> W + Send + Sync + 'static,
70 restart_policy: RestartPolicy,
71 ) -> Self {
72 self.children.push(ChildSpec::Worker(WorkerSpec::new(
73 id,
74 factory,
75 restart_policy,
76 )));
77 self
78 }
79
80 pub fn with_supervisor(mut self, supervisor: SupervisorSpec<W>) -> Self {
82 self.children
83 .push(ChildSpec::Supervisor(Arc::new(supervisor)));
84 self
85 }
86}