Sha256: 631556de5c1ad92c76559c9b1101d46433f39b43756fa276002a7ece4fce2cb7

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

module Greenjaguar
  class PolicyBuilder
    attr_accessor :count, :wait_strategy, :timeout, :logger, :strategy, :exceptions

    def initialize(&block)
      @count = 1
      @timeout = 300
      @exceptions = []
      instance_eval(&block)
    end

    def retry_times(retry_count)
      @count = retry_count
      self
    end

    def should_timeout_after(retry_timeout)
      @timeout = retry_timeout
      self
    end

    def use_logger(logger)
      @logger = logger
      self
    end

    def fail_silently
      @fail_silently = true
    end

    def fail_silently?
      @fail_silently
    end

    def never_give_up
      @count = -1
    end

    def never_give_up?
      @count == -1
    end

    def measure_time_in(time_unit)
      @strategy.time_unit = time_unit
      self
    end

    def with_strategy(wait_strategy, *args)
      @strategy = init_wait_strategy(wait_strategy, *args)
      self
    end

    def only_on_exceptions(exception_array)
      @exceptions.concat exception_array
    end

    def valid_exception?(exception)
      if @exceptions.empty?
        return true
      else
        @exceptions.each {|ex| return true if exception.class <= ex}
      end
      false
    end

    def wait
      strategy.wait
    end

    private

    def strategy
      @strategy ||= init_wait_strategy(:default)
    end

    def init_wait_strategy(wait_strategy, *args)
      case wait_strategy
        when :fibonacci
          return Greenjaguar::Strategies::FibonacciStrategy.new
        when :exponential_backoff
          return Greenjaguar::Strategies::ExponentialBackoffStrategy.new
        when :random
          return Greenjaguar::Strategies::RandomStrategy.new
        when :fixed_interval
          return Greenjaguar::Strategies::FixedIntervalStrategy.new(*args)
        else
          return Greenjaguar::Strategies::DefaultWaitStrategy.new
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
greenjaguar-0.0.3 lib/greenjaguar/policy_builder.rb