Sha256: 519eb44ca5666bff47e62e85e1ed4f67ce4f61de6eb6b9a89817c4f92988af23

Contents?: true

Size: 1.63 KB

Versions: 3

Compression:

Stored size: 1.63 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 = yield
          return result unless condition.call
          sleep(Sufia.config.retry_unless_sleep) if Sufia.config.retry_unless_sleep > 0
        end
        raise "retry_unless could not complete successfully. Try upping the # of tries?"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sufia-models-6.7.0 lib/sufia/models/utils.rb
sufia-models-6.6.1 lib/sufia/models/utils.rb
sufia-models-6.6.0 lib/sufia/models/utils.rb