2012-04-23 34 views
25

我正在使用Resque workers在队列中处理作业,我在队列中有大量作业> 1M,并且有一些我需要删除的作业(由错误添加)。使用作业对队列进行装箱并非易事,因此使用resque-web清除队列并再次添加正确的作业不是我的选择。如何在不清除整个队列的情况下从Resque队列中删除特定的作业?

欣赏任何建议。谢谢!

+0

你找到一个解决的办法?使用销毁速度非常慢吗? – 2016-06-16 15:25:41

回答

21

在resque的来源(工作类)有这样的方法,想这是你所需要的:)

# 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) 
+0

如果你使用ActiveJob,'Resque :: Job.destroy'不会有帮助,请查看这个答案:http://stackoverflow.com/questions/35589052/activejob-with-resque-enqueuing-jobs -with-uninteded论点/ 40066148#40066148 – Jared 2016-10-16 02:17:03

18

若要从队列中删除特定的工作,你可以使用destroy方法。这很容易,如果你想删除与类岗位和id x,它是在一个名为队列1 你可以这样做队列中的作业使用, 例如..

Resque::Job.destroy(queue1, Post, 'x') 

如果你想删除所有从队列中特定类型的工作,你可以使用

Resque::Job.destroy(QueueName, ClassName) 

你可以找到它的文档在

http://www.rubydoc.info/gems/resque/Resque%2FJob.destroy

0

如果您知道传递给作业的所有参数,上述解决方案的效果很好。如果你有一个情况下,你知道的传递给作业的参数一些下面的脚本将工作:

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 
相关问题