Sha256: bce57cc19f313bac816d06dd3ba3698b0a784eb973a7baade48340387502c575

Contents?: true

Size: 1.78 KB

Versions: 10

Compression:

Stored size: 1.78 KB

Contents

require 'active_support/concern'

module RocketJob
  module Batch
    # Throttle the number of slices of a specific batch job that are processed at the same time.
    #
    # Example:
    #   class MyJob < RocketJob
    #     include RocketJob::Batch
    #
    #     # Maximum number of slices to process at the same time for each running instance.
    #     self.throttle_running_slices = 25
    #
    #     def perform(record)
    #       # ....
    #     end
    #   end
    #
    # It attempts to ensure that the number of workers do not exceed this number.
    # This is not a hard limit and it is possible for the number of workers to
    # slightly exceed this value at times. It can also occur that the number of
    # slices running can drop below this number for a short period.
    #
    # This value can be modified while a job is running. The change will be picked
    # up at the start of processing slices, or after processing a slice and
    # `re_check_seconds` has been exceeded.
    #
    # 0 or nil : No limits in place
    #
    # Default: nil
    module ThrottleRunningSlices
      extend ActiveSupport::Concern

      included do
        field :throttle_running_slices, type: Integer, class_attribute: true, user_editable: true, copy_on_restart: true

        validates :throttle_running_slices, numericality: {greater_than_or_equal_to: 0}, allow_nil: true

        define_batch_throttle :throttle_running_slices_exceeded?, filter: :throttle_filter_id
      end

      private

      # Returns [Boolean] whether the throttle for this job has been exceeded
      def throttle_running_slices_exceeded?(slice)
        throttle_running_slices &&
          (throttle_running_slices != 0) &&
          (input.running.where(:id.ne => slice.id).count >= throttle_running_slices)
      end

    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rocketjob-5.0.0.beta4 lib/rocket_job/batch/throttle_running_slices.rb
rocketjob-5.0.0.beta3 lib/rocket_job/batch/throttle_running_slices.rb
rocketjob-5.0.0.beta2 lib/rocket_job/batch/throttle_running_slices.rb
rocketjob-5.0.0.beta lib/rocket_job/batch/throttle_running_slices.rb
rocketjob-4.3.0.beta2 lib/rocket_job/batch/throttle_running_slices.rb
rocketjob-4.3.0.beta lib/rocket_job/batch/throttle_running_slices.rb
rocketjob-4.2.0 lib/rocket_job/batch/throttle_running_slices.rb
rocketjob-4.1.1 lib/rocket_job/batch/throttle_running_slices.rb
rocketjob-4.1.0 lib/rocket_job/batch/throttle_running_slices.rb
rocketjob-4.0.0 lib/rocket_job/batch/throttle_running_slices.rb