Class: RetryIt
- Inherits:
-
Object
- Object
- RetryIt
- Defined in:
- lib/retryit.rb
Instance Attribute Summary collapse
-
#constant_sleep ⇒ Object
Returns the value of attribute constant_sleep.
-
#max_retries ⇒ Object
Returns the value of attribute max_retries.
-
#max_sleep ⇒ Object
Returns the value of attribute max_sleep.
-
#min_sleep ⇒ Object
Returns the value of attribute min_sleep.
Instance Method Summary collapse
-
#initialize ⇒ RetryIt
constructor
A new instance of RetryIt.
- #load_options(args) ⇒ Object
- #log_out(message) ⇒ Object
- #run(args) ⇒ Object
- #sleep_amount(attempts) ⇒ Object
Constructor Details
#initialize ⇒ RetryIt
Returns a new instance of RetryIt.
8 9 10 11 12 13 |
# File 'lib/retryit.rb', line 8 def initialize() @max_tries = 10 @min_sleep = 0.3 @max_sleep = 60.0 @constant_sleep = nil end |
Instance Attribute Details
#constant_sleep ⇒ Object
Returns the value of attribute constant_sleep.
6 7 8 |
# File 'lib/retryit.rb', line 6 def constant_sleep @constant_sleep end |
#max_retries ⇒ Object
Returns the value of attribute max_retries.
6 7 8 |
# File 'lib/retryit.rb', line 6 def max_retries @max_retries end |
#max_sleep ⇒ Object
Returns the value of attribute max_sleep.
6 7 8 |
# File 'lib/retryit.rb', line 6 def max_sleep @max_sleep end |
#min_sleep ⇒ Object
Returns the value of attribute min_sleep.
6 7 8 |
# File 'lib/retryit.rb', line 6 def min_sleep @min_sleep end |
Instance Method Details
#load_options(args) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/retryit.rb', line 15 def (args) return if args.size < 1 optparser = OptionParser.new do |opts| opts. = "Usage: retry [options] [-f fail_script +commands] -e execute command" opts.on("-h", "-?", "--help") do |v| puts opts exit end opts.on("-t#", "--tries=#", Integer, "Set max retries: Default 10") do |v| @max_tries = v end opts.on("-s#", "--sleep=secs", Float, "Constant sleep amount (seconds)") do |v| @constant_sleep = v end opts.on("-m#", "--min=secs", Float, "Exponenetial Backoff: minimum sleep amount (seconds): Default 0.3") do |v| @min_sleep = v end opts.on("-x#", "--max=secs", Float, "Exponenetial Backoff: maximum sleep amount (seconds): Default 60") do |v| @max_sleep = v end end optparser.parse(*args) end |
#log_out(message) ⇒ Object
51 52 53 |
# File 'lib/retryit.rb', line 51 def log_out() STDERR.puts() end |
#run(args) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/retryit.rb', line 55 def run(args) if (args.size < 1 || ["-h", "-?", "--help"].include?(args[0])) (["-?"]) end fail_command = nil idx = args.find_index("-f") || args.find_index("-e") if !idx.nil? (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 process = nil attempts = 0 success = false while (success == false && attempts <= @max_tries) if (attempts > 0) sleep_time = sleep_amount(attempts) log_out("Before retry ##{attempts}: sleeping #{sleep_time} seconds") sleep sleep_time end success = system(args[0], *args[1..-1]) process = $? attempts += 1 end 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 |
#sleep_amount(attempts) ⇒ Object
47 48 49 |
# File 'lib/retryit.rb', line 47 def sleep_amount(attempts) @constant_sleep || [@min_sleep * (2 ** (attempts - 1)), @max_sleep].min end |