Class: PerfectRetry
- Inherits:
-
Object
show all
- Defined in:
- lib/perfect_retry.rb,
lib/perfect_retry/config.rb,
lib/perfect_retry/version.rb
Defined Under Namespace
Classes: Config, TooManyRetry
Constant Summary
collapse
- REGISTERED_CONFIG =
{ }
- DEFAULTS =
{
limit: 5,
rescues: [StandardError],
dont_rescues: [],
logger: Logger.new(STDERR),
log_level: :info,
sleep: lambda{|n| n ** 2},
ensure: lambda{},
}.freeze
- VERSION =
"0.3.2".freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(config_key = nil, &block) ⇒ PerfectRetry
Returns a new instance of PerfectRetry.
43
44
45
46
47
|
# File 'lib/perfect_retry.rb', line 43
def initialize(config_key = nil, &block)
@config = REGISTERED_CONFIG[config_key] || default_config
block.call(@config) if block_given?
@config.set_log_level
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
41
42
43
|
# File 'lib/perfect_retry.rb', line 41
def config
@config
end
|
Class Method Details
.deregister_all ⇒ Object
37
38
39
|
# File 'lib/perfect_retry.rb', line 37
def self.deregister_all
REGISTERED_CONFIG.clear
end
|
.register(name, &block) ⇒ Object
.registered_config(key) ⇒ Object
33
34
35
|
# File 'lib/perfect_retry.rb', line 33
def self.registered_config(key)
REGISTERED_CONFIG[key]
end
|
.registered_config_all ⇒ Object
29
30
31
|
# File 'lib/perfect_retry.rb', line 29
def self.registered_config_all
REGISTERED_CONFIG
end
|
.with_retry(config_key = nil, &block) ⇒ Object
19
20
21
|
# File 'lib/perfect_retry.rb', line 19
def self.with_retry(config_key = nil, &block)
new(config_key).with_retry(&block)
end
|
Instance Method Details
#should_retry?(count) ⇒ Boolean
81
82
83
84
|
# File 'lib/perfect_retry.rb', line 81
def should_retry?(count)
return true unless config.limit
count < config.limit
end
|
#sleep_before_retry(count) ⇒ Object
77
78
79
|
# File 'lib/perfect_retry.rb', line 77
def sleep_before_retry(count)
sleep config.sleep_sec(count)
end
|
#with_retry(&block) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/perfect_retry.rb', line 53
def with_retry(&block)
count = 0
begin
retry_with_catch(count, &block)
rescue *config.dont_rescues => e
raise e
rescue *config.rescues => e
e.backtrace.each do |line|
config.logger.debug line
end
if should_retry?(count)
count += 1
config.logger.warn "[#{count}/#{config.limit || "Infinitiy"}] Retrying after #{config.sleep_sec(count)} seconds. Ocurred: #{e}(#{e.class})"
sleep_before_retry(count)
retry
end
raise TooManyRetry.new("too many retry (#{config.limit} times)")
ensure
config.ensure.call
end
end
|