Class: RocketJob::ThrottleDefinition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name, filter) ⇒ ThrottleDefinition

Returns a new instance of ThrottleDefinition.



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

def initialize(method_name, filter)
  @method_name = method_name.to_sym
  @filter      = filter
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



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

def filter
  @filter
end

#method_nameObject (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_filter(job, *args) ⇒ Object

Returns the filter to apply to the job when the above throttle returns true.



27
28
29
30
31
32
33
34
35
# File 'lib/rocket_job/throttle_definition.rb', line 27

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.

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rocket_job/throttle_definition.rb', line 11

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