我正在使用Collectoridea版本的delay_job:https://github.com/collectiveidea/delayed_job
谁能指出我一个工人去挑选下一份工作的实际标准?我假设它类似于“从delay_jobs中的run_at> NOW()ORDER BY优先级ASC,RUN_AT ASC LIMIT 1来选择ID”(优先顺序优先,RUN_AT时间第二优先),但我无法确切找到考虑过的。我已经在GitHub上的代码中做了一些探索,但是还没有找到下一份工作的实际查询。对几件事感到好奇,包括是否将“ created_at”,“ attempts”或“ failed_at”因素都放在了优先级中。 (而且我意识到我不太可能找到实际的SQL,这只是表示我假设查询所做的一种简单方法)。
其次,该宝石的实际文档有什么好的来源?对于在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 - Rails的确切作业执行顺序delay_job,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6118540/