Exception Notifier: Foreground vs. background sections

Posted Almost 9 years ago. Visible to the public.

Since version 2.6 exception notifier distinguishes between foreground and background sections. The reason is that with background jobs (e.g. methods that are called by a cron job) some variables are not available for exception notifier, e.g. @request and @kontroller.
Therefore you can configure foreground and background sections individually. Our default settings are documented in Get notified when your application raises an error.

When not using the default sections, be sure that you only use background_sections that are available for background jobs. Do not use request or Exception Notifier will crash itself and not send a mail!

With foreground jobs, exception notifier gets called automatically when an error raises.
For background jobs, exceptions need to be catched and delivered manually:

class MyClass
  
  def self.do_something_per_cron
    ...
  rescue Exception => e
    # call exception notifier    
  end

end

Make sure that the call to exception notifier triggers the correct kind of mail. If foreground_sections include request (which is the case for default configuration) and your call leads to a foreground mail, but is triggered by a background job, exception notifier will also crash and not send mails!

How to call the exception notifier varies between versions:

Exception Notifier 4

ExceptionNotifier.notify_exception(e)

In version 4, foreground sections are only used when an :env option is used in the call:

ExceptionNotifier.notify_exception(exception, :env =>f request.env)

So don't use this kind of call if your code is run by a background job. Better, don't use it at all.

Exception Notifier 2.6 and 3

ExceptionNotifier::Notifier.background_exception_notification(e).deliver

Exception Notifier 2.4 and before

Seems not to differentiate between foreground and background sections.

Hint

Of course you can configure cron to send console output to an email address. But this is no solution for sidekiq, resque and other background jobs that start without cron. If your application use these, make sure that Exception Notifier is configured as explained above.

Last edit
Almost 9 years ago
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted to makandra dev (2015-05-28 09:26)