Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Delayed_Job notify Airbrake when an ActionMailer runs into an error?

The DelayedJob docs mention hooks, including an error hook, but only in the context of custom Job subclasses.

This similar question (with no answers) says adding the same hook to the mailer class did not work.

What's the trick?

Update:

In general, I'd like to see how to add hooks to jobs that are triggered using the object.delay.action() syntax, where I don't see an obvious link to a ____Job class.

like image 785
Jordan Feldstein Avatar asked Oct 02 '12 01:10

Jordan Feldstein


1 Answers

I was just searching for a solution to this problem too, and I found this gist.

I don't know where it comes from (found it on Google), but well, it seems to do the job pretty well, is quite simple, and seems to follow a DelayedJob's plugin system I was not even aware of...

Here is a lightly improved one using parts of previous monkey-patch code:

# https://gist.github.com/2223758
# modified

module Delayed
  module Plugins
    class Airbrake < Plugin
      module Notify
        def error(job, error)
          ::Airbrake.notify_or_ignore(
            :error_class   => error.class.name,
            :error_message => "#{error.class.name}: #{error.message}",
            :parameters    => {
              :failed_job => job.inspect,
            }
          )
          super if defined?(super)
        end
      end

      callbacks do |lifecycle|
        lifecycle.before(:invoke_job) do |job|
          payload = job.payload_object
          payload = payload.object if payload.is_a? Delayed::PerformableMethod
          payload.extend Notify
        end
      end
    end
  end
end

Delayed::Worker.plugins << Delayed::Plugins::Airbrake

It will add the error's message and payload so that it's available in Airbrake.

like image 71
Romain Champourlier Avatar answered Nov 28 '22 11:11

Romain Champourlier