diff --git a/README.md b/README.md index a196ea9..1c15d32 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,6 @@ like waiting for a specific URL to become available after booting up a server in # Usage ``` -waitz 0.2.2 -Max StrĂ¼bing -Waits (retries) a command until it exits with 0 - USAGE: waitz [FLAGS] [OPTIONS] ... @@ -30,7 +26,8 @@ FLAGS: --verbose Forwards stdout/stderr from the command to the terminal OPTIONS: - -i, --interval in which interval the command should be retried in milliseconds [default: 1000] + -i, --interval in which interval the command should be retried in milliseconds [default: 1000] + -r, --max-retries Positive integer how often the command should be retried [default: 0] ARGS: ... Which command should be waited for diff --git a/src/main.rs b/src/main.rs index e72b96a..43c7942 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,6 +33,16 @@ fn main() -> Result<()> { .required(false) .help("Don't try to rerun the command in case it fails with non-zero exit code"), ) + .arg( + Arg::with_name("max-retries") + .long("max-retries") + .short("r") + .value_name("max-retries") + .takes_value(true) + .required(false) + .default_value("0") + .help("Positive integer how often the command should be retried"), + ) .arg( Arg::with_name("verbose") .long("verbose") @@ -69,7 +79,15 @@ fn main() -> Result<()> { .ok_or_else(|| Error::NoneError("Could not get interval".to_owned()))? .parse::()?; - let no_retry = matches.is_present("no-retry"); + let mut max_retries = matches + .value_of("max-retries") + .ok_or_else(|| Error::NoneError("Could not get max retries".to_owned()))? + .parse::()?; + + // no-retry has precedence + if matches.is_present("no-retry") { + max_retries = 1 + } let mut original_args = matches .values_of("COMMAND") @@ -85,7 +103,7 @@ fn main() -> Result<()> { command, args, interval: Duration::from_millis(interval), - no_retry, + max_retries, logger, }; diff --git a/src/waitz.rs b/src/waitz.rs index 5784250..271f674 100644 --- a/src/waitz.rs +++ b/src/waitz.rs @@ -9,15 +9,21 @@ pub struct Waitz<'a> { pub command: &'a str, pub args: Vec<&'a str>, pub interval: Duration, - pub no_retry: bool, + pub max_retries: u64, pub logger: Logger, } impl Waitz<'_> { pub fn run(&self) { self.logger.debug(&format!("{:?}", &self)); + let mut runs = 0; - while !is_successful(self.command, &self.args, &self.logger) && !self.no_retry { + while (self.max_retries == 0 || runs < self.max_retries) + && !is_successful(self.command, &self.args, &self.logger) + { + runs += 1; + + self.logger.debug(&format!("Run {:?}", runs)); self.logger .debug(&format!("Wait for {:?} to run again", self.interval)); thread::sleep(self.interval);