Rails的确切作业执行顺序delay_job

Jas*_*ase 2 ruby-on-rails delayed-job

我正在使用Collectoridea的delay_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”,“ attempts”或“ failed_at”因素都放在了优先级中。(而且我意识到我不太可能找到实际的SQL,这只是表示我假设查询所做的一种简单方法)。

其次,该宝石的实际文档有什么好的来源?对于在Rails中如此普遍使用的东西,我所看到的文档相当稀疏。

Ale*_*ban 5

有点挖掘源代码可以在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
Run Code Online (Sandbox Code Playgroud)

同样,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
Run Code Online (Sandbox Code Playgroud)

reserve 由工人叫来选择下一份工作。