Is it possible to call delayed_job with maximum attempts of 1?

I have a method that I run asynchronously

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

If this fails, I want him to not try again. My default attempts are 3, which I cannot change. I just want to try this only once. The following does not work:

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

Any ideas?

+7
ruby-on-rails ruby-on-rails-4 delayed-job
source share
2 answers

This is not my favorite solution, but if you need to use the delay method that you can set, follow these steps: one step less than your attempts. So in your case, the following should work

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

Even better, but you can make it safer using Delayed :: Worker.max_attempts

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

This would put it into your delayed_jobs table, as if it had already been run twice, so when you restart it, it will be executed at maximum attempts.

+2
source share

From https://github.com/collectiveidea/delayed_job#custom-jobs

To set the maximum number of attempts specified in one task, Delayed :: Worker.max_attempts is delayed, you can define the max_attempts method on the task

 NewsletterJob = Struct.new(:text, :emails) do def perform emails.each { |e| NewsletterMailer.deliver_text_to_email(text, e) } end def max_attempts 3 end end 

Does this help you?

+1
source share

All Articles