Sha256: b7481282cf4f3bfe3185e3b7527b2f8f26b74d993381280094f2f2471ee990ca

Contents?: true

Size: 930 Bytes

Versions: 2

Compression:

Stored size: 930 Bytes

Contents

require "imap/backup/logger"

module Imap; end

module Imap::Backup
  # Provides a mechanism for retrying blocks of code which often throw errors
  module RetryOnError
    # Calls the supplied block,
    # traps the given types of errors
    # retrying up to a given number of times
    # @param errors [Array<Exception>] the exceptions to trap
    # @param limit [Integer] the maximum number of retries
    # @param on_error [Proc] a block to call when an error occurs
    # @raise any error ocurring more than `limit` times
    # @return the result of any successful completion of the block
    def retry_on_error(errors:, limit: 10, on_error: nil, &block)
      tries ||= 1
      block.call
    rescue *errors => e
      if tries < limit
        message = "#{e}, attempt #{tries} of #{limit}"
        Logger.logger.debug message
        on_error&.call
        tries += 1
        retry
      end
      raise e
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
imap-backup-15.0.3.rc1 lib/imap/backup/retry_on_error.rb
imap-backup-15.0.2 lib/imap/backup/retry_on_error.rb