Module: Retryable

Defined in:
lib/retryable.rb,
lib/retryable/version.rb,
lib/retryable/configuration.rb

Defined Under Namespace

Modules: Version Classes: Configuration

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

The configuration object.

See Also:



30
31
32
# File 'lib/retryable.rb', line 30

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Call this method to modify defaults in your initializers.

Examples:

Retryable.configure do |config|
  config.contexts     = {}
  config.ensure       = proc {}
  config.exception_cb = proc {}
  config.matching     = /.*/
  config.on           = StandardError
  config.sleep        = 1
  config.tries        = 2
  config.not          = []
  config.sleep_method = ->(seconds) { Kernel.sleep(seconds) }
end

Yields:



24
25
26
# File 'lib/retryable.rb', line 24

def configure
  yield(configuration)
end

.disableObject



51
52
53
# File 'lib/retryable.rb', line 51

def disable
  configuration.disable
end

.enableObject



47
48
49
# File 'lib/retryable.rb', line 47

def enable
  configuration.enable
end

.enabled?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/retryable.rb', line 43

def enabled?
  configuration.enabled?
end

.retryable(options = {}) ⇒ 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
# File 'lib/retryable.rb', line 55

def retryable(options = {})
  opts = configuration.to_hash

  check_for_invalid_options(options, opts)
  opts.merge!(options)

  return if opts[:tries] == 0

  on_exception = [opts[:on]].flatten
  not_exception = [opts[:not]].flatten
  tries = opts[:tries]
  retries = 0
  retry_exception = nil

  begin
    return yield retries, retry_exception
  rescue *not_exception
    raise
  rescue *on_exception => exception
    raise unless configuration.enabled?
    raise unless exception.message =~ opts[:matching]
    raise if tries != :infinite && retries + 1 >= tries

    # Interrupt Exception could be raised while sleeping
    begin
      seconds = opts[:sleep].respond_to?(:call) ? opts[:sleep].call(retries) : opts[:sleep]
      opts[:sleep_method].call(seconds)
    rescue *not_exception
      raise
    rescue *on_exception
    end

    retries += 1
    retry_exception = exception
    opts[:exception_cb].call(retry_exception)
    retry
  ensure
    opts[:ensure].call(retries)
  end
end

.with_context(context_key, options = {}, &block) ⇒ Object Also known as: retryable_with_context



34
35
36
37
38
39
# File 'lib/retryable.rb', line 34

def with_context(context_key, options = {}, &block)
  unless configuration.contexts.keys.include? context_key
    raise ArgumentError, "#{context_key} not found in Retryable.configuration.contexts. Available contexts: #{configuration.contexts.keys}"
  end
  retryable(configuration.contexts[context_key].merge(options), &block) if block
end