我正在使用 Collectiveidea 的 Delayed_Job 版本: https://github.com/collectiveidea/delayed_job
有人可以告诉我当 worker 选择下一份工作时使用的实际标准吗?我假设它类似于“SELECT id FROM dependent_jobs WHERE run_at > NOW() ORDER BYpriority ASC, run_at ASC LIMIT 1”(首先按优先级选择,其次按 run_at 时间选择),但我无法准确找到是什么经过考虑的。我在 GitHub 上的代码中做了一些研究,但还没有找到下一个作业的实际查询。对一些事情感到好奇,包括“created_at”、“attempts”或“failed_at”是否会影响优先级。 (我意识到我不太可能找到实际的 SQL,只是一种简单的方法来表示我假设的查询的作用)。
其次,这个 gem 的实际文档有什么好的来源吗?对于 Rails 中常用的东西,我见过的文档非常稀疏。
最佳答案
对源代码的一些挖掘在 backend/active_record.rb 中显示了这一点:
scope :ready_to_run, lambda {|worker_name, max_run_time|
where(['(run_at <= ? AND (locked_at IS NULL OR locked_at < ?) OR locked_by = ?) AND failed_at IS NULL', db_time_now, db_time_now - max_run_time, worker_name])
}
scope :by_priority, order('priority ASC, run_at ASC')
# Find a few candidate jobs to run (in case some immediately get locked by others).
def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time)
scope = self.ready_to_run(worker_name, max_run_time)
scope = scope.scoped(:conditions => ['priority >= ?', Worker.min_priority]) if Worker.min_priority
scope = scope.scoped(:conditions => ['priority <= ?', Worker.max_priority]) if Worker.max_priority
::ActiveRecord::Base.silence do
scope.by_priority.all(:limit => limit)
end
end
此外,backend/base.rb 中的这一点也很有趣:
def reserve(worker, max_run_time = Worker.max_run_time)
# We get up to 5 jobs from the db. In case we cannot get exclusive access to a job we try the next.
# this leads to a more even distribution of jobs across the worker processes
find_available(worker.name, 5, max_run_time).detect do |job|
job.lock_exclusively!(max_run_time, worker.name)
end
end
reserve
由工作人员调用以选择下一个工作。
关于ruby-on-rails - Railsdelayed_job 作业执行的确切顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6118540/