Ruby on rails 在延迟作业中,优先级如何与预读交互?
当延迟的_作业将新作业从队列中拉出时,它是否首先按优先级对队列进行排序?如果不是,那么我猜低优先级作业可以在高优先级之前运行,因为“预读” 从延迟的工作文档中: 默认行为是在查找作业时从队列中读取5个作业 可用的工作。您可以通过设置 延迟::Worker.read_在前面Ruby on rails 在延迟作业中,优先级如何与预读交互?,ruby-on-rails,delayed-job,Ruby On Rails,Delayed Job,当延迟的_作业将新作业从队列中拉出时,它是否首先按优先级对队列进行排序?如果不是,那么我猜低优先级作业可以在高优先级之前运行,因为“预读” 从延迟的工作文档中: 默认行为是在查找作业时从队列中读取5个作业 可用的工作。您可以通过设置 延迟::Worker.read_在前面 示例:我添加了100个优先级为10的作业(优先级较低的作业首先运行)。然后添加1个优先级为0的作业。如果我在5之前使用默认的read\u,那么延迟的\u作业是否需要先处理96个作业,然后才能找到我的高优先级作业之一?我有一个类
示例:我添加了100个优先级为10的作业(优先级较低的作业首先运行)。然后添加1个优先级为0的作业。如果我在5之前使用默认的read\u,那么延迟的\u作业是否需要先处理96个作业,然后才能找到我的高优先级作业之一?我有一个类似的问题,并深入源代码以找到答案-这是假设您使用的是
延迟的\u作业活动的\u记录。在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
类作业<::ActiveRecord::Base
范围:按优先级,lambda{顺序(“优先级ASC,在ASC运行”)}
def self.reserve(worker,max_run_time=worker.max_run_time)#rubocop:禁用循环复杂度
#要筛选到“准备运行”记录的范围
ready\u scope=ready\u to\u run(worker.name,最大运行时间)
#要筛选到下一个合格作业的范围
ready\u scope=ready\u scope.where(“priority>=?”,Worker.min\u priority)如果Worker.min\u priority
ready\u scope=ready\u scope.where(“优先级