implement before and after config options; remove extra PartialEq, Eq, Hash impl

This commit is contained in:
yggverse 2026-04-07 14:33:21 +03:00
parent c297ccfd44
commit f3d69c3ef9
4 changed files with 57 additions and 2 deletions

View file

@ -9,6 +9,18 @@ sleep = 1
# * unset or comment to run once
# update = 60
# Run specified commands or scripts before and after each queue iteration
# * `exec` is required
# * `stdout_contains` is optional
# * will panic if `stdout_contains` is defined and does not match expected stdout
# [before]
# exec = ""
# stdout_contains = [""]
# [after]
# exec = ""
# stdout_contains = [""]
# Channels queue config
[channel.test]

View file

@ -4,6 +4,12 @@ use channel::Channel;
use serde::Deserialize;
use std::{collections::HashMap, path::PathBuf};
#[derive(Deserialize)]
pub struct Command {
pub exec: String,
pub stdout_contains: Option<Vec<String>>,
}
#[derive(Deserialize)]
pub struct Config {
pub channel: HashMap<String, Channel>,
@ -15,4 +21,10 @@ pub struct Config {
/// Iterator delay in seconds (prevents server abuse)
/// * None to run asap
pub sleep: Option<u64>,
/// Run specified commands or scripts before each queue iteration
/// * will panic if `stdout_contains` is defined and does not match expected stdout
pub before: Option<Command>,
/// Run specified commands or scripts after each queue iteration
/// * will panic if `stdout_contains` is defined and does not match expected stdout
pub after: Option<Command>,
}

View file

@ -1,6 +1,6 @@
use serde::Deserialize;
#[derive(Deserialize, PartialEq, Eq, Hash)]
#[derive(Deserialize)]
pub struct Item {
pub exec: String,
pub stdout_contains: Option<Vec<String>>,

View file

@ -36,6 +36,10 @@ async fn main() {
let rp = RustyPipe::new();
loop {
if let Some(ref before) = config.before {
debug!("exec {} before starting queue...", before.exec);
cmd(before)
}
info!("begin {} channels update...", config.channel.len());
for (c, channel) in &config.channel {
debug!("get `{c}` ({})...", channel.id);
@ -132,7 +136,7 @@ async fn main() {
}
} else {
warn!(
"unexpected exec `{cmd}` for channel `{c}`: `{:?}`",
"unexpected exec result `{cmd}` for channel `{c}`: `{:?}`",
response
)
}
@ -157,6 +161,10 @@ async fn main() {
Err(e) => warn!("can't scrape channel `{c}`: `{e}`"),
}
}
if let Some(ref after) = config.after {
debug!("exec {} after queue...", after.exec);
cmd(after)
}
match update {
Some(duration) => {
info!(
@ -172,3 +180,26 @@ async fn main() {
}
}
}
fn cmd(command: &config::Command) {
match Command::new("sh").arg("-c").arg(&command.exec).output() {
Ok(response) => {
if response.status.success() {
if command.stdout_contains.as_ref().is_none_or(|v| {
v.iter()
.all(|s| String::from_utf8_lossy(&response.stdout).contains(s))
}) {
info!("exec `{}` successful: `{:?}`", command.exec, response)
} else {
panic!(
"unexpected exec result `{}`: `{:?}`",
command.exec, response
)
}
} else {
panic!("exec `{}` failed: `{:?}`", command.exec, response)
}
}
Err(e) => panic!("can't execute `{}`: `{e}`", command.exec),
}
}