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

Extended by:
ActiveSupport::Concern
Included in:
Job
Defined in:
lib/rocket_job/plugins/job/throttle.rb

Overview

Throttle number of jobs of a specific class that are processed at the same time.

Example:

class MyJob < RocketJob
  # Maximum number of workers to process instances of this job at the same time.
  self.throttle_max_workers = 25

  def perform
    # ....
  end
end

Notes:

  • The actual number will be around this value, it con go over slightly and can drop depending on check interval can drop slightly below this value.

  • By avoid hard locks and counters performance can be maintained while still supporting good enough throttling.

  • If throughput is not as important as preventing brief spikes when many workers are running, add a double check into the perform:

    class MyJob < RocketJob
      self.throttle_max_workers = 25
    
      def perform
        # (Optional) Prevent a brief spike from exceeding the wax worker throttle
        self.class.throttle_double_check
    
        # ....
      end
    end
    

Instance Method Summary collapse

Instance Method Details

#throttle_double_check(check_seconds = 1) ⇒ Object

Prevent a brief spike from exceeding the wax worker throttle



54
55
56
57
58
# File 'lib/rocket_job/plugins/job/throttle.rb', line 54

def throttle_double_check(check_seconds = 1)
  while !throttle_exceeded?
    sleep check_seconds
  end
end

#throttle_exceeded?Boolean

Returns [Boolean] whether the throttle for this job has been exceeded

Returns:

  • (Boolean)


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

def throttle_exceeded?
  throttle_max_workers && (throttle_max_workers != 0) ? (self.class.running.count >= throttle_max_workers) : false
end

#throttle_filterObject

Throttle to add when the throttle is exceeded



44
45
46
# File 'lib/rocket_job/plugins/job/throttle.rb', line 44

def throttle_filter
  {:_type.nin => [self.class.name]}
end