Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sidekiq Error Handling and stop worker

To avoid running unnecessary, I'd like my Sidekiq worker to make a check at each stage for a certain condition. If that condition is not met, then Sidekiq should stop and report the error.

Currently I have:

class BotWorker
    include Sidekiq::Worker

    def perform(id)
        user = User.find(id) 
        if user.nil?
           # report the error? Thinking of using UserMailer
           return false # stop the worker
        end

        # other processing here
    end

This seems like a naive way to handle Sidekiq errors. The app needs to immediately notify admin if something breaks in the worker.

Am I missing something? What is a better way to handle errors in Sidekiq?

like image 211
Delos Chang Avatar asked Oct 20 '22 17:10

Delos Chang


1 Answers

You can create your own error handler

Sidekiq.configure_server do |config|
  config.error_handlers << Proc.new {|exception,context_hash| MyErrorService.notify(exception,context_hash) }
end
like image 151
Ranjithkumar Ravi Avatar answered Nov 09 '22 16:11

Ranjithkumar Ravi