当ActionMailer发生错误时,如何使Delayed_Job通知Airbrake? [英] How to make Delayed_Job notify Airbrake when an ActionMailer runs into an error?
问题描述
DelayedJob文档提到了挂钩,包括错误挂钩,但仅在自定义Job子类的上下文中。
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.
诀窍是什么?
更新:
总的来说,我想看看如何将钩子添加到使用 object.delay.action()
语法触发的作业中,在该语法中,我看不到 ____ Job
类。
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.
推荐答案
我也正在寻找此问题的解决方案,所以发现要点。
I was just searching for a solution to this problem too, and I found this gist.
我不知道它在哪里来自(在Google上找到它),但是看起来似乎做得很好,很简单,而且似乎遵循了我甚至都不知道的DelayedJob的插件系统...
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
它将添加错误消息和有效负载,以便在Airbrake中可用
It will add the error's message and payload so that it's available in Airbrake.
这篇关于当ActionMailer发生错误时,如何使Delayed_Job通知Airbrake?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!