Module: MailFetcher::RetryHelper

Included in:
GmailClient
Defined in:
lib/mail_fetcher/retry_helper.rb

Instance Method Summary collapse

Instance Method Details

#eventually(options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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