18

I'm playing with delayed_job and I need to delete all the job with a specified handler value, I tried in this way

class Auction < ActiveRecord::Base
  def clean_jobs
    Delayed::Job.all.each do |job|
      job.delete if job.payload_object.auction_id == id
    end
  end
end

and it works but I have to go through the entire queue...not cool. How can i work around this? Thank you

Mattia Lipreri
  • 953
  • 1
  • 16
  • 30

1 Answers1

27

You are using payload_object, which is a YAML text.

May be this code do the same thing.

Delayed::Job.where("handler LIKE '%auction_id: #{id}%'").delete_all

And for double check:

Delayed::Job.where("handler LIKE '%auction_id: #{id}%'").each do |job|
  job.delete if job.payload_object.auction_id == id
end
erickzetta
  • 681
  • 5
  • 3
  • 1
    You can use the space before the attribute and new line character afterwards to avoid having to check each payload_object - e.g. Delayed::Job.where("handler LIKE '% user_id: #{self.id}%\n'") – RidingTheRails Jun 26 '15 at 22:55