Module: Retriable
- Included in:
- GaddyGaddy_Client, GaddyGaddy_Client, Notification::Send, Request
- Defined in:
- lib/utils/retriable.rb
Overview
Name:
retriable.rb
Created by: GaddyGaddy
Description:
Constant Summary collapse
- @@test_mode =
false
Class Method Summary collapse
Instance Method Summary collapse
- #extract_options(array) ⇒ Object
-
#with_retries(*args, &block) ⇒ Object
This will catch any exception and retry twice (three tries total): with_retries { … }.
Class Method Details
.set_test_mode ⇒ Object
58 59 60 |
# File 'lib/utils/retriable.rb', line 58 def self.set_test_mode @@test_mode = true end |
Instance Method Details
#extract_options(array) ⇒ Object
54 55 56 |
# File 'lib/utils/retriable.rb', line 54 def (array) array.last.is_a?(::Hash) ? array.pop : {} end |
#with_retries(*args, &block) ⇒ Object
This will catch any exception and retry twice (three tries total):
with_retries { ... }
This will catch any exception and retry four times (five tries total):
with_retries(:limit => 5) { ... }
This will catch a specific exception and retry once (two tries total):
with_retries(Some::Error, :limit => 2) { ... }
You can also sleep inbetween tries. This is helpful if you’re hoping that some external service recovers from its issues.
with_retries(Service::Error, :sleep => 1) { ... }
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/utils/retriable.rb', line 29 def with_retries(*args, &block) = args exceptions = args [:limit] ||= 3 [:limit] = 0 if @@test_mode [:sleep] ||= 0 exceptions = [Exception] if exceptions.empty? retried = 0 begin yield rescue *exceptions => e logger.warn "Could not do command, will retry. Error message is #{e.inspect.to_s}" if retried + 1 < [:limit] retried += 1 sleep [:sleep] retry else raise e end end end |