我正在使用collectiveidea版本的delayed_job:https://github.com/collectiveidea/delayed_job
有人能指出工人去接下一份工作时使用的实际标准吗?我假设它类似于“SELECT id FROM delayed_jobs WHERE run_at> NOW()ORDER BY priority ASC,run_at ASC LIMIT 1”(首先选择优先级,run_at时间秒),但我一直无法找到考虑到了什么。我在GitHub上的代码中做了一些讨论,但是没有找到下一个工作的实际查询。对几件事感到好奇,包括'created_at','尝试'或'failed_at'是否会影响优先级。 (我意识到我不太可能找到实际的SQL,只是一种简单的方式来表示我认为查询的内容。)
其次,这个宝石的任何实际文档的好来源?对于在Rails中常用的东西,我看到的文档非常稀疏。
答案 0 :(得分:4)
对源代码的一些挖掘在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
此外,后端/ 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
选择下一份工作。
答案 1 :(得分:1)
delayed_job/lib/delayed/backend/active_record.rb
self.find_available
来电ready_to_run
然后根据当前工人最小/最大优先级对其进行范围调整
然后按priority ASC, run_at ASC
scope :ready_to_run
来电
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])
所以它产生类似
的东西SELECT * FROM jobs where run at <= ? AND locked_at IS NULL OR locked_at < ?) OR locked_by = ?) AND failed_at IS NULL AND priority >= ? AND priority <= ? ORDER BY priority ASC, run_at ASC