问题描述
我正在使用,对此我感到非常满意(尤其是扩展名)。
I'm using delayed_job and I'm very happy with it (especially with the workless extension).
但是我想要设置我的应用中的 ALL 邮件是异步发送的。
But I'd like to set that ALL mails from my app are sent asynchronously.
实际上,为邮件提供的解决方案
Indeed, the solution offered for mailers
# without delayed_job
Notifier.signup(@user).deliver
# with delayed_job
Notifier.delay.signup(@user)
不适合我,因为:
- 不容易维护
- 从gems发送的邮件不是异步发送的(,)
- it is not easily maintainable
- mails sent from gems are not sent asynchronously (devise, mailboxer)
我可以使用这种扩展名但我宁愿为整体解决方案
I could use this kind of extension https://github.com/mhfs/devise-async but I'd rather figure out a solution for the whole app at once.
我不能扩展 ActionMailer
来覆盖 .deliver
方法(如此处,但这是4岁,就像我在该主题上找到的几乎所有文档一样)?
Can't I extend ActionMailer
to override the .deliver
method (like here https://stackoverflow.com/a/4316543/1620081 but it is 4 years old, like pretty much all the doc I found on the topic)?
我正在将Ruby 1.9和Rails 3.2与activerecord一起使用。
I'm using Ruby 1.9 and Rails 3.2 with activerecord.
感谢支持
推荐答案
一个简单的解决方案是在Notifier上编写实用程序方法对象如下:
A simple solution would be to write a utility method on the Notifier object as follows:
class Notifier
def self.deliver(message_type, *args)
self.delay.send(message_type, *args)
end
end
发送注册电子邮件,如下所示:
Send the sign up email as follows:
Notifier.deliver(:signup, @user)
实用程序方法提供了一个要点,如果需要,可以用resque或sidekiq解决方案代替延迟的工作。
The utility method provides a single point where if needed you could replace delayed job with resque or sidekiq solutions.
这篇关于Rails-异步发送所有带有delay_job的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!