Class: RocketJob::ThrottleDefinitions
- Inherits:
-
Object
- Object
- RocketJob::ThrottleDefinitions
- Defined in:
- lib/rocket_job/throttle_definitions.rb
Instance Attribute Summary collapse
-
#throttles ⇒ Object
Returns the value of attribute throttles.
Instance Method Summary collapse
- #add(method_name, filter, description = nil) ⇒ Object
- #deep_dup ⇒ Object
-
#exist?(method_name) ⇒ Boolean
Has a throttle been defined?.
-
#initialize ⇒ ThrottleDefinitions
constructor
A new instance of ThrottleDefinitions.
-
#matching_filter(job) ⇒ Object
Returns the matching filter, or nil if no throttles were triggered.
-
#matching_throttle(job, *args) ⇒ Object
Returns [ThrottleDefinition] the first throttle that was triggered, or nil if no throttles were triggered.
-
#remove(method_name) ⇒ Object
Undefine a previously defined throttle.
Constructor Details
#initialize ⇒ ThrottleDefinitions
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
#throttles ⇒ Object
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, description = nil) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/rocket_job/throttle_definitions.rb', line 9 def add(method_name, filter, description = nil) 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, description)] end |
#deep_dup ⇒ Object
44 45 46 47 48 |
# File 'lib/rocket_job/throttle_definitions.rb', line 44 def deep_dup new_defination = dup new_defination.throttles = throttles.map(&:dup) new_defination end |
#exist?(method_name) ⇒ Boolean
Has a throttle been defined?
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) ⇒ Object
Returns the matching filter, or nil if no throttles were triggered.
40 41 42 |
# File 'lib/rocket_job/throttle_definitions.rb', line 40 def matching_filter(job, *) matching_throttle(job, *)&.extract_filter(job, *) end |
#matching_throttle(job, *args) ⇒ Object
Returns [ThrottleDefinition] the first throttle that was triggered, or nil if no throttles were triggered.
The returned definition exposes both the filter (extract_filter) and the
human readable reason (extract_description) so callers can apply the filter
and persist why the job was throttled.
34 35 36 |
# File 'lib/rocket_job/throttle_definitions.rb', line 34 def matching_throttle(job, *args) throttles.find { |throttle| throttle.throttled?(job, *args) } 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 |