Module: RocketJob::Plugins::Job::Throttle::ClassMethods

Defined in:
lib/rocket_job/plugins/job/throttle.rb

Instance Method Summary collapse

Instance Method Details

#define_throttle(method_name, filter: :throttle_filter_class) ⇒ Object

Add a new throttle.

Parameters:

method_name: [Symbol]
  Name of method to call to evaluate whether a throttle has been exceeded.
  Note: Must return true or false.
filter: [Symbol|Proc]
  Name of method to call to return the filter when the throttle has been exceeded.
  Or, a block that will return the filter.
  Default: :throttle_filter_class (Throttle all jobs of this class)

Note: Throttles are executed in the order they are defined.

Raises:

  • (ArgumentError)


49
50
51
52
53
54
# File 'lib/rocket_job/plugins/job/throttle.rb', line 49

def define_throttle(method_name, filter: :throttle_filter_class)
  raise(ArgumentError, "Filter for #{method_name} must be a Symbol or Proc") unless filter.is_a?(Symbol) || filter.is_a?(Proc)
  raise(ArgumentError, "Cannot define #{method_name} twice, undefine previous throttle first") if has_throttle?(method_name)

  self.rocket_job_throttles += [ThrottleDefinition.new(method_name, filter)]
end

#has_throttle?(method_name) ⇒ Boolean

Has a throttle been defined?

Returns:

  • (Boolean)


62
63
64
# File 'lib/rocket_job/plugins/job/throttle.rb', line 62

def has_throttle?(method_name)
  rocket_job_throttles.find { |throttle| throttle.method_name == method_name }
end

#undefine_throttle(method_name) ⇒ Object

Undefine a previously defined throttle



57
58
59
# File 'lib/rocket_job/plugins/job/throttle.rb', line 57

def undefine_throttle(method_name)
  rocket_job_throttles.delete_if { |throttle| throttle.method_name }
end