Class: RocketJob::ThrottleDefinitions

Inherits:
Object
  • Object
show all
Defined in:
lib/rocket_job/throttle_definitions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeThrottleDefinitions

Returns a new instance of ThrottleDefinitions.



5
6
7
# File 'lib/rocket_job/throttle_definitions.rb', line 5

def initialize
  @throttles = []
end

Instance Attribute Details

#throttlesObject

Returns the value of attribute throttles.



3
4
5
# File 'lib/rocket_job/throttle_definitions.rb', line 3

def throttles
  @throttles
end

Instance Method Details

#add(method_name, filter) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
# File 'lib/rocket_job/throttle_definitions.rb', line 9

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

#deep_dupObject



39
40
41
42
43
# File 'lib/rocket_job/throttle_definitions.rb', line 39

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

#exist?(method_name) ⇒ Boolean

Has a throttle been defined?

Returns:

  • (Boolean)


24
25
26
# File 'lib/rocket_job/throttle_definitions.rb', line 24

def exist?(method_name)
  throttles.any? { |throttle| throttle.method_name == method_name }
end

#matching_filter(job, *args) ⇒ Object

Returns the matching filter, or nil if no throttles were triggered.



30
31
32
33
34
35
36
37
# File 'lib/rocket_job/throttle_definitions.rb', line 30

def matching_filter(job, *args)
  throttles.each do |throttle|
    next unless throttle.throttled?(job, *args)

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

#remove(method_name) ⇒ Object

Undefine a previously defined throttle



19
20
21
# File 'lib/rocket_job/throttle_definitions.rb', line 19

def remove(method_name)
  throttles.delete_if { |throttle| throttle.method_name == method_name }
end