Sha256: 1ddac8ec44e9e60e89799becf002eb7dc96338601615bbb784ff8067f518b70e

Contents?: true

Size: 1.07 KB

Versions: 13

Compression:

Stored size: 1.07 KB

Contents

module RocketJob
  class ThrottleDefinitions
    attr_reader :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
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rocketjob-6.0.0.rc1 lib/rocket_job/throttle_definitions.rb
rocketjob-5.4.1 lib/rocket_job/throttle_definitions.rb
rocketjob-5.4.0 lib/rocket_job/throttle_definitions.rb
rocketjob-5.4.0.beta2 lib/rocket_job/throttle_definitions.rb
rocketjob-5.4.0.beta1 lib/rocket_job/throttle_definitions.rb
rocketjob-5.3.3 lib/rocket_job/throttle_definitions.rb
rocketjob-5.3.2 lib/rocket_job/throttle_definitions.rb
rocketjob-5.3.1 lib/rocket_job/throttle_definitions.rb
rocketjob-5.3.0 lib/rocket_job/throttle_definitions.rb
rocketjob-5.2.0 lib/rocket_job/throttle_definitions.rb
rocketjob-5.2.0.beta3 lib/rocket_job/throttle_definitions.rb
rocketjob-5.2.0.beta2 lib/rocket_job/throttle_definitions.rb
rocketjob-5.2.0.beta1 lib/rocket_job/throttle_definitions.rb