ubermind-daemon 0.6.7

Daemon process supervisor for ubermind - v2 alpha with native supervision
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
use crate::output::OutputCapture;
use libproc::processes::{pids_by_type, ProcFilter};
use netstat2::*;
use std::collections::HashMap;
use std::process::Stdio;
use std::sync::Arc;
use std::time::Instant;
use tokio::io::AsyncReadExt;
use tokio::process::{Child, Command};
use tokio::sync::RwLock;
use ubermind_core::config::{self, GlobalConfig};
use ubermind_core::types::*;

pub struct Supervisor {
	pub services: Arc<RwLock<HashMap<String, ManagedService>>>,
	pub config: GlobalConfig,
	pub http_port: Option<u16>,
}

pub struct ManagedService {
	#[allow(dead_code)]
	pub name: String,
	#[allow(dead_code)]
	pub dir: std::path::PathBuf,
	pub processes: HashMap<String, ManagedProcess>,
}

pub struct ManagedProcess {
	pub def: ProcessDef,
	pub state: ProcessState,
	pub output: OutputCapture,
	#[allow(dead_code)]
	pub started_at: Option<Instant>,
	pub retry_count: u32,
	cancel: Option<tokio::sync::watch::Sender<bool>>,
}

impl Supervisor {
	pub fn new(config: GlobalConfig, http_port: Option<u16>) -> Arc<Self> {
		Arc::new(Self {
			services: Arc::new(RwLock::new(HashMap::new())),
			config,
			http_port,
		})
	}

	pub async fn status(self: &Arc<Self>) -> Vec<ServiceStatus> {
		let entries = config::load_service_entries();
		let services = self.services.read().await;
		let running_pids: Vec<u32> = services
			.values()
			.flat_map(|s| s.processes.values())
			.filter_map(|mp| match &mp.state {
				ProcessState::Running { pid, .. } => Some(*pid),
				_ => None,
			})
			.collect();
		let pid_ports = listening_ports_for_pids(&running_pids);
		let mut result = Vec::new();

		for (name, entry) in &entries {
			if let Some(managed) = services.get(name) {
				let processes = managed
					.processes
					.iter()
					.map(|(pname, mp)| {
						let pid = match &mp.state {
							ProcessState::Running { pid, .. } => Some(*pid),
							_ => None,
						};
						let ports = pid
							.and_then(|p| pid_ports.get(&p))
							.cloned()
							.unwrap_or_default();
						ProcessStatus {
							name: pname.clone(),
							state: mp.state.clone(),
							pid,
							autostart: mp.def.autostart,
							ports,
						}
					})
					.collect();
				result.push(ServiceStatus {
					name: name.clone(),
					dir: entry.dir.clone(),
					processes,
				});
			} else {
				let service = config::load_service(entry, &self.config.defaults);
				let processes = service
					.processes
					.iter()
					.map(|p| ProcessStatus {
						name: p.name.clone(),
						state: ProcessState::Stopped,
						pid: None,
						autostart: p.autostart,
						ports: vec![],
					})
					.collect();
				result.push(ServiceStatus {
					name: name.clone(),
					dir: entry.dir.clone(),
					processes,
				});
			}
		}
		result
	}

