agentmux_launcher/
startup_events.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum StartupStatus {
17 Ok,
18 Warn,
19 Error,
20}
21
22#[derive(Debug)]
23pub enum StartupEvent {
24 StageBegin {
26 stage: &'static str,
27 label: &'static str,
28 },
29 StageEnd {
31 stage: &'static str,
32 duration_ms: u64,
33 status: StartupStatus,
34 detail: Option<String>,
35 },
36 SubBegin {
38 stage: &'static str,
39 id: String,
40 label: String,
41 },
42 SubEnd {
44 stage: &'static str,
45 id: String,
46 duration_ms: u64,
47 status: StartupStatus,
48 detail: Option<String>,
50 },
51}
52
53#[derive(Clone, Debug)]
60pub struct StartupEventSink {
61 tx: std::sync::Arc<std::sync::Mutex<std::sync::mpsc::Sender<StartupEvent>>>,
62}
63
64impl StartupEventSink {
65 pub fn new() -> (Self, std::sync::mpsc::Receiver<StartupEvent>) {
68 let (tx, rx) = std::sync::mpsc::channel();
69 (Self { tx: std::sync::Arc::new(std::sync::Mutex::new(tx)) }, rx)
70 }
71
72 pub fn reconnect(&self) -> std::sync::mpsc::Receiver<StartupEvent> {
75 let (tx, rx) = std::sync::mpsc::channel();
76 if let Ok(mut current) = self.tx.lock() {
77 *current = tx;
78 }
79 rx
80 }
81
82 fn send(&self, event: StartupEvent) {
83 if let Ok(tx) = self.tx.lock() {
84 let _ = tx.send(event);
85 }
86 }
87
88 pub fn stage_begin(&self, stage: &'static str, label: &'static str) {
89 self.send(StartupEvent::StageBegin { stage, label });
90 }
91
92 pub fn stage_end(
93 &self,
94 stage: &'static str,
95 duration_ms: u64,
96 status: StartupStatus,
97 detail: Option<String>,
98 ) {
99 self.send(StartupEvent::StageEnd {
100 stage,
101 duration_ms,
102 status,
103 detail,
104 });
105 }
106
107 pub fn sub_begin(
108 &self,
109 stage: &'static str,
110 id: impl Into<String>,
111 label: impl Into<String>,
112 ) {
113 self.send(StartupEvent::SubBegin {
114 stage,
115 id: id.into(),
116 label: label.into(),
117 });
118 }
119
120 pub fn sub_end(
121 &self,
122 stage: &'static str,
123 id: impl Into<String>,
124 duration_ms: u64,
125 status: StartupStatus,
126 detail: Option<String>,
127 ) {
128 self.send(StartupEvent::SubEnd {
129 stage,
130 id: id.into(),
131 duration_ms,
132 status,
133 detail,
134 });
135 }
136}
137
138#[cfg(test)]
139mod tests {
140 use super::*;
141
142 #[test]
143 fn reconnect_moves_every_clone_to_the_new_receiver() {
144 let (sink, first) = StartupEventSink::new();
145 let ipc_clone = sink.clone();
146 ipc_clone.stage_begin("host", "Host startup");
147 assert!(matches!(first.try_recv(), Ok(StartupEvent::StageBegin { stage: "host", .. })));
148
149 let second = sink.reconnect();
150 ipc_clone.stage_begin("cef_init", "CEF initialize");
151 assert!(matches!(second.try_recv(), Ok(StartupEvent::StageBegin { stage: "cef_init", .. })));
152 assert!(first.try_recv().is_err(), "the old receiver gets nothing after a reconnect");
153 }
154}