Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I call delayed_job with max attempts of 1?

I have a method that I run asynchronously

User.delay(queue: 'users').grab_third_party_info(user.id)

In case this fails, I want it to not retry. My default retries are 3, which I cannot change. I just want to have this only try once. The following doesn't seem to work:

User.delay(queue: 'users', attempts: 3).grab_third_party_info(user.id)

Any ideas?

like image 950
Jeff Avatar asked May 01 '15 20:05

Jeff


People also ask

How do you restart a delayed job?

Restart Delayed Job on deploy You must remove that one now if you have it. It basically does the same thing that we will add now but without using upstart. We will now create a new file that will host our start, stop and restart tasks. Create a file at lib/capistrano/tasks/delayed_job.

What is delayed job?

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.

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.


1 Answers

This isn't my favorite solution, but if you need to use the delay method that you can set the attempts: to one less your max attempts. So in your case the following should work

User.delay(queue: 'users', attempts: 2).grab_third_party_info(user.id)

Better yet you could make it safer by using Delayed::Worker.max_attempts

User.delay(queue: 'users', attempts: Delayed::Worker.max_attempts-1).grab_third_party_info(user.id)

This would enter it into your delayed_jobs table as if it already ran twice so when it runs again it will be at the max attempts.

like image 132
KevinM Avatar answered Sep 22 '22 06:09

KevinM