	pub async fn start_service_filtered(
		self: &Arc<Self>,
		name: &str,
		all: bool,
		processes: &[String],
	) -> Result<String, String> {
		let entries = config::load_service_entries();
		let entry = entries.get(name).ok_or_else(|| format!("unknown service: {}", name))?;

		{
			let services = self.services.read().await;
			if let Some(managed) = services.get(name) {
				if managed.processes.values().any(|p| p.state.is_running()) {
					return Ok(format!("{}: already running", name));
				}
			}
		}

		let service = config::load_service(entry, &self.config.defaults);
		if service.processes.is_empty() {
			return Err(format!("{}: no processes defined (missing Procfile?)", name));
		}

		let mut managed_processes = HashMap::new();

		for proc_def in &service.processes {
			let should_start = if !processes.is_empty() {
				processes.iter().any(|p| p == &proc_def.name)
			} else if all {
				true
			} else {
				proc_def.autostart
			};

			let output = OutputCapture::new(name, &proc_def.name, self.config.logs.max_size_bytes);
			let (cancel_tx, cancel_rx) = tokio::sync::watch::channel(false);

			let mp = ManagedProcess {
				def: proc_def.clone(),
				state: ProcessState::Stopped,
				output: output.clone(),
				started_at: None,
				retry_count: 0,
				cancel: Some(cancel_tx),
			};
			managed_processes.insert(proc_def.name.clone(), mp);

			if should_start {
				let sup = Arc::clone(self);
				let service_name = name.to_string();
				let process_name = proc_def.name.clone();
				let proc_def_clone = proc_def.clone();
				let dir = entry.dir.clone();

				tokio::spawn(async move {
					run_process_loop(sup, service_name, process_name, proc_def_clone, dir, output, cancel_rx).await;
				});
			}
		}

		{
			let mut services = self.services.write().await;
			services.insert(
				name.to_string(),
				ManagedService {
					name: name.to_string(),
					dir: entry.dir.clone(),
					processes: managed_processes,
				},
			);
		}

		Ok(format!("{}: starting", name))
	}

	pub async fn stop_service(self: &Arc<Self>, name: &str) -> Result<String, String> {
		let mut services = self.services.write().await;
		let managed = services.get_mut(name).ok_or_else(|| format!("{}: not running", name))?;

		let mut any_running = false;
		for (_, mp) in managed.processes.iter_mut() {
			if mp.state.is_running() {
				any_running = true;
				if let Some(cancel) = mp.cancel.take() {
					let _ = cancel.send(true);
				}
				if let ProcessState::Running { pid, .. } = &mp.state {
					kill_process_tree(*pid);
				}
				mp.state = ProcessState::Stopped;
			}
		}

		if !any_running {
			return Ok(format!("{}: already stopped", name));
		}

		services.remove(name);
		Ok(format!("{}: stopped", name))
	}

	pub async fn reload_service_filtered(
		self: &Arc<Self>,
		name: &str,
		all: bool,
		processes: &[String],
	) -> Result<String, String> {
		let _ = self.stop_service(name).await;
		tokio::time::sleep(std::time::Duration::from_millis(200)).await;
		self.start_service_filtered(name, all, processes).await
	}

	pub async fn restart_process(self: &Arc<Self>, service: &str, process: &str) -> Result<String, String> {
		let entries = config::load_service_entries();
		let entry = entries.get(service).ok_or_else(|| format!("unknown service: {}", service))?;

		let mut services = self.services.write().await;
		let managed = services.get_mut(service).ok_or_else(|| format!("{}: not running", service))?;
		let mp = managed.processes.get_mut(process).ok_or_else(|| format!("{}/{}: not found", service, process))?;

		if let Some(cancel) = mp.cancel.take() {
			let _ = cancel.send(true);
		}
		if let ProcessState::Running { pid, .. } = &mp.state {
			kill_process_tree(*pid);
		}
		mp.state = ProcessState::Stopped;
		mp.retry_count = 0;

		let output = OutputCapture::new(service, process, self.config.logs.max_size_bytes);
		let (cancel_tx, cancel_rx) = tokio::sync::watch::channel(false);
		mp.output = output.clone();
		mp.cancel = Some(cancel_tx);

		let sup = Arc::clone(self);
		let service_name = service.to_string();
		let process_name = process.to_string();
		let proc_def = mp.def.clone();
		let dir = entry.dir.clone();

		tokio::spawn(async move {
			run_process_loop(sup, service_name, process_name, proc_def, dir, output, cancel_rx).await;
		});

		Ok(format!("{}/{}: restarting", service, process))
	}

	pub async fn kill_process(self: &Arc<Self>, service: &str, process: &str) -> Result<String, String> {
		let mut services = self.services.write().await;
		let managed = services.get_mut(service).ok_or_else(|| format!("{}: not running", service))?;
		let mp = managed.processes.get_mut(process).ok_or_else(|| format!("{}/{}: not found", service, process))?;

		if let Some(cancel) = mp.cancel.take() {
			let _ = cancel.send(true);
		}
		if let ProcessState::Running { pid, .. } = &mp.state {
			kill_process_tree(*pid);
		}
		mp.state = ProcessState::Stopped;

		Ok(format!("{}/{}: killed", service, process))
	}

