Class: Tretry
- Inherits:
-
Object
- Object
- Tretry
- Defined in:
- lib/tretry.rb
Overview
A library for doing retries in Ruby with timeouts, analysis of errors, waits between tries and more.
Constant Summary collapse
- VALID_KEYS =
Valid keys that can be given as argument for the method ‘try’.
[:tries, :timeout, :wait, :interrupt, :exit, :errors, :return_error]
Instance Attribute Summary collapse
-
#fails ⇒ Object
readonly
Returns the value of attribute fails.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#tries ⇒ Object
Returns the value of attribute tries.
-
#wait ⇒ Object
Returns the value of attribute wait.
Class Method Summary collapse
-
.try(args = {}, &block) ⇒ Object
Runs a block of code a given amount of times until it succeeds.
Instance Method Summary collapse
- #before_retry(&block) ⇒ Object
-
#initialize(args = {}) ⇒ Tretry
constructor
A new instance of Tretry.
- #try(&block) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Tretry
Returns a new instance of Tretry.
21 22 23 24 25 26 27 |
# File 'lib/tretry.rb', line 21 def initialize(args = {}) @args = args @fails = [] @before_retry = [] parse_arguments end |
Instance Attribute Details
#fails ⇒ Object (readonly)
Returns the value of attribute fails.
3 4 5 |
# File 'lib/tretry.rb', line 3 def fails @fails end |
#timeout ⇒ Object
Returns the value of attribute timeout.
4 5 6 |
# File 'lib/tretry.rb', line 4 def timeout @timeout end |
#tries ⇒ Object
Returns the value of attribute tries.
4 5 6 |
# File 'lib/tretry.rb', line 4 def tries @tries end |
#wait ⇒ Object
Returns the value of attribute wait.
4 5 6 |
# File 'lib/tretry.rb', line 4 def wait @wait end |
Class Method Details
.try(args = {}, &block) ⇒ Object
Runs a block of code a given amount of times until it succeeds.
Examples
res = Tretry.try(:tries => 3) do
#something that often fails
end
puts "Tries: '#{res[:tries]}'."
puts "Result: '#{res[:result}'."
17 18 19 |
# File 'lib/tretry.rb', line 17 def self.try(args = {}, &block) Tretry.new(args).try(&block) end |
Instance Method Details
#before_retry(&block) ⇒ Object
29 30 31 |
# File 'lib/tretry.rb', line 29 def before_retry(&block) @before_retry << block end |
#try(&block) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 |
# File 'lib/tretry.rb', line 33 def try(&block) raise "No block given." unless block @block = block @tries.times do |count| @count = count unless first_try? # Sleep for a given amount of time if the 'wait'-argument is given. sleep(@wait) if @wait call_before_retry(error: @error) @error = nil end begin # If a timeout-argument has been given, then run the code through the timeout. if @timeout try_with_timeout else # Else call block normally. @res = @block.call @dobreak = true end rescue Exception => e handle_error(e) end if @doraise if @args[:return_error] @fails << {error: @error} return { fails: @fails, error: true } else raise @error end elsif @error @fails << {error: @error} end break if @dobreak end return { fails: @fails, result: @res, error: false } end |