1use std::{sync::Arc, time::Duration};
2
3use srad_client::{Client, DynClient, DynEventLoop, EventLoop};
4use srad_types::Template;
5
6use crate::{
7 metric_manager::manager::{DynNodeMetricManager, NoMetricManager, NodeMetricManager},
8 node::TemplateRegistry,
9 EoN, NodeHandle,
10};
11
12pub struct EoNBuilder {
14 pub(crate) group_id: Option<String>,
15 pub(crate) node_id: Option<String>,
16 pub(crate) eventloop_client: (Box<DynEventLoop>, Arc<DynClient>),
17 pub(crate) metric_manager: Box<DynNodeMetricManager>,
18 pub(crate) node_rebirth_request_cooldown: Duration,
19 pub(crate) templates: TemplateRegistry,
20}
21
22impl EoNBuilder {
23 pub fn new<E: EventLoop + Send + 'static, C: Client + Send + Sync + 'static>(
27 eventloop: E,
28 client: C,
29 ) -> Self {
30 Self {
31 group_id: None,
32 node_id: None,
33 eventloop_client: (Box::new(eventloop), Arc::new(client)),
34 metric_manager: Box::new(NoMetricManager::new()),
35 node_rebirth_request_cooldown: Duration::from_secs(5),
36 templates: TemplateRegistry::new(),
37 }
38 }
39
40 pub fn with_group_id<S: Into<String>>(mut self, group_id: S) -> Self {
44 self.group_id = Some(group_id.into());
45 self
46 }
47
48 pub fn with_node_id<S: Into<String>>(mut self, node_id: S) -> Self {
52 self.node_id = Some(node_id.into());
53 self
54 }
55
56 pub fn with_rebirth_cmd_cooldown(mut self, cooldown: Duration) -> Self {
58 self.node_rebirth_request_cooldown = cooldown;
59 self
60 }
61
62 pub fn with_metric_manager<M: NodeMetricManager + Send + Sync + 'static>(
66 mut self,
67 metric_manager: M,
68 ) -> Self {
69 self.metric_manager = Box::new(metric_manager);
70 self
71 }
72
73 pub fn register_template<T: Template>(mut self) -> Self {
79 if self.templates.register::<T>().is_err() {
80 panic!(
81 "Template with duplicate metric definition name registered. name={}",
82 T::template_definition_metric_name()
83 )
84 }
85 self
86 }
87
88 pub fn build(self) -> Result<(EoN, NodeHandle), String> {
94 EoN::new_from_builder(self)
95 }
96}