From 60ece288ec0bcbeee2d005f43c96267b5434063c Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Wed, 27 Mar 2019 18:51:10 +0100 Subject: [PATCH] Wait for a hook to end before starting the next one Not doing so may result in race conditions, hence breaking the promise that hooks are called in sequential order. Also, debug output has been added to the hooks. --- acmed/src/hooks.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/acmed/src/hooks.rs b/acmed/src/hooks.rs index 206a6d2..237fc64 100644 --- a/acmed/src/hooks.rs +++ b/acmed/src/hooks.rs @@ -1,6 +1,7 @@ use crate::config::Hook; use crate::errors::Error; use handlebars::Handlebars; +use log::debug; use serde::Serialize; use std::fs::File; use std::io::prelude::*; @@ -27,6 +28,7 @@ pub fn call_multiple(data: &T, hooks: &Vec) -> Result<(), Er } pub fn call(data: &T, hook: &Hook) -> Result<(), Error> { + debug!("Calling hook: {}", hook.name); let reg = Handlebars::new(); let mut v = vec![]; let args = match &hook.args { @@ -39,6 +41,8 @@ pub fn call(data: &T, hook: &Hook) -> Result<(), Error> { } None => &[], }; + debug!("Hook {}: cmd: {}", hook.name, hook.cmd); + debug!("Hook {}: args: {:?}", hook.name, args); let mut cmd = Command::new(&hook.cmd) .args(args) .stdout(get_hook_output!(&hook.stdout, reg, data)) @@ -50,8 +54,15 @@ pub fn call(data: &T, hook: &Hook) -> Result<(), Error> { .spawn()?; if hook.stdin.is_some() { let data_in = reg.render_template(&hook.stdin.to_owned().unwrap(), data)?; + debug!("Hook {}: stdin: {}", hook.name, data_in); let stdin = cmd.stdin.as_mut().ok_or("stdin not found")?; stdin.write_all(data_in.as_bytes())?; } + // TODO: add a timeout + let status = cmd.wait()?; + match status.code() { + Some(code) => debug!("Hook {}: exited with code {}", hook.name, code), + None => debug!("Hook {}: exited", hook.name), + }; Ok(()) }