Sha256: d959b8adcf4ee31072fa50bad3ae33190fc0acf51aa55544249650bdb3b98a02

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

require 'version'
require 'config'

module Kernel
  def retryable(options = {}, &block)
    opts = {:tries => 2, :sleep => 1, :on => StandardError, :matching  => /.*/, :ensure => Proc.new {}}
    check_for_invalid_options(options, opts)
    opts.merge!(options)

    return if opts[:tries] == 0

    on_exception, tries = [ opts[:on] ].flatten, opts[:tries]
    retries = 0
    retry_exception = nil

    begin
      return yield retries, retry_exception
    rescue *on_exception => exception
      raise unless Retryable.enabled?
      raise unless exception.message =~ opts[:matching]
      raise if retries+1 >= opts[:tries]

      # Interrupt Exception could be raised while sleeping
      begin
        sleep opts[:sleep].respond_to?(:call) ? opts[:sleep].call(retries) : opts[:sleep]
      rescue *on_exception
      end

      retries += 1
      retry_exception = exception
      retry
    ensure
      opts[:ensure].call(retries)
    end
  end

  private

  def check_for_invalid_options(custom_options, default_options)
    invalid_options = default_options.merge(custom_options).keys - default_options.keys

    raise ArgumentError.new("[Retryable] Invalid options: #{invalid_options.join(", ")}") unless invalid_options.empty?
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
retryable-1.3.3 lib/retryable.rb
retryable-1.3.2 lib/retryable.rb