aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2025-06-07 17:01:25 +0100
committerTomasz Kramkowski <tomasz@kramkow.ski>2025-06-07 17:01:25 +0100
commit2619da44db3c61e9e44e1110b3d0e2fb3f6e10cd (patch)
treee54403d8fb59d8760b0fb9d65b9fd01a0d6b9a0c /src
parentf28ba49bdaa3b5536f250c06830a160e5588af61 (diff)
downloadmqttt-2619da44db3c61e9e44e1110b3d0e2fb3f6e10cd.tar.gz
mqttt-2619da44db3c61e9e44e1110b3d0e2fb3f6e10cd.tar.xz
mqttt-2619da44db3c61e9e44e1110b3d0e2fb3f6e10cd.zip
Don't die if children fail
Diffstat (limited to 'src')
-rw-r--r--src/main.rs26
1 files 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<P: AsRef<Path>>(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:?}");
+ }
}
}),
_ => (),