如何在不清除整个队列的情况下从Resque队列中删除特定作业?

Ram*_*ami 27 ruby ruby-on-rails resque redis

我正在使用Resque worker来处理队列中的作业,我在队列中有大量的作业> 1M,并且我需要删除一些作业(由于错误而添加).使用作业对队列进行装箱并非易事,因此使用resque-web清除队列并再次添加正确的作业对我来说不是一种选择.

感谢任何建议.谢谢!

小智 22

在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)
Run Code Online (Sandbox Code Playgroud)


Pun*_*eth 21

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

Resque::Job.destroy(queue1, Post, 'x')
Run Code Online (Sandbox Code Playgroud)

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

Resque::Job.destroy(QueueName, ClassName) 
Run Code Online (Sandbox Code Playgroud)

你可以在这里找到它的文档

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


ilo*_*aly 6

如果您知道传递给作业的所有参数,则上述解决方案非常有效。如果您知道某些传递给作业的参数的情况,则以下脚本将起作用:

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
Run Code Online (Sandbox Code Playgroud)