ruby on rails-在Delayed_job中手动重试作业

Delayed :: Job的自动重试功能很棒,但是我现在想手动重做一项工作。 有没有一种方法可以像我这样调用工作本身?

Delayed::Job.all[0].perform

或跑步或其他。 我尝试了一些事情,并整理了文档,但不知道如何执行作业的手动重试。

9个解决方案
94 votes

手动呼叫工作

Delayed::Job.find(10).invoke_job # 10 is the job.id

如果作业成功运行,这不会删除该作业。 您需要手动将其删除:

Delayed::Job.find(10).destroy
The Who answered 2019-10-08T10:11:26Z
26 votes
Delayed::Worker.new.run(Delayed::Job.last)

完成后将删除该作业。

Beena Shetty answered 2019-10-08T10:11:50Z
11 votes

通过找到工作并执行绩效,您可以按照您所说的完全做到这一点。

但是,我通常要做的只是将run_at设置回去,以便作业处理器再次将其拾取。

Joe Martinez answered 2019-10-08T10:12:20Z
7 votes

我在控制器中有一个用于测试目的的方法,当我点击一个URL时,它只会重置所有延迟的作业。 不是超级优雅,但是对我来说很棒:

# For testing purposes
  def reset_all_jobs
    Delayed::Job.all.each do |dj|
      dj.run_at = Time.now - 1.day
      dj.locked_at = nil
      dj.locked_by = nil
      dj.attempts = 0
      dj.last_error = nil
      dj.save
    end
    head :ok
  end
Tony answered 2019-10-08T10:12:44Z
6 votes

上面的先前答案可能已过时。 我发现需要将failed_at,locked_by和locked_at设置为nil:

(对于您要重试的每个作业):

d.last_error = nil
d.run_at = Time.now
d.failed_at = nil
d.locked_at = nil
d.locked_by = nil
d.attempts = 0
d.failed_at = nil # needed in Rails 5 / delayed_job (4.1.2)
d.save!
jpwynn answered 2019-10-08T10:13:14Z
4 votes

在开发环境中,按照Joe Martinez的建议,通过rails console,重试所有延迟的工作的好方法是:

Delayed::Job.all.each{|d| d.run_at = Time.now; d.save!}
Darme answered 2019-10-08T10:13:38Z
4 votes

如果延迟的作业失败,需要重新运行,则只需选择它们,并将引用失败的重试的所有内容设置为null:

Delayed::Job.where("last_error is not null").each do |dj|
  dj.run_at = Time.now.advance(seconds: 5)
  dj.locked_at = nil
  dj.locked_by = nil
  dj.attempts = 0
  dj.last_error = nil
  dj.failed_at = nil
  dj.save  
end
mohamed-ibrahim answered 2019-10-08T10:14:02Z
2 votes
Delayed::Job.all.each(&:invoke_job)
rubycop answered 2019-10-08T10:14:19Z
0 votes

把它放在一个初始化文件中!

module Delayed
  module Backend
    module ActiveRecord
      class Job
        def retry!
          self.run_at = Time.now - 1.day
          self.locked_at = nil
          self.locked_by = nil
          self.attempts = 0
          self.last_error = nil
          self.failed_at = nil
          self.save!
        end
      end
    end
  end
end

然后可以运行Delayed::Job.find(1234).retry!

基本上,这会将作业重新放入队列并正常处理。

Blair Anderson answered 2019-10-08T10:14:56Z
translate from https://stackoverflow.com:/questions/3312054/manually-retry-job-in-delayed-job