0
0
mirror of https://github.com/thegeeklab/retry.git synced 2024-11-21 23:20:40 +00:00

Merge pull request #2 from kadwanev/failscript

add fail script functionality
This commit is contained in:
Neville Kadwa 2015-06-23 12:07:18 -04:00
commit 4febe3b860
2 changed files with 42 additions and 4 deletions

View File

@ -15,8 +15,9 @@ Help:
`retry -?` `retry -?`
Usage: retry [options] -e execute command Usage: retry [options] [-f fail_script +commands] -e execute command
-h, -?, --help -h, -?, --help
-f Execute fail script after all retries are exhausted
-t, --tries=# Set max retries: Default 10 -t, --tries=# Set max retries: Default 10
-s, --sleep=secs Constant sleep amount (seconds) -s, --sleep=secs Constant sleep amount (seconds)
-m, --min=secs Exponenetial Backoff: minimum sleep amount (seconds): Default 0.3 -m, --min=secs Exponenetial Backoff: minimum sleep amount (seconds): Default 0.3
@ -55,6 +56,7 @@ Test functionality:
y u no work y u no work
Before retry #10: sleeping 60.0 seconds Before retry #10: sleeping 60.0 seconds
y u no work y u no work
etc..
Limit retries: Limit retries:
@ -69,6 +71,7 @@ Limit retries:
y u no work y u no work
Before retry #4: sleeping 2.4 seconds Before retry #4: sleeping 2.4 seconds
y u no work y u no work
Retries exhausted
Bad command: Bad command:
@ -76,6 +79,20 @@ Bad command:
Command Failed: poop 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 ### License
Apache 2.0 - go nuts Apache 2.0 - go nuts

View File

@ -16,7 +16,7 @@ class RetryIt
return if args.size < 1 return if args.size < 1
optparser = OptionParser.new do |opts| 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| opts.on("-h", "-?", "--help") do |v|
puts opts puts opts
@ -58,12 +58,24 @@ class RetryIt
load_options(["-?"]) load_options(["-?"])
end end
idx = args.find_index("-e") fail_command = nil
idx = args.find_index("-f") || args.find_index("-e")
if !idx.nil? if !idx.nil?
load_options(args[0...idx]) 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] args = args[(idx+1)..-1]
end 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 "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 "minimum sleep cannot be greater than maximum sleep" unless @max_sleep >= @min_sleep
raise "unknown execute command" unless args.size > 0 raise "unknown execute command" unless args.size > 0
@ -82,7 +94,16 @@ class RetryIt
attempts += 1 attempts += 1
end 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 exit process.exitstatus
end end