smartinez87 / exception_notification

Exception Notifier Plugin for Rails
http://smartinez87.github.io/exception_notification
MIT License
2.18k stars 415 forks source link

Background notifications for Shoryuken/ActiveJob #368

Open Startouf opened 8 years ago

Startouf commented 8 years ago

Any hope to see some adaptation for shoryuken ? Or even better, for ActiveJob ? (and if it's supposed to be working already, I can fill out a better bug report)

drselump14 commented 4 years ago

would love to have it

coezbek commented 2 years ago

It seems harder than it should be to integrate with ActiveJob.

How are others solving it:

If you don't care for perfection, one way to go is to add the following to your ApplicationJob:

# app/jobs/application_job.rb
class ApplicationJob < ActiveJob::Base
  # Automatically retry jobs that encountered a deadlock
  # retry_on ActiveRecord::Deadlocked

  # Most jobs are safe to ignore if the underlying records are no longer available
  # discard_on ActiveJob::DeserializationError

  rescue_from StandardError do |exception|
    ExceptionNotifier.notify_exception(exception)
  end

end

Note: Since rescue_from stops with the first handler which handles an exception there is no way to continue with other rescue_from handlers. You could go another path with around_perform, but then all helper methods such as retry_on and discard_on will not prevent exceptions from being sent.

To decorate the failure a bit more (by giving the name of the failed job for instance) you can also do:

  rescue_from StandardError do |exception|
    begin
      raise StandardError, "#{self.class.name} failed: (#{exception.class}) #{exception}", exception.backtrace
    rescue => e
      ExceptionNotifier.notify_exception(e)
    end
  end