Class: Retry

Inherits:
Object
  • Object
show all
Defined in:
lib/jieshun/parking/helpers/retry.rb

Class Method Summary collapse

Class Method Details

.with_retries(options) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jieshun/parking/helpers/retry.rb', line 3

def self.with_retries(options)
  sleep_seconds = options[:sleep_seconds]
  sleep_seconds ||= 1
  max_tries = options[:max_tries]
  max_tries ||= 10
  tried_times = 0
  begin
    yield
  rescue Exception => e
    if Jieshun::Parking.logger
      Jieshun::Parking.logger.error("Failed to execute operation, business_info: #{options[:business_info]}, error: #{e}")
    end
    if tried_times < max_tries
      sleep(sleep_seconds)
      tried_times = tried_times + 1
      if Jieshun::Parking.logger
        Jieshun::Parking.logger.info("Retrying operation, business_info: #{options[:business_info]}, tried_times: #{tried_times}")
      end
      retry
    else
      Jieshun::Parking.logger.error("Retried #{tried_times} times, aborted.")
      raise RuntimeError.new("Failed to execute operation with Retrier.")
    end
  end
end