1
require 'retryable/version'
2
require 'retryable/configuration'
6
# A Retryable configuration object. Must act like a hash and return sensible
7
# values for all Retryable configuration options. See Retryable::Configuration.
8
attr_writer :configuration
10
# Call this method to modify defaults in your initializers.
13
# Retryable.configure do |config|
14
# config.ensure = Proc.new {}
15
# config.exception_cb = Proc.new {}
16
# config.matching = /.*/
17
# config.on = StandardError
25
# The configuration object.
26
# @see Retryable.configure
28
@configuration ||= Configuration.new
32
configuration.enabled?
43
def retryable(options = {}, &block)
45
:tries => self.configuration.tries,
46
:sleep => self.configuration.sleep,
47
:on => self.configuration.on,
48
:matching => self.configuration.matching,
49
:ensure => self.configuration.ensure,
50
:exception_cb => self.configuration.exception_cb
53
check_for_invalid_options(options, opts)
56
return if opts[:tries] == 0
58
on_exception, tries = [ opts[:on] ].flatten, opts[:tries]
63
return yield retries, retry_exception
64
rescue *on_exception => exception
65
raise unless configuration.enabled?
66
raise unless exception.message =~ opts[:matching]
67
raise if retries+1 >= tries
69
# Interrupt Exception could be raised while sleeping
71
Kernel.sleep opts[:sleep].respond_to?(:call) ? opts[:sleep].call(retries) : opts[:sleep]
76
retry_exception = exception
77
opts[:exception_cb].call(retry_exception)
80
opts[:ensure].call(retries)
86
def check_for_invalid_options(custom_options, default_options)
87
invalid_options = default_options.merge(custom_options).keys - default_options.keys
89
raise ArgumentError.new("[Retryable] Invalid options: #{invalid_options.join(", ")}") unless invalid_options.empty?