2012-04-23 135 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