Class: ActiveMessaging::Adapters::Kestrel::SimpleRetry

Inherits:
Object
  • Object
show all
Defined in:
lib/active_messaging/adapters/kestrel.rb

Overview

class for retrying things (should be pulled into a separate gem)

Instance Method Summary collapse

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(options = {})
  use_options = {:tries => 3, :delay => 5}.merge(options || {})
  exception = nil
  return_value = nil
  logger = use_options[:logger] || (defined?(::ActiveMessaging) ? ::ActiveMessaging.logger : nil)
  use_options[: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 #{use_options[:delay]} seconds") if logger
    end
    sleep use_options[:delay]
  end

  raise exception if exception
  return return_value
end