diff --git a/README.md b/README.md index d01781e..68aacc7 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,9 @@ Help: `retry -?` - Usage: retry [options] -e execute command + Usage: retry [options] [-f fail_script +commands] -e execute command -h, -?, --help + -f Execute fail script after all retries are exhausted -t, --tries=# Set max retries: Default 10 -s, --sleep=secs Constant sleep amount (seconds) -m, --min=secs Exponenetial Backoff: minimum sleep amount (seconds): Default 0.3 @@ -55,6 +56,7 @@ Test functionality: y u no work Before retry #10: sleeping 60.0 seconds y u no work + etc.. Limit retries: @@ -69,6 +71,7 @@ Limit retries: y u no work Before retry #4: sleeping 2.4 seconds y u no work + Retries exhausted Bad command: @@ -76,6 +79,20 @@ Bad command: Command Failed: poop +Fail command: + +`retry -t 3 -f echo "oh poopsickles" -e 'echo "y u no work"; false'` + + y u no work + Before retry #1: sleeping 0.3 seconds + y u no work + Before retry #2: sleeping 0.6 seconds + y u no work + Before retry #3: sleeping 1.2 seconds + y u no work + Retries exhausted, running fail script + oh poopsickles + ### License Apache 2.0 - go nuts diff --git a/lib/retryit.rb b/lib/retryit.rb index a74d8cf..39506c9 100644 --- a/lib/retryit.rb +++ b/lib/retryit.rb @@ -16,7 +16,7 @@ class RetryIt return if args.size < 1 optparser = OptionParser.new do |opts| - opts.banner = "Usage: retry [options] -e execute command" + opts.banner = "Usage: retry [options] [-f fail_script +commands] -e execute command" opts.on("-h", "-?", "--help") do |v| puts opts @@ -58,12 +58,24 @@ class RetryIt load_options(["-?"]) end - idx = args.find_index("-e") + fail_command = nil + + idx = args.find_index("-f") || args.find_index("-e") if !idx.nil? load_options(args[0...idx]) + if (args[idx] == "-f") + e_idx = args.find_index("-e") + raise "fail script (-f) must be combined with execution script (-e)" if e_idx.nil? + raise "fail script not defined" if idx == e_idx + fail_command = args[(idx+1)..(e_idx-1)] + idx = e_idx + end args = args[(idx+1)..-1] end + #log_out("Run script #{args[0]} #{args[1..-1]}") + #log_out("Fail script #{fail_command[0]} #{fail_command[1..-1]}") unless fail_command.nil? + raise "max_tries must be greater than 0" unless @max_tries > 0 raise "minimum sleep cannot be greater than maximum sleep" unless @max_sleep >= @min_sleep raise "unknown execute command" unless args.size > 0 @@ -82,7 +94,16 @@ class RetryIt attempts += 1 end - log_out("Command Failed: #{args[0]}") if success.nil? + if success.nil? + log_out("Command Failed: #{args[0]}") + elsif attempts > @max_tries + if !fail_command.nil? + log_out("Retries exhausted, running fail script") + system(fail_command[0], *fail_command[1..-1]) + else + log_out("Retries exhausted") + end + end exit process.exitstatus end