Sha256: 410764966ae837ed7db6781708bdef497648ccbcebd97ebe32a377c02aa3c10f

Contents?: true

Size: 1.39 KB

Versions: 8

Compression:

Stored size: 1.39 KB

Contents

require "active_support/concern"
module RocketJob
  module Plugins
    # Prevent this job from starting, or a batch slice from starting if the dependent jobs are running.
    #
    # Features:
    # - Ensures dependent jobs won't run
    # When the throttle has been exceeded all jobs of this class will be ignored until the
    # next refresh. `RocketJob::Config::re_check_seconds` which by default is 60 seconds.
    module ThrottleDependentJobs
      extend ActiveSupport::Concern

      included do
        field :dependent_jobs, type: Array, class_attribute: true, user_editable: true, copy_on_restart: true

        define_throttle :dependent_jobs_running?
        define_batch_throttle :dependent_jobs_running? if respond_to?(:define_batch_throttle)
      end

      class_methods do
        def depends_on_job(*jobs)
          self.dependent_jobs = Array(jobs).collect(&:to_s)
        end
      end

      private

      # Checks if there are any dependent jobs are running
      def dependent_jobs_running?
        return false if dependent_jobs.blank?

        jobs_count = RocketJob::Job.running.where(:_type.in => dependent_jobs).count
        return false if jobs_count.zero?

        logger.info(
          message: "#{jobs_count} Dependent Jobs are running from #{dependent_jobs.join(', ')}",
          metric:  "#{self.class.name}/dependent_jobs_throttle"
        )
        true
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rocketjob-6.3.1 lib/rocket_job/plugins/throttle_dependent_jobs.rb
rocketjob-6.3.0 lib/rocket_job/plugins/throttle_dependent_jobs.rb
rocketjob-6.2.0 lib/rocket_job/plugins/throttle_dependent_jobs.rb
rocketjob-6.1.1 lib/rocket_job/plugins/throttle_dependent_jobs.rb
rocketjob-6.1.0 lib/rocket_job/plugins/throttle_dependent_jobs.rb
rocketjob-6.0.3 lib/rocket_job/plugins/throttle_dependent_jobs.rb
rocketjob-6.0.2 lib/rocket_job/plugins/throttle_dependent_jobs.rb
rocketjob-6.0.1 lib/rocket_job/plugins/throttle_dependent_jobs.rb