以编程方式访问Resque失败作业队列

Lad*_*ein 3 ruby resque

如何编写代码以通过Resque故障队列并有选择地删除作业?现在我在那里遇到了一些重要的失败,穿插在反复失控的失控工作的成千上万次失败中.我想删除失控作业生成的那些.我熟悉的唯一API是排队工作.(我会继续RTFMing,但我有点匆忙.)

Epi*_*ene 7

我需要这样做:

# loop over all failure indices, instantiating as needed  
(Resque::Failure.count-1).downto(0).each do |error_index_number|     

  failure = Resque::Failure.all(error_index_number)

  # here :failure is the hash that has all the data about the failed job, perform any check you need here      

  if failure["error"][/regex_identifying_runaway_job/].present?        
    Resque::Failure.remove(error_index_number)
    # or
    # Resque::Failure.requeue(error_index_number)
  end
Run Code Online (Sandbox Code Playgroud)

正如@Winfield 提到的,查看Resque 的失败后端很有用。


Win*_*eld 5

您可以按照要求的方式手动修改失败队列,但最好编写一个自定义失败处理程序,在失败时删除/重新排队作业.

你可以找到后端基础故障在这里和登录失败的作业给黾例外跟踪服务的实施在这里.

例如:

module Resque
  module Failure
    class RemoveRunaways < Base
      def save
        i=0
        while job = Resque::Failure.all(i)
          # Selectively remove all MyRunawayJobs from failure queue whenever they fail
          if job.fetch('payload').fetch('class') == 'MyRunawayJob'
            remove(i) 
          else
            i = i + 1
          end
        end
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

编辑:忘了提到如何指定此后端来处理失败.

在您的Resque初始化程序中(例如:config/initializers/resque.rb):

# Use Resque Multi failure handler: standard handler and your custom handler
Resque::Failure::Multiple.classes = [Resque::Failure::Redis, Resque::Failure::RemoveRunaways]
Resque::Failure.backend = Resque::Failure::Multiple
Run Code Online (Sandbox Code Playgroud)