Sha256: 5180d5fc1c598a56e645dcf060fd73456f7b536eec749de82c526e8cb4d2d8d9

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

# This module includes utility methods
module Sufia
  module Utils
    extend ActiveSupport::Concern

    # retry the block if the conditional call is true unless we hit the maximum tries
    #
    # @param number_of_tries [enumerator] maximum number of times to retry the block (eg. 7.times)
    # @param condition [#call] conditional to call and see if we SHOULD retry
    # @yeild block [] code you want to run and retry
    #
    # @return result of the block call
    def retry_unless(number_of_tries, condition, &block)
      self.class.retry_unless(number_of_tries, condition, &block)
    end

    module ClassMethods

      # retry the block if the conditional call is true unless we hit the maximum tries
      #
      # @param number_of_tries [enumerator] maximum number of times to retry the block
      # @param condition [#call] conditional to call and see if we SHOULD retry
      # @yeild block [] code you want to run and retry
      #
      # @return result of the block call
      def retry_unless(number_of_tries, condition, &block)
        raise ArgumentError, "First argument must be an enumerator" unless number_of_tries.is_a? Enumerator
        raise ArgumentError, "Second argument must be a lambda" unless condition.respond_to? :call
        raise ArgumentError, "Must pass a block of code to retry" unless block_given?
        number_of_tries.each do
          result = block.call
          return result unless condition.call
          sleep(Sufia.config.retry_unless_sleep) if Sufia.config.retry_unless_sleep > 0
        end
        raise RuntimeError, "retry_unless could not complete successfully. Try upping the # of tries?"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sufia-models-6.2.0 lib/sufia/models/utils.rb
sufia-models-6.1.0 lib/sufia/models/utils.rb