发送带有delay_job的电子邮件时报告错误 [英] Error reporting when sending emails with delayed_job

查看:92
本文介绍了发送带有delay_job的电子邮件时报告错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AirBrake或ExceptionNotifier之类的工具通过邮件发送延迟的工作时,获取错误报告的正确方法是什么?

What's the proper way to get error reports, when using a tool like AirBrake or ExceptionNotifier from mailing delayed jobs?

我试图创建自己的延迟的工作类别,但是 Mailer.welcome()(或类似的文件)创建的邮件对象未正确序列化。我还尝试将 error(job,exception)方法添加到 PerformableMailer PerformableMethod 类,但我相信通常会发生更多与序列化有关的错误。我尝试了psych和sych进行序列化。

I tried to creating my own delayed job class, but the mail object created by Mailer.welcome() (or similar) is not serialized correctly. I also tried adding an error(job, exception) method to the PerformableMailer and PerformableMethod classes, but I got more errors generally related to serializing I believe. I tried both psych and sych for the serialization.

推荐答案

更新的解决方案



总体而言,解决方案非常简单。如果您要在 Object 上执行delay_job(例如 MyClass.new.delay.some_method ),则需要将错误处理定义为一个对象方法。如果您正在 Class 上执行delay_job(例如 MyTestMailer.test_email ... ),则需要将错误处理定义为类方法。

Updated Solution

Overall the solution is quite simple. If you have are doing delayed_job on an Object (like MyClass.new.delay.some_method), then you need to define error handling as an object method. If you're doing delayed_job on a Class (like MyTestMailer.test_email ...), then you need to define error handling as a class method.

假设您有一个名为 TestMailer 的邮件程序。解决方案是将错误处理定义为类方法,而不是对象方法

Let's say you have a mailer called TestMailer. The solution is to define the error handling as a class method, not an object method:

# Your rails mailer
class TestMailer

  # Whoa! error has to be a class method!
  def self.error(job, e)
    puts "I can now handle test mailer errors in delayed job!!!!"
  end

end

现在上述 def self.error 方法将用作延迟作业中的错误回调!

Now the above def self.error method will be used as the error callback in the delayed job!

或者如果您希望能够处理所有动作邮件错误,

Or if you want to be able to handle all action mailer errors,

class ActionMailer::Base
  def self.error(job, e)
    puts "I can now handle all mailer errors in delayed job!!!"
  end
end

原因是因为DelayedJob内部的 PerformableMethod 处理错误。 PerformableMethod 有两件事:目标对象和目标方法。对于Action Mailer,目标对象不是对象,而是您的邮件程序类 TestMailer 。目标方法是您使用的邮件方法,例如 test_mail 。 DelayedJob查找所有挂钩(错误之前之后等)。但是在我们的例子中,目标对象是类本身。因此,必须将钩子定义为类方法。

The reason is because of the way DelayedJob's internal PerformableMethod handles errors. A PerformableMethod has two things: a Target Object, and a Target Method. In Action Mailer's case, the Target Object is not an object, but your mailer class TestMailer. The target method is the mail method that you use, say test_mail. DelayedJob looks for all the hooks (error, before, after, etc) on the Target Object. But in our case, the Target Object is the class itself. Hence the hooks have to be defined as class methods.

DelayedJob 处理ActionMailer邮件的方式有点怪异。如果添加对象方法而不是类方法,则它将引发不必要的异常。例如,下面是代码:

The way DelayedJob handles ActionMailer mails is a little hacky. If you add an object method instead of a class method, it throws an unwanted exception. For example, here is the code:

# In <delayed-job-gem>/lib/delayed/performable_method.rb
module Delayed
  class PerformableMethod

    # line #7
    delegate :method, :to => :object

Ruby中的每个对象都有方法函数,用于获取对该类内方法的原始引用。但是在DelayedJob中-这个原始的方法函数已经委托给其他一些目标对象。此hack使我们无法正常使用 def错误函数来处理作业错误。

Every object in ruby has a method function, which is used to get a raw reference to a method inside that class. But in DelayedJob - this raw method function has been kind of delegated to some other target object. This hack prevents us from normally using the def error function for handling job errors.

编辑:添加了脚注,较小澄清

Added footnote, minor clarification

编辑2:重新排列答案

这篇关于发送带有delay_job的电子邮件时报告错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