Sha256: 4dab145d2a2d68063f185f1da2b2ec04d39afcc011fa39e9538657f336637f99

Contents?: true

Size: 1.5 KB

Versions: 24

Compression:

Stored size: 1.5 KB

Contents

require "active_support/concern"
module RocketJob
  module Batch
    # Automatically lower the priority for Jobs with a higher record_count.
    #
    # Note:
    #   - Add `:lower_priority` as a before_batch, but only once the `record_count` has been set.
    #   - If the `record_count` is not set by the time this plugins `before_batch`
    #     is called, then the priority will not be modified.
    #
    # class SampleJob < RocketJob::Job
    #   include RocketJob::Plugins::Batch
    #   include RocketJob::Plugins::Batch::LowerPriority
    #
    #   before_batch :upload_data, :lower_priority
    #
    #   def perform(record)
    #     record.reverse
    #   end
    #
    #   private
    #
    #   def upload_data
    #     upload do |stream|
    #       stream << 'abc'
    #       stream << 'def'
    #       stream << 'ghi'
    #     end
    #   end
    # end
    module LowerPriority
      extend ActiveSupport::Concern

      included do
        unless public_method_defined?(:record_count=)
          raise(ArgumentError, "LowerPriority can only be used in conjunction with RocketJob::Plugins::Batch")
        end

        # For each of this many records lower the priority by 1.
        class_attribute :lower_priority_count
        self.lower_priority_count = 100_000
      end

      private

      def lower_priority
        return unless record_count

        new_priority  = priority + (record_count.to_f / lower_priority_count).to_i
        self.priority = [new_priority, 100].min
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
rocketjob-5.2.0 lib/rocket_job/batch/lower_priority.rb
rocketjob-5.2.0.beta3 lib/rocket_job/batch/lower_priority.rb
rocketjob-5.2.0.beta2 lib/rocket_job/batch/lower_priority.rb
rocketjob-5.2.0.beta1 lib/rocket_job/batch/lower_priority.rb