Class: ActiveMessaging::Adapters::Kestrel::SimpleRetry
- Inherits:
-
Object
- Object
- ActiveMessaging::Adapters::Kestrel::SimpleRetry
- Defined in:
- lib/active_messaging/adapters/kestrel.rb
Overview
class for retrying things (should be pulled into a separate gem)
Instance Method Summary collapse
-
#do_work(options = {}) ⇒ Object
Yields to caller’s block and retries up to options (default 3) times in the face of exceptions.
Instance Method Details
#do_work(options = {}) ⇒ Object
Yields to caller’s block and retries up to options (default 3) times in the face of exceptions. Returns return result of block if successful. If number of tries is exhausted, the exception is reraised. Retry loop will sleep options seconds between retries (default 5). If you want error logging, pass in :logger. If not provided, ActiveMessaging.logger is used if defined.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/active_messaging/adapters/kestrel.rb', line 34 def do_work( = {}) = {:tries => 3, :delay => 5}.merge( || {}) exception = nil return_value = nil logger = [:logger] || (defined?(::ActiveMessaging) ? ::ActiveMessaging.logger : nil) [:tries].times do |try| begin exception = nil return_value = yield break rescue Exception => e exception = e logger.warn("Got error on try #{try}: #{exception} retrying after #{[:delay]} seconds") if logger end sleep [:delay] end raise exception if exception return return_value end |