6

I have a sidekiq worker which will request 3rd party api(Mailchimp) and got some response. Sometimes it will response an error message which the api gem will raise an error.

However, those Errors are normal and no need to retry. So I would like Sidekiq prevent retry when those Errors raised.

I have tried a simply rescue, but it won't prevent the sidekiq capture the error raised.

def preform(id)
  UpdateMailchimpService.new.(id)
rescue
  Mailchimp::ListInvalidBounceMemberError
end

Any way to do this? Thanks

Update

Finally found that my problem was caused by the broken of our deploy tool(deployment failed but not realised). Actually, the Sidekiq will ignore any rescued error/exception and they are not be retried and reported to Bugsnag.

In Bugsnag's documentation, it clearly said:

Bugsnag should be installed and configured, and any unhandled exceptions will be automatically detected and should appear in your Bugsnag dashboard.

This post on github didn't have an clear explanation so that's why I am confused by this question.

Stephen
  • 3,822
  • 2
  • 25
  • 45
  • _"it won't prevent the sidekiq capture the error raised"_ – what do you mean by that? – Stefan Jun 26 '17 at 08:02
  • @Stefan it means the sidekiq still know this job caused an error `ListInvalidBounceMemberError`, and will retry this job. – Stephen Jun 26 '17 at 12:49
  • @Stephen Your code example looks invalid. Can you provide real example. Because `rescue` prevents Sidekiq job retry for me. – Pavel Mikhailyuk Jun 26 '17 at 15:21

2 Answers2

8

Your assumption/example is incorrect. Do the normal Ruby thing: rescue the error and ignore it.

def perform(id)
  UpdateMailchimpService.new.(id)
rescue NormalError
  # job will succeed normally and Sidekiq won't retry it.
end
Jarl
  • 2,831
  • 4
  • 24
  • 31
Mike Perham
  • 21,300
  • 6
  • 59
  • 61
  • 2
    Hi Mike, thanks for your answer. the `def` block can treat as a begin block. https://stackoverflow.com/questions/1542672/how-does-one-use-rescue-in-ruby-without-the-begin-and-end-block – Stephen Jun 27 '17 at 01:50
  • And I also tried your solution but the error still appear on my bugsnag(which I think it's retried) – Stephen Jun 27 '17 at 01:53
  • Hi Mike, thanks for you confirmation, I just found my deploy tool have problem so worker didn't deploy successfully. Sidekiq would ignore any rescued error. Thank you for your so wonderful gem. – Stephen Jun 27 '17 at 04:16
1

Use retry: false advanced option:

class UpdateMailchimpWorker
  include Sidekiq::Worker
  sidekiq_options retry: false # ⇐ HERE

  def perform(id)
    UpdateMailchimpService.new.(id)
  end
end
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • Thanks for your answer. the `sidekiq_options` will change this worker totally cannot be retried. But what I want is when certain Error raised, the job stop retry. But for others, for example, connection failed, it will retry the job. – Stephen Jun 26 '17 at 12:51