Class: Retrier
- Inherits:
-
Object
- Object
- Retrier
- Defined in:
- lib/retrier.rb
Instance Method Summary collapse
-
#initialize(options = {}, &block) ⇒ Retrier
constructor
A new instance of Retrier.
Constructor Details
#initialize(options = {}, &block) ⇒ Retrier
Returns a new instance of Retrier.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/retrier.rb', line 2 def initialize(={}, &block) max_tries = .fetch(:max_tries, 10) rescuable = Array(.fetch(:rescue, StandardError)) handlers = [:handlers] attempts = 0 begin attempts += 1 return block.call(attempts) rescue *rescuable => exception if handlers handlers.each do |ex,handler| handler.call(exception) if Kernel.const_get(ex) == exception.class end elsif attempts >= max_tries raise exception else retry end end end |