module MailFetcher::RetryHelper

Public Instance Methods

eventually(options = {}) { |i| ... } click to toggle source
# File lib/mail_fetcher/retry_helper.rb, line 3
def eventually(options = {})
  options = {:tries => 10, :delay => 1, :catch => []}.merge(options)
  last_error = nil
  start_time = Time.now.to_i
  options[:tries].times do |i|
    begin
      result = yield i
      return result if result
    rescue => e
      raise e unless Array(options[:catch]).any? { |type| e.is_a?(type) }
      last_error = e
    end
    timeout = options[:timeout] || (options[:tries] * options[:delay])
    if (Time.now.to_i - start_time) > timeout
      break
    else
      sleep(options[:delay])
    end
  end
  raise last_error if last_error
  nil
end