From 2619da44db3c61e9e44e1110b3d0e2fb3f6e10cd Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Sat, 7 Jun 2025 17:01:25 +0100 Subject: Don't die if children fail --- src/main.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6ccafba..3f7e188 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use std::{ }; use anyhow::Context; -use rumqttc::{Event::Incoming, Packet::Publish, QoS}; +use rumqttc::{Event::Incoming, Packet, Publish, QoS}; mod config; @@ -95,6 +95,17 @@ impl Node { } } +fn run>(path: P, message: &Publish) -> anyhow::Result<()> { + let mut proc = Command::new(path.as_ref()) + .args([&message.topic]) + .stdin(Stdio::piped()) + .spawn()?; + let stdin = proc.stdin.as_mut().context("No stdin")?; + stdin.write_all(&message.payload)?; + println!("{}", proc.wait()?); + Ok(()) +} + fn main() -> anyhow::Result<()> { let mut conf_path: PathBuf = option_env!("SYSCONFDIR").unwrap_or("/usr/local/etc").into(); conf_path.push(format!("{PROGRAM}.toml")); @@ -113,16 +124,11 @@ fn main() -> anyhow::Result<()> { }); for notification in connection.iter() { match notification? { - Incoming(Publish(p)) => root.publish(&p.topic, &mut |node| { + Incoming(Packet::Publish(p)) => root.publish(&p.topic, &mut |node| { for e in &node.executables { - let mut proc = Command::new(e) - .args([&p.topic]) - .stdin(Stdio::piped()) - .spawn() - .unwrap(); - let stdin = proc.stdin.as_mut().unwrap(); - stdin.write_all(&p.payload).unwrap(); - println!("{}", proc.wait().unwrap()); + if let Err(err) = run(e, &p) { + println!("Error running {e:?}: {err:?}"); + } } }), _ => (), -- cgit v1.2.3-70-g09d2