Skip to content

Commit

Permalink
Merge pull request #25 from mstruebing/feat/22/max-retry
Browse files Browse the repository at this point in the history
feat: max-retry
  • Loading branch information
mstruebing authored Apr 15, 2022
2 parents b1a297c + d681b0b commit 145fa58
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <mxstrbng@gmail.com>
Waits (retries) a command until it exits with 0
USAGE:
waitz [FLAGS] [OPTIONS] <COMMAND>...
Expand All @@ -30,7 +26,8 @@ FLAGS:
--verbose Forwards stdout/stderr from the command to the terminal
OPTIONS:
-i, --interval <interval> in which interval the command should be retried in milliseconds [default: 1000]
-i, --interval <interval> in which interval the command should be retried in milliseconds [default: 1000]
-r, --max-retries <max-retries> Positive integer how often the command should be retried [default: 0]
ARGS:
<COMMAND>... Which command should be waited for
Expand Down
22 changes: 20 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -69,7 +79,15 @@ fn main() -> Result<()> {
.ok_or_else(|| Error::NoneError("Could not get interval".to_owned()))?
.parse::<u64>()?;

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::<u64>()?;

// no-retry has precedence
if matches.is_present("no-retry") {
max_retries = 1
}

let mut original_args = matches
.values_of("COMMAND")
Expand All @@ -85,7 +103,7 @@ fn main() -> Result<()> {
command,
args,
interval: Duration::from_millis(interval),
no_retry,
max_retries,
logger,
};

Expand Down
10 changes: 8 additions & 2 deletions src/waitz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 145fa58

Please sign in to comment.