Sha256: aaec0b9d016b230e56179bdb817a785972ec198578a71b34eb36fd0b14422eb6

Contents?: true

Size: 917 Bytes

Versions: 8

Compression:

Stored size: 917 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)
      tries ||= 1
      yield
    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

8 entries across 8 versions & 1 rubygems

Version Path
imap-backup-15.0.1 lib/imap/backup/retry_on_error.rb
imap-backup-15.0.0 lib/imap/backup/retry_on_error.rb
imap-backup-14.6.1 lib/imap/backup/retry_on_error.rb
imap-backup-14.6.0 lib/imap/backup/retry_on_error.rb
imap-backup-14.5.2 lib/imap/backup/retry_on_error.rb
imap-backup-14.5.1 lib/imap/backup/retry_on_error.rb
imap-backup-14.5.0 lib/imap/backup/retry_on_error.rb
imap-backup-14.4.5 lib/imap/backup/retry_on_error.rb