优先级如何与delayed_job中的read_ahead进行交互?

时间:2016-03-01 21:49:56

标签: ruby-on-rails delayed-job

当delayed_job从队列中拉出新作业时,它是否先按优先级对队列进行排序?如果没有,那么我猜测低优先级的工作可以在高优先级之前运行,因为" read_ahead"。

来自delayed_job文档:

  

默认行为是在查找时从队列中读取5个作业   可用的工作。您可以通过设置进行配置   延迟:: Worker.read_ahead。

示例:我添加100个优先级为10的作业(首先运行较低优先级)。然后我添加1个优先级为0的作业。如果我使用默认的read_ahead为5,那么delayed_job是否需要首先处理96个作业才能找到我的一个高优先级作业?

1 个答案:

答案 0 :(得分:5)

我有一个类似的问题并深入挖掘源代码以找到答案 - 假设您正在使用delayed_job_active_record。在backend / active_record.rb中:

  class Job < ::ActiveRecord::Base
    scope :by_priority, lambda { order("priority ASC, run_at ASC") }

    def self.reserve(worker, max_run_time = Worker.max_run_time) # rubocop:disable CyclomaticComplexity
      # scope to filter to records that are "ready to run"
      ready_scope = ready_to_run(worker.name, max_run_time)

      # scope to filter to the single next eligible job
      ready_scope = ready_scope.where("priority >= ?", Worker.min_priority) if Worker.min_priority
      ready_scope = ready_scope.where("priority <= ?", Worker.max_priority) if Worker.max_priority
      ready_scope = ready_scope.where(queue: Worker.queues) if Worker.queues.any?
      ready_scope = ready_scope.by_priority

      reserve_with_scope(ready_scope, worker, db_time_now)
    end

    def self.reserve_with_scope(ready_scope, worker, now)
      # Optimizations for faster lookups on some common databases
      case connection.adapter_name
      when "PostgreSQL"
        quoted_table_name = connection.quote_table_name(table_name)
        subquery_sql      = ready_scope.limit(1).lock(true).select("id").to_sql
        reserved          = find_by_sql(["UPDATE #{quoted_table_name} SET locked_at = ?, locked_by = ? WHERE id IN (#{subquery_sql}) RETURNING *", now, worker.name])
        reserved[0]
      when "MySQL", "Mysql2"
        now = now.change(usec: 0)
        count = ready_scope.limit(1).update_all(locked_at: now, locked_by: worker.name)
        return nil if count == 0
        where(locked_at: now, locked_by: worker.name, failed_at: nil).first
      when "MSSQL", "Teradata"
        subsubquery_sql = ready_scope.limit(1).to_sql
        subquery_sql = "SELECT id FROM (#{subsubquery_sql}) AS x"
        quoted_table_name = connection.quote_table_name(table_name)
        sql = ["UPDATE #{quoted_table_name} SET locked_at = ?, locked_by = ? WHERE id IN (#{subquery_sql})", now, worker.name]
        count = connection.execute(sanitize_sql(sql))
        return nil if count == 0
        where(locked_at: now, locked_by: worker.name, failed_at: nil).first
      else
        reserve_with_scope_using_default_sql(ready_scope, worker, now)
      end
    end

    def self.reserve_with_scope_using_default_sql(ready_scope, worker, now)
      # This is our old fashion, tried and true, but slower lookup
      ready_scope.limit(worker.read_ahead).detect do |job|
        count = ready_scope.where(id: job.id).update_all(locked_at: now, locked_by: worker.name)
        count == 1 && job.reload
      end
    end

因此看起来优先级优先 - 当DelayedJob找到下一个可预留和运行的可用作业时,它会先按优先级排序,然后再尝试将结果限制为&#34; read_ahead&#34;。

事实上,最后一个方法reserve_with_scope_using_default_sql是唯一一个&#34; read_ahead&#34;甚至提到了,所以如果您使用Postgres或MySQL,它会自动选择优先级最高的作业(限制1)并忽略&#34; read_ahead&#34;。