Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to query delayed_job handler

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

like image 377
Mattia Lipreri Avatar asked Mar 06 '12 18:03

Mattia Lipreri


People also ask

How Delayed_ Job works?

Delayed::Job works by serializing a Ruby object as YAML and storing it in a database table so that a worker in a different process can retrieve, deserialize, and perform the requested work. Some operations that we use Delayed::Job for are longer-running data processing, sending emails, and updating search indicies.

How do I know if my job is running late?

The most simple way to check whether delayed_job is running or not, is to check at locked_by field. This field will contain the worker or process locking/processing the job. Running Delayed::Job. where('locked_by is not null') will give you some results, if there are jobs running.

What is Delayed Job in Rails?

Delayed Job, also known as DJ, makes it easy to add background tasks to your Rails applications on Heroku. You can also use Resque and many other popular background queueing libraries. Delayed Job uses your database as a queue to process background jobs.


1 Answers

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
like image 184
erickzetta Avatar answered Oct 06 '22 08:10

erickzetta