Sha256: 3737a81ff401084a374f2435d127369dcdabf8faf06151c6702041562094ded8

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 KB

Contents

require 'active_support/concern'

module RocketJob
  module Plugins
    module Job
      # Throttle the number of jobs of a specific class that are processed at the same time.
      #
      # Example:
      #   class MyJob < RocketJob
      #     # Maximum number of jobs of this class to process at the same time.
      #     self.throttle_running_jobs = 25
      #
      #     def perform
      #       # ....
      #     end
      #   end
      #
      # Notes:
      # - The actual number will be around this value, it can go over slightly and
      #   can drop depending on check interval can drop slightly below this value.
      # - By avoiding hard locks and counters performance can be maintained while still
      #   supporting good enough quantity throttling.
      module ThrottleRunningJobs
        extend ActiveSupport::Concern

        included do
          # Limit number of jobs running of this class.
          class_attribute :throttle_running_jobs
          self.throttle_running_jobs = nil

          define_throttle :throttle_running_jobs_exceeded?
        end

        private

        # Returns [Boolean] whether the throttle for this job has been exceeded
        def throttle_running_jobs_exceeded?
          throttle_running_jobs &&
            (throttle_running_jobs != 0) &&
              (self.class.running.where(:id.ne => id).count >= throttle_running_jobs)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rocketjob-3.3.0 lib/rocket_job/plugins/job/throttle_running_jobs.rb
rocketjob-3.2.1 lib/rocket_job/plugins/job/throttle_running_jobs.rb
rocketjob-3.2.0 lib/rocket_job/plugins/job/throttle_running_jobs.rb
rocketjob-3.1.0 lib/rocket_job/plugins/job/throttle_running_jobs.rb