class RocketJob::ThrottleDefinition

Attributes

filter[R]
method_name[R]

Public Class Methods

new(method_name, filter) click to toggle source
# File lib/rocket_job/throttle_definition.rb, line 5
def initialize(method_name, filter)
  @method_name = method_name.to_sym
  @filter      = filter
end

Public Instance Methods

extract_filter(job, *args) click to toggle source

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

# 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) click to toggle source

Returns [true|false] whether the throttle was triggered.

# 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