Rami Rami - 1 year ago 114
Ruby Question

How can I delete specific jobs from Resque queue without clearing the whole queue?

I'm using Resque workers to process job in a queue, I have a large number of jobs > 1M in a queue and have some of the jobs that I need to remove ( added by error). Crating the queue with the jobs was not an easy tasks, so clearing the queue using resque-web and adding the correct jobs again is not an option for me.

Appreciate any advice. Thanks!

Answer Source

In resque's sources (Job class) there's such method, guess it's what you need :)

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