可编程访问Resque失败作业队列

6

我如何编写代码来遍历Resque失败队列并有选择地删除作业?目前,我有几个重要的失败案例,在其中间插着数千个由一个反复运行的任务生成的失败案例。我希望删除由失控的作业生成的那些失败案例。我熟悉的唯一API是用于排队作业的API。(我会继续翻阅文档,但我有点赶。)


可能不是你想听到的,但直接访问 Redis 并以这种方式更改数据可能是最好的选择。 - mikeycgto
3个回答

8
我最终是这样做的:
# 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

像@Winfield 提到的那样,查看Resque的失败后端是有用的。


6
您可以手动修改失败队列的方式,但编写一个自定义的故障处理程序来在作业失败时删除/重新加入可能更好。
您可以在这里找到基础故障后端以及将失败作业记录到Hoptoad异常跟踪服务中的实现 例如:
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

编辑:忘记讲述如何指定这个后端来处理失败的情况。

在您的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

0

使用布尔函数删除示例

我使用了一种高阶函数的方法,它评估了删除失败。

    def remove_failures(should_remove_failure_func)
      (Resque::Failure.count-1).downto(0).each do |i|
        failure = Resque::Failure.all(i)
        Resque::Failure.remove(i) if should_remove_failure_func.call(failure)
      end
    end

    def remove_failed_validation_jobs
      has_failed_for_validation_reason = -> (failure) do
        failure["error"] == "Validation failed: Example has already been taken"
      end
      remove_failures(has_failed_for_validation_reason)
    end

虽然这段代码可能解决了问题,但是包括解释它如何以及为什么解决了问题将有助于提高您的帖子质量,并可能导致更多的赞。请记住,您正在回答未来读者的问题,而不仅仅是现在提问的人。请[编辑]您的答案以添加解释并指出适用的限制和假设。 - Adrian Mole

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接