diff options
author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2025-06-27 17:46:13 +0100 |
---|---|---|
committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2025-06-27 17:46:13 +0100 |
commit | 214bf200d55575acacf03a83495ea65a0eda6e0d (patch) | |
tree | 8b785263e461fe81311115da27ac229bff251cfe | |
parent | 1fde6642082ece4b6c64e29f2983e0f988ce2b59 (diff) | |
download | mqttr-214bf200d55575acacf03a83495ea65a0eda6e0d.tar.gz mqttr-214bf200d55575acacf03a83495ea65a0eda6e0d.tar.xz mqttr-214bf200d55575acacf03a83495ea65a0eda6e0d.zip |
Process timeout after 60 seconds
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | src/main.rs | 10 |
3 files changed, 8 insertions, 6 deletions
@@ -10,5 +10,5 @@ edition = "2021" anyhow = "1.0.98" rumqttc = "0.24.0" serde = { version = "1.0.219", features = ["derive"] } -tokio = { version = "1.45.1", features = ["rt", "macros", "process"] } +tokio = { version = "1.45.1", features = ["rt", "macros", "process", "time"] } toml = { version = "0.8.22", default-features = false, features = ["parse"] } @@ -73,7 +73,7 @@ it being ran every time a new MQTT message is published to this topic. ## Missing Features -* Timeouts (existent, configurable, per process configurable) +* Configurable timeouts (eventually configurable per process) * Permission checks on `mqttr.toml` if it contains a password (to ensure the password isn't being exposed) * Configurable QoS for each subscription (default is 0 (at most once)) diff --git a/src/main.rs b/src/main.rs index bc9a7b5..cf451dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,11 +3,11 @@ // TODO: Log levels -use std::{path::PathBuf, process::Stdio}; +use std::{path::PathBuf, process::Stdio, time::Duration}; use anyhow::Context; use rumqttc::{Event::Incoming, Packet, Publish, QoS}; -use tokio::{io::AsyncWriteExt, process::Command}; +use tokio::{io::AsyncWriteExt, process::Command, time::timeout}; mod config; @@ -87,8 +87,10 @@ async fn main() -> anyhow::Result<()> { let program = program.clone(); let p = p.clone(); tokio::spawn(async move { - if let Err(e) = run(&program, &p).await { - eprintln!("error: Failed to run {program:?}: {e:?}"); + match timeout(Duration::from_secs(60), run(&program, &p)).await { + Err(_) => eprintln!("error: Execution of {program:?} for message {p:?} timed out"), + Ok(Err(e)) => eprintln!("error: Failed to run {program:?}: {e:?}"), + _ => (), } }); } |