Sha256: b43d0db3dc96b581b2475bf129df870a0f45f1b9b8d2369f5f19658619b899ee

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

require 'timeout'

module Unbreakable
  module Decorators
    # Catches timeouts and retries with exponential backoff. To configure:
    #
    #     scraper.configure do |c|
    #       c.datastore.retry_limit = 5 # the maximum number of retries
    #       c.datastore.timeout_length = 60 # the timeout length
    #     end
    #
    module Timeout
      # @param object an object
      def self.extended(obj)
        obj.class.instance_eval do
          configurable_attr :retry_limit, 5
          configurable_attr :timeout_length, 60
        end
      end

    private

      # (see DataStorage::FileDataStore#yield_block)
      def yield_block(relative_path)
        retry_attempt = 0
        begin
          retry_attempt += 1
          ::Timeout::timeout(timeout_length) do
            super
          end
        rescue ::Timeout::Error
          if retry_attempt < retry_limit
            log.warn "Timeout on #{relative_path}, retrying in #{retry_delay} (#{retry_attempt}/#{retry_limit})"
            sleep retry_delay
            retry
          else
            log.error "Timeout on #{relative_path}, skipping"
          end
        end
      end

      def retry_delay(retry_attempt)
        2 ** retry_attempt
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
unbreakable-0.0.4 lib/unbreakable/decorators/timeout.rb
unbreakable-0.0.3 lib/unbreakable/decorators/timeout.rb
unbreakable-0.0.2 lib/unbreakable/decorators/timeout.rb
unbreakable-0.0.1 lib/unbreakable/decorators/timeout.rb