Sha256: 550051b8a25eeed9e86e798e0489835c78142c20bdc268e06584c00437623ec9

Contents?: true

Size: 827 Bytes

Versions: 4

Compression:

Stored size: 827 Bytes

Contents

module Greenjaguar
  class Retrier

    def initialize(policy, &block)
      @policy = policy
      @retry_block = block
      exec
    end

    private
    def exec
      @start_time = Time.new
      begin
        @retry_block.call
      rescue => e
        raise unless @policy.valid_exception? e
        if @policy.never_give_up?
          @policy.wait
          retry
        else
          if time_out? || retry_count_reached?
            raise unless @policy.fail_silently?
          else
            decrement_retry_count
            @policy.wait
            retry
          end
        end
      end
    end

    def time_out?
      Time.now - @start_time > @policy.timeout
    end

    def decrement_retry_count
      @policy.count -= 1
    end

    def retry_count_reached?
      @policy.count == 0
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
greenjaguar-0.0.6 lib/greenjaguar/retrier.rb
greenjaguar-0.0.5 lib/greenjaguar/retrier.rb
greenjaguar-0.0.4 lib/greenjaguar/retrier.rb
greenjaguar-0.0.3 lib/greenjaguar/retrier.rb