	pub async fn get_output(&self, service: &str, process: Option<&str>) -> Result<OutputCapture, String> {
		let services = self.services.read().await;
		let managed = services.get(service).ok_or_else(|| format!("{}: not found", service))?;

		if let Some(proc_name) = process {
			let mp = managed.processes.get(proc_name).ok_or_else(|| format!("{}/{}: not found", service, proc_name))?;
			Ok(mp.output.clone())
		} else {
			// Return the first process's output (or we could merge them)
			managed
				.processes
				.values()
				.next()
				.map(|mp| mp.output.clone())
				.ok_or_else(|| format!("{}: no processes", service))
		}
	}

	pub async fn get_all_outputs(&self, service: &str) -> Result<Vec<(String, OutputCapture)>, String> {
		let services = self.services.read().await;
		let managed = services.get(service).ok_or_else(|| format!("{}: not found", service))?;
		Ok(managed
			.processes
			.iter()
			.map(|(name, mp)| (name.clone(), mp.output.clone()))
			.collect())
	}
}

async fn run_process_loop(
	supervisor: Arc<Supervisor>,
	service: String,
	process: String,
	def: ProcessDef,
	dir: std::path::PathBuf,
	output: OutputCapture,
	mut cancel: tokio::sync::watch::Receiver<bool>,
) {
	let mut retry_count: u32 = 0;

	loop {
		if *cancel.borrow() {
			return;
		}

		let child = spawn_process(&def, &dir).await;
		let mut child = match child {
			Ok(c) => c,
			Err(e) => {
				let msg = format!("[ubermind] failed to spawn {}/{}: {}\n", service, process, e);
				output.write(msg.as_bytes()).await;
				update_state(&supervisor, &service, &process, ProcessState::Failed { exit_code: -1 }).await;
				return;
			}
		};

		let pid = child.id().unwrap_or(0) as u32;
		let started_at = Instant::now();
		update_state(
			&supervisor,
			&service,
			&process,
			ProcessState::Running {
				pid,
				uptime_secs: 0,
			},
		)
		.await;

		if let Some(stdout) = child.stdout.take() {
			let out = output.clone();
			tokio::spawn(async move {
				pipe_output(stdout, out).await;
			});
		}
		if let Some(stderr) = child.stderr.take() {
			let out = output.clone();
			tokio::spawn(async move {
				pipe_output(stderr, out).await;
			});
		}

		// Also spawn an uptime updater
		let sup_clone = Arc::clone(&supervisor);
		let svc = service.clone();
		let proc_name = process.clone();
		let cancel_clone = cancel.clone();
		let uptime_handle = tokio::spawn(async move {
			loop {
				tokio::time::sleep(std::time::Duration::from_secs(1)).await;
				if *cancel_clone.borrow() {
					return;
				}
				let uptime = started_at.elapsed().as_secs();
				update_state(
					&sup_clone,
					&svc,
					&proc_name,
					ProcessState::Running { pid, uptime_secs: uptime },
				)
				.await;
			}
		});

		let exit_result = tokio::select! {
			status = child.wait() => status,
			_ = cancel.changed() => {
				let _ = child.kill().await;
				uptime_handle.abort();
				return;
			}
		};

		// Process exited, stop the uptime updater
		uptime_handle.abort();

		match exit_result {
			Ok(exit) if exit.success() => {
				let msg = format!("[ubermind] {}/{} exited cleanly\n", service, process);
				output.write(msg.as_bytes()).await;
				update_state(&supervisor, &service, &process, ProcessState::Stopped).await;
				return;
			}
			Ok(exit) => {
				let code = exit.code().unwrap_or(-1);
				retry_count += 1;

				if def.restart && retry_count <= def.max_retries {
					let msg = format!(
						"[ubermind] {}/{} crashed (exit {}), restarting ({}/{})\n",
						service, process, code, retry_count, def.max_retries
					);
					output.write(msg.as_bytes()).await;
					update_state(
						&supervisor,
						&service,
						&process,
						ProcessState::Crashed { exit_code: code, retries: retry_count },
					)
					.await;
					tokio::time::sleep(std::time::Duration::from_secs(def.restart_delay_secs)).await;
					continue;
				} else {
					let msg = format!(
						"[ubermind] {}/{} failed (exit {}), max retries exceeded\n",
						service, process, code
					);
					output.write(msg.as_bytes()).await;
					update_state(
						&supervisor,
						&service,
						&process,
						ProcessState::Failed { exit_code: code },
					)
					.await;
					return;
				}
			}
			Err(e) => {
				let msg = format!("[ubermind] {}/{} error: {}\n", service, process, e);
				output.write(msg.as_bytes()).await;
				update_state(&supervisor, &service, &process, ProcessState::Failed { exit_code: -1 }).await;
				return;
			}
		}
	}
}

