Sha256: bfdcc5b1b558f1091896718eddc8f679e48e782080e7bc58544a588a9492fb37

Contents?: true

Size: 1.21 KB

Versions: 11

Compression:

Stored size: 1.21 KB

Contents

module RocketJob
  class ThrottleDefinitions
    attr_accessor :throttles

    def initialize
      @throttles = []
    end

    def add(method_name, filter)
      unless filter.is_a?(Symbol) || filter.is_a?(Proc)
        raise(ArgumentError, "Filter for #{method_name} must be a Symbol or Proc")
      end
      raise(ArgumentError, "Cannot define #{method_name} twice, undefine previous throttle first") if exist?(method_name)

      @throttles += [ThrottleDefinition.new(method_name, filter)]
    end

    # Undefine a previously defined throttle
    def remove(method_name)
      throttles.delete_if { |throttle| throttle.method_name == method_name }
    end

    # Has a throttle been defined?
    def exist?(method_name)
      throttles.any? { |throttle| throttle.method_name == method_name }
    end

    # Returns the matching filter,
    # or nil if no throttles were triggered.
    def matching_filter(job, *args)
      throttles.each do |throttle|
        next unless throttle.throttled?(job, *args)

        return throttle.extract_filter(job, *args)
      end
      nil
    end

    def deep_dup
      new_defination           = dup
      new_defination.throttles = throttles.map(&:dup)
      new_defination
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rocketjob-6.3.1 lib/rocket_job/throttle_definitions.rb
rocketjob-6.3.0 lib/rocket_job/throttle_definitions.rb
rocketjob-6.2.0 lib/rocket_job/throttle_definitions.rb
rocketjob-6.1.1 lib/rocket_job/throttle_definitions.rb
rocketjob-6.1.0 lib/rocket_job/throttle_definitions.rb
rocketjob-6.0.3 lib/rocket_job/throttle_definitions.rb
rocketjob-6.0.2 lib/rocket_job/throttle_definitions.rb
rocketjob-6.0.1 lib/rocket_job/throttle_definitions.rb
rocketjob-6.0.0 lib/rocket_job/throttle_definitions.rb
rocketjob-6.0.0.rc3 lib/rocket_job/throttle_definitions.rb
rocketjob-6.0.0.rc2 lib/rocket_job/throttle_definitions.rb