Search code examples
ruby-on-railsdelayed-job

Handle Errors with Delayed Job


Isn't there the possibility to handle errors with delayed_job gem? I would like to find a way (runtime) to send an email to a given account while the errors occur and after it performs delete them.


Solution

  • Not that code above doesn't report all the problems .. We recently got a lot issues on worker dynos and we were not even aware about them because we were listening for errors only ...

    DelayedJob knows 2 type of errors - errors and failures. In our case - failures were triggered on lower level because we were using Octopus gem. Failure message was: undefined method[]' for nil:NilClass`

    class DelayedJobExceptionPlugin < Delayed::Plugin
      def self.report_error(exception, job, type: 'error')
        Airbrake.notify(
          exception, 
          error_message: "#{exception.class.name}: #{exception.message}",
          backtrace: exception.backtrace,
          component: 'DelayedJob Worker',
          type: type,
          parameters: {
            failed_job: job.inspect
          },
        )
      end
    
      callbacks do |lifecycle|
        lifecycle.around(:invoke_job) do |job, *args, &block|
          begin
            # Forward the call to the next callback in the callback chain
            block.call(job, *args)
          rescue Exception => exception
            DelayedJobExceptionPlugin.report_error(exception, job)
    
            raise error
          end
        end
    
        lifecycle.after(:failure) do |job| # Note - some jobs did not trigger Airbrake.notify if we do not use failure!
          exception = ::RuntimeError.new('DelayedJob Failure')
          DelayedJobExceptionPlugin.report_error(exception, job, type: 'failure')
    
          raise exception if Rails.env.test?
        end
      end
    end
    
    Delayed::Worker.plugins << DelayedJobExceptionPlugin