Sha256: b07bceab77bef03c3578c5cdd13c255e5ee10f99edf731266ad5172a506d2bce

Contents?: true

Size: 1.5 KB

Versions: 14

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

14 entries across 14 versions & 1 rubygems

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