12

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.

4

4 回答 4

20

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.

于 2013-01-05T13:16:53.683 回答
2

There's a DJ-honeybadger gem. The gem is of course actively maintained which makes it much better than using a monkey patch. Granted, when the monkey patch came out, there was no gem to do this.

https://github.com/honeybadger-io/delayed_job_honeybadger

于 2014-02-21T00:58:06.873 回答
1

The best method would be to use Global Hooks. Someone proposed this in 2011, but doesn't look like they've been implemented yet.

In the mean time, this works to monkey patch things:

# Patch delayed job to report runtime errors to Airbrake
module Delayed
  class Worker

    protected

    def handle_failed_job_with_airbrake(job, error)
      ::Airbrake.notify(
        :error_class   => error.class.name,
        :error_message => "#{error.class.name}: #{error.message}",
        :parameters    => {
          :failed_job => job.inspect,
        }
      )

      handle_failed_job_without_airbrake(job, error)
    end

    alias_method_chain :handle_failed_job, :airbrake

  end
end
于 2012-11-19T18:04:01.263 回答
1

Probably the simplest way to get delayed_job to send through an alert to Airbrake when the job fails is to monkey-patch delayed_job. This allows you to hook into delayed_job's internals, and modify it slightly to alert Airbrake when something goes wrong.

Unfortunately, exactly how to do this will depend on which version of delayed_job you are using, and which version of Airbrake you are using; it will also depend, perhaps, on exactly where within the delayed_job processing you want to hook into the system.

However, probably the simplest example I have seen for how to do what you want is to monkey-patch the handle_failed_job method, as seen here. Note though that this example uses the old Hoptoad system for the alerting, so if you're using a modern Airbrake gem, you'll need to change the code that does the actual notifying to be as described here.

于 2012-12-12T17:09:43.323 回答