Search code examples
ruby-on-railssidekiq

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?


Solution

  • 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