module RocketJob::Plugins::Job::ThrottleRunningJobs

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

Example:

class MyJob < RocketJob::Job
  # Maximum number of jobs of this class to process at the same time.
  self.throttle_running_jobs = 25

  def perform
    # ....
  end
end

Notes:

Private Instance Methods

throttle_running_jobs_base_query() click to toggle source
# File lib/rocket_job/plugins/job/throttle_running_jobs.rb, line 51
def throttle_running_jobs_base_query
  {:id.ne => id}
end
throttle_running_jobs_exceeded?() click to toggle source

Returns [true|false] whether the throttle for this job has been exceeded

# File lib/rocket_job/plugins/job/throttle_running_jobs.rb, line 41
def throttle_running_jobs_exceeded?
  return false unless throttle_running_jobs&.positive?

  RocketJob::Job.with(read: {mode: :primary}) do |conn|
    query = throttle_running_jobs_base_query
    throttle_group ? query["throttle_group"] = throttle_group : query["_type"] = self.class.name
    conn.running.where(query).count >= throttle_running_jobs
  end
end