Module: Retryable
- Defined in:
- lib/retryable.rb,
lib/retryable/config.rb,
lib/retryable/version.rb
Defined Under Namespace
Classes: Version
Class Attribute Summary collapse
-
.enabled ⇒ Object
Returns the value of attribute enabled.
Class Method Summary collapse
Class Attribute Details
.enabled ⇒ Object
Returns the value of attribute enabled.
3 4 5 |
# File 'lib/retryable/config.rb', line 3 def enabled @enabled end |
Class Method Details
.disable ⇒ Object
9 10 11 |
# File 'lib/retryable/config.rb', line 9 def self.disable @enabled = false end |
.enable ⇒ Object
5 6 7 |
# File 'lib/retryable/config.rb', line 5 def self.enable @enabled = true end |
.enabled? ⇒ Boolean
13 14 15 |
# File 'lib/retryable/config.rb', line 13 def self.enabled? !!@enabled end |
.retryable(options = {}, &block) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/retryable.rb', line 5 def self.retryable( = {}, &block) opts = {:tries => 2, :sleep => 1, :on => StandardError, :matching => /.*/, :ensure => Proc.new {}, :exception_cb => Proc.new {}} (, opts) opts.merge!() return if opts[:tries] == 0 on_exception, tries = [ opts[:on] ].flatten, opts[:tries] retries = 0 retry_exception = nil begin return yield retries, retry_exception rescue *on_exception => exception raise unless Retryable.enabled? raise unless exception. =~ opts[:matching] raise if retries+1 >= tries # Interrupt Exception could be raised while sleeping begin Kernel.sleep opts[:sleep].respond_to?(:call) ? opts[:sleep].call(retries) : opts[:sleep] rescue *on_exception end retries += 1 retry_exception = exception opts[:exception_cb].call(retry_exception) retry ensure opts[:ensure].call(retries) end end |