How to cancel a job?

358 views
Skip to first unread message

Andrew Havens

unread,
May 10, 2012, 4:37:33 PM5/10/12
to delay...@googlegroups.com
Me again, sorry, but I can't find any documentation on how to cancel a job. I now have some jobs that are scheduled to run in the future, but I need the ability to cancel running that job. How can this be done with delayed job?

--Andrew

Matt Griffin

unread,
May 10, 2012, 4:47:18 PM5/10/12
to delay...@googlegroups.com
Delayed::Job is just an ActiveRecord class so you can query the table for the offending jobs and delete them. There is no way to otherwise "cancel" a job once queued and this shouldn't be something that's normally part of your work flow. If you queue jobs that may not be valid when they run, make sure they check their preconditions and just short-circuit if they shouldn't be run anymore.

For administration, there's some web interfaces floating around that let you manage jobs.

Andrew Havens

unread,
May 10, 2012, 5:36:53 PM5/10/12
to delay...@googlegroups.com
Maybe canceling a job is not part of a normal workflow, but in this case it is a business requirement that I need to implement. So let's say I have a model that has been saved to the database, and a job associated with this model which is queued to run in 5 minutes. Before 5 minutes has passed, the user cancels the job (which updates the model with a cancelled=true). My question is how do I tell delayed job not to do the job? Does DJ keep the model ID so I can query for that job and delete it? Or when the job is run, I can just skip execution, but is the model a copy of when the job was created, or do I need to fetch it from the database again?

--Andrew

Matt Griffin

unread,
May 10, 2012, 5:49:50 PM5/10/12
to delay...@googlegroups.com
When you use handle_asynchronously you can pretty much forget about what DJ is doing and just look at your model. It's going to fetch a fresh copy of the model so it can see the status of your cancel flag.

handle_asynchronously :deliver

def deliver
  return if cancelled?

  //.. do delivery
end

Greg Blass

unread,
Aug 21, 2019, 5:47:09 PM8/21/19
to delayed_job
Here's my use case: User tries to export data. Its 20,000 records and will take a long time and hold up their application's workers. They didn't want that many results anyway. They now want to cancel...

Matt Griffin

unread,
Aug 21, 2019, 6:27:52 PM8/21/19
to delay...@googlegroups.com
Holy thread necromancy.

It may be 7 years later but this is still the approach. Have your job check periodically to see if it should continue running or if the user canceled the export.

--

---
You received this message because you are subscribed to the Google Groups "delayed_job" group.
To unsubscribe from this group and stop receiving emails from it, send an email to delayed_job...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/delayed_job/dcf0a3e4-5fbb-4ff5-9cef-54e9a08724c4%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages