40

I am using collectiveidea for rails 2.3.8. I am creating array of delayed jobs to perform some tasks, after some time I want to destroy all the delayed jobs which are running. If anyone know the way to do this please help me.

Jak
  • 2,893
  • 3
  • 19
  • 33
Rahul Tapali
  • 9,887
  • 7
  • 31
  • 44

3 Answers3

72

You can invoke rake jobs:clear to delete all jobs in the queue.

Dorian
  • 7,749
  • 4
  • 38
  • 57
Lee Jarvis
  • 16,031
  • 4
  • 38
  • 40
  • 4
    This doesn't do what OP asked. He is trying to kill jobs that are running, not clear the queue. – volx757 Oct 16 '15 at 18:50
  • I had a running job (it was going to hang for two hours), and after running `rake jobs:clear` the worker stopped that specific job too. – morhook Mar 07 '19 at 21:12
  • The OP wanted to know how to remove the jobs from the queue from the application side - not from the terminal, so this indeed doesn’t answer his/her question. From the upvotes seems people are coming to this question for the latter though. – Tasos Anesiadis Oct 24 '19 at 04:55
59

In addition to the rake task, DelayedJob jobs are just a normal ActiveRecord model, so if you're in Ruby code you can do what you like with them:

Delayed::Job.destroy_all
Delayed::Job.delete_all
Delayed::Job.find(4).destroy
# etc.
Ryan Brunner
  • 14,723
  • 1
  • 36
  • 52
  • Can you please elaborate Delayed::Job.find(4).destroy – Rahul Tapali Mar 14 '12 at 15:01
  • 1
    @clickit it destroy the delayed job that has the id 4. – lulalala Aug 16 '12 at 08:57
  • 1
    Is there a difference between `destroy` and `delete`? – Ian Vaughan Nov 11 '13 at 12:40
  • 4
    `destroy` will handle any callbacks that Rails defines - notably, it will clean up any associations where you have `dependent: destroy` or `depdendent: nullify` set. `delete` just deletes the record from the database. In general, unless you have a good reason to, always use `destroy` over `delete`. – Ryan Brunner Jul 15 '14 at 12:22
  • 1
    Also, Delayed::Job.find(id).locked_by will give you a PID in the string that's displayed, ex: "host:Your-Host.local pid:6899" – volx757 Oct 16 '15 at 18:55
  • there is a bunch of interesting methods/attributes, `handler` for the yaml of the class handling the job and the job params, `last_error` for a stacktrace of the last error, etc. `job.attributes` will give you what is stored in the database. also you can do `invoke_job` to run job but it will not delete it – localhostdotdev Apr 16 '19 at 17:41
0

Sounds like you've got a parent process that wants to timeout if its set of jobs doesn't complete within a certain time. Instead of hanging on to references to the jobs themselves, set a flag on a model that indicates that the process has given up. Jobs can check for that flag and short circuit if they're not needed anymore. (Your Job class should also wrap the contents of its #perform method in a timeout.)

It's almost always a bad idea to try to hang on to references to DJ objects as you seem to be suggesting.

betamatt
  • 500
  • 2
  • 8