[英]How can I delete specific jobs from Resque queue without clearing the whole queue?
我正在使用Resque worker来处理队列中的作业,我在队列中有大量的作业> 1M,并且我需要删除一些作业(由于错误而添加)。 使用作业对队列进行装箱并非易事,因此使用resque-web清除队列并再次添加正确的作业对我来说不是一种选择。
感谢任何建议。 谢谢!
在resque的来源(Job类)中有这样的方法,猜猜你需要什么:)
# Removes a job from a queue. Expects a string queue name, a
# string class name, and, optionally, args.
#
# Returns the number of jobs destroyed.
#
# If no args are provided, it will remove all jobs of the class
# provided.
#
# That is, for these two jobs:
#
# { 'class' => 'UpdateGraph', 'args' => ['defunkt'] }
# { 'class' => 'UpdateGraph', 'args' => ['mojombo'] }
#
# The following call will remove both:
#
# Resque::Job.destroy(queue, 'UpdateGraph')
#
# Whereas specifying args will only remove the 2nd job:
#
# Resque::Job.destroy(queue, 'UpdateGraph', 'mojombo')
#
# This method can be potentially very slow and memory intensive,
# depending on the size of your queue, as it loads all jobs into
# a Ruby array before processing.
def self.destroy(queue, klass, *args)
要从队列中删除特定作业,可以使用destroy方法。 它很容易使用,例如,如果你想删除一个类Post和id x的作业,它在队列中名为queue1你可以这样做..
Resque::Job.destroy(queue1, Post, 'x')
如果要从队列中删除所有特定类型的作业,可以使用
Resque::Job.destroy(QueueName, ClassName)
你可以在这里找到它的文档
如果您知道传递给作业的所有参数,上述解决方案的效果很好。 如果您知道传递给作业的某些参数,则以下脚本将起作用:
queue_name = 'a_queue'
jobs = Resque.data_store.peek_in_queue(queue_name, 0, 500_000);
deleted_count = 0
jobs.each do |job|
decoded_job = Resque.decode(job)
if decoded_job['class'] == 'CoolJob' && decoded_job['args'].include?('a_job_argument')
Resque.data_store.remove_from_queue(queue_name, job)
deleted_count += 1
puts "Deleted!"
end
end
puts deleted_count
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.