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

假如想象 提交于 2020-01-01 04:23:08

问题


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.


回答1:


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.




回答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




回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/12683364/how-to-make-delayed-job-notify-airbrake-when-an-actionmailer-runs-into-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!