Class: RocketJob::ThrottleDefinition
- Inherits:
-
Object
- Object
- RocketJob::ThrottleDefinition
- Defined in:
- lib/rocket_job/throttle_definition.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#filter ⇒ Object
readonly
Returns the value of attribute filter.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
Instance Method Summary collapse
-
#extract_description(job) ⇒ Object
Returns [String] the human readable reason why the job is throttled.
-
#extract_filter(job, *args) ⇒ Object
Returns the filter to apply to the job when the above throttle returns true.
-
#initialize(method_name, filter, description = nil) ⇒ ThrottleDefinition
constructor
Parameters: description: [String|Proc|nil] Human readable reason why the job is throttled, persisted to the job as
throttled_byand surfaced in Mission Control. -
#throttled?(job, *args) ⇒ Boolean
Returns [true|false] whether the throttle was triggered.
Constructor Details
#initialize(method_name, filter, description = nil) ⇒ ThrottleDefinition
Parameters:
description: [String|Proc|nil]
Human readable reason why the job is throttled, persisted to the job as
`throttled_by` and surfaced in Mission Control.
When a Proc, it is called with the same arguments as the throttle and must
return a String, allowing the reason to include runtime detail.
When nil, a humanized version of the method name is used.
12 13 14 15 16 |
# File 'lib/rocket_job/throttle_definition.rb', line 12 def initialize(method_name, filter, description = nil) @method_name = method_name.to_sym @filter = filter @description = description end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
3 4 5 |
# File 'lib/rocket_job/throttle_definition.rb', line 3 def description @description end |
#filter ⇒ Object (readonly)
Returns the value of attribute filter.
3 4 5 |
# File 'lib/rocket_job/throttle_definition.rb', line 3 def filter @filter end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
3 4 5 |
# File 'lib/rocket_job/throttle_definition.rb', line 3 def method_name @method_name end |
Instance Method Details
#extract_description(job) ⇒ Object
Returns [String] the human readable reason why the job is throttled.
46 47 48 49 50 51 52 |
# File 'lib/rocket_job/throttle_definition.rb', line 46 def extract_description(job, *) return description.call(job, *) if description.is_a?(Proc) return description if description # Default: humanize the method name, dropping a trailing `?` or `_exceeded`. method_name.to_s.sub(/_exceeded\?\z/, "").sub(/\?\z/, "").tr("_", " ").capitalize end |
#extract_filter(job, *args) ⇒ Object
Returns the filter to apply to the job when the above throttle returns true.
35 36 37 38 39 40 41 42 43 |
# File 'lib/rocket_job/throttle_definition.rb', line 35 def extract_filter(job, *args) return filter.call(job, *args) if filter.is_a?(Proc) if args.size.positive? job.method(filter).arity.zero? ? job.send(filter) : job.send(filter, *args) else job.send(filter) end end |
#throttled?(job, *args) ⇒ Boolean
Returns [true|false] whether the throttle was triggered.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rocket_job/throttle_definition.rb', line 19 def throttled?(job, *args) # Throttle exceeded? # Throttle methods can be private. throttled = if args.size.positive? job.method(method_name).arity.zero? ? job.send(method_name) : job.send(method_name, *args) else job.send(method_name) end return false unless throttled job.logger.debug { "Throttle: #{method_name} has been exceeded." } true end |