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?


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.


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

To set a per-job max attempts that overrides the Delayed::Worker.max_attempts you can define a max_attempts method on the job

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?

链接地址: http://www.djcxy.com/p/84760.html

上一篇: GitHub API:识别包含给定提交的分支

下一篇: 最多尝试1次的作业?