Ruby 对Resque失败作业队列的编程访问
如何编写代码来遍历Resque失败队列并有选择地删除作业?现在,我已经有了一些重要的失败,在一份反复运行的失控工作中,有数千次失败。我想删除失控作业生成的作业。我唯一熟悉的API是用于排队作业的API。(我将继续RTFMing,但我有点赶时间。)您可以按照要求的方式手动修改失败队列,但最好编写一个自定义失败处理程序,在作业失败时删除/重新排队 您可以找到基本失败后端和将失败作业记录到Hoptoad异常跟踪服务的实现 例如:Ruby 对Resque失败作业队列的编程访问,ruby,resque,Ruby,Resque,如何编写代码来遍历Resque失败队列并有选择地删除作业?现在,我已经有了一些重要的失败,在一份反复运行的失控工作中,有数千次失败。我想删除失控作业生成的作业。我唯一熟悉的API是用于排队作业的API。(我将继续RTFMing,但我有点赶时间。)您可以按照要求的方式手动修改失败队列,但最好编写一个自定义失败处理程序,在作业失败时删除/重新排队 您可以找到基本失败后端和将失败作业记录到Hoptoad异常跟踪服务的实现 例如: module Resque module Failure c
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
我不得不这样做:
# 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所提到的,查看是很有用的。Remove with bool函数示例
我使用了一种更高阶的函数方法,用于评估移除失败
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
可能不是你想听到的,但直接去Redis并以这种方式修改数据可能是最好的做法。虽然这段代码可能会解决这个问题,但如何以及为什么解决这个问题将真正有助于提高你的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。
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