Module: Patriot::Util::Retry

Included in:
JobStore::InMemoryStore, JobStore::RDBJobStore, Worker::Base
Defined in:
lib/patriot/util/retry.rb

Overview

a mudule enables operations to be retried

Class Method Summary collapse

Class Method Details

.execute_with_retry(retry_config = {}, &blk) ⇒ Object

execute block and retry the block

Parameters:

  • retry_config (defaults to: {})

    :num_retry the max number of retry, :wait_time retry interval

Returns:

  • return value of the block

Raises:

  • an exception thrown from the block



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/patriot/util/retry.rb', line 10

def execute_with_retry(retry_config = {}, &blk)
  retry_config = {:num_retry => 3, :wait_time => 3}.merge(retry_config)
  e = nil
  1.upto(retry_config[:num_retry]) do |i|
    begin
      return yield
    rescue Exception => e
      if @logger
        @logger.error "fail to execute (#{i}) #{blk.to_s}"
        @logger.error e
        $@.each{|m| @logger.error m}
      end
    end 
    sleep retry_config[:wait_time]
  end
  raise e unless e.nil?
end