async fn spawn_process(def: &ProcessDef, dir: &std::path::Path) -> Result<Child, String> {
	let mut cmd = Command::new("sh");
	cmd.args(["-c", &def.command])
		.current_dir(dir)
		.stdout(Stdio::piped())
		.stderr(Stdio::piped())
		// Create a new process group so we can kill the tree
		.process_group(0);

	for (key, val) in &def.env {
		cmd.env(key, val);
	}

	cmd.spawn().map_err(|e| format!("spawn failed: {}", e))
}

async fn pipe_output<R: tokio::io::AsyncRead + Unpin>(mut reader: R, output: OutputCapture) {
	let mut buf = [0u8; 4096];
	loop {
		match reader.read(&mut buf).await {
			Ok(0) => break,
			Ok(n) => output.write(&buf[..n]).await,
			Err(_) => break,
		}
	}
}

async fn update_state(supervisor: &Arc<Supervisor>, service: &str, process: &str, state: ProcessState) {
	let mut services = supervisor.services.write().await;
	if let Some(managed) = services.get_mut(service) {
		if let Some(mp) = managed.processes.get_mut(process) {
			mp.state = state;
		}
	}
}

/// Get listening TCP ports for a set of managed process PIDs.
/// First checks if the PID itself owns the port (fast path).
/// Falls back to expanding the process group to find child listeners.
fn listening_ports_for_pids(target_pids: &[u32]) -> HashMap<u32, Vec<u16>> {
	let af = AddressFamilyFlags::IPV4 | AddressFamilyFlags::IPV6;
	let proto = ProtocolFlags::TCP;
	let sockets = match get_sockets_info(af, proto) {
		Ok(s) => s,
		Err(_) => return HashMap::new(),
	};

	let mut all_ports: HashMap<u32, Vec<u16>> = HashMap::new();
	for si in &sockets {
		if let ProtocolSocketInfo::Tcp(ref tcp) = si.protocol_socket_info {
			if tcp.state == TcpState::Listen {
				for pid in &si.associated_pids {
					let ports = all_ports.entry(*pid).or_default();
					if !ports.contains(&tcp.local_port) {
						ports.push(tcp.local_port);
					}
				}
			}
		}
	}

	let mut result: HashMap<u32, Vec<u16>> = HashMap::new();
	for &pid in target_pids {
		if let Some(ports) = all_ports.get(&pid) {
			result.insert(pid, ports.clone());
			continue;
		}
		// PID is likely `sh -c ...`; check its process group for child listeners
		let group_pids = pids_by_type(ProcFilter::ByProgramGroup { pgrpid: pid })
			.unwrap_or_default();
		let mut ports: Vec<u16> = Vec::new();
		for gpid in &group_pids {
			if let Some(p) = all_ports.get(gpid) {
				for port in p {
					if !ports.contains(port) {
						ports.push(*port);
					}
				}
			}
		}
		if !ports.is_empty() {
			ports.sort();
			result.insert(pid, ports);
		}
	}
	result
}

fn kill_process_tree(pid: u32) {
	use nix::sys::signal::{killpg, Signal};
	use nix::unistd::Pid;
	let pgid = Pid::from_raw(pid as i32);
	let _ = killpg(pgid, Signal::SIGTERM);
	// Give processes a moment, then force kill
	std::thread::spawn(move || {
		std::thread::sleep(std::time::Duration::from_secs(3));
		let _ = killpg(pgid, Signal::SIGKILL);
	});
}