Sha256: 4fc1041b597f216a3c3a90859edabe857c448a262a04da573a3f79d084ce20b4

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

require_relative 'job'

# JobFinder runs SQL query which is almost same as delayed_job_active_record.
# See https://github.com/collectiveidea/delayed_job_active_record/blob/master/lib/delayed/backend/active_record.rb
module Delayed
  module Master
    class JobFinder
      def initialize(master)
        @config = master.config
      end

      def ready_jobs(database, setting, limit)
        ready_scope(database, setting).limit(limit).pluck(:id, :run_at).map do |id, run_at|
          Job.new(database: database, setting: setting, id: id, run_at: run_at)
        end
      end

      def recent_jobs(database)
        recent_scope(database).order(:run_at).limit(1).pluck(:id, :run_at).map do |id, run_at|
          Job.new(database: database, id: id, run_at: run_at)
        end
      end

      private

      def ready_scope(database, setting)
        model = database.model
        model.where("(run_at <= ? AND (locked_at IS NULL OR locked_at < ?)) AND failed_at IS NULL",
          model.db_time_now,
          model.db_time_now - (setting.max_run_time || Delayed::Worker::DEFAULT_MAX_RUN_TIME)
        ).tap do |jobs|
          jobs.where!("priority >= ?", setting.min_priority) if setting.min_priority
          jobs.where!("priority <= ?", setting.max_priority) if setting.max_priority
          jobs.where!(queue: setting.queues) if setting.queues.present?
        end
      end

      def recent_scope(database)
        model = database.model
        model.where("run_at > ? AND run_at < ? AND locked_at IS NULL AND failed_at IS NULL",
          model.db_time_now,
          model.db_time_now + @config.polling_interval,
        )
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
delayed_job_master-3.1.2 lib/delayed/master/job_finder.rb
delayed_job_master-3.1.1 lib/delayed/master/job_finder.rb
delayed_job_master-3.1.0 lib/delayed/master/job_finder.rb