Rails - 使用delayed_job异步发送所有电子邮件

2024-05-09

我在用着延迟作业 https://github.com/collectiveidea/delayed_job我对此非常满意(尤其是workless https://github.com/lostboy/workless扩大)。

但我想这样设置ALL我的应用程序中的邮件是异步发送的。

事实上,为邮寄者提供的解决方案

# without delayed_job
Notifier.signup(@user).deliver

# with delayed_job
Notifier.delay.signup(@user)

不适合我,因为:

  • 它不容易维护
  • 从 gems 发送的邮件不是异步发送的(devise https://github.com/plataformatec/devise, 邮递员 https://github.com/ging/mailboxer)

我可以使用这种扩展https://github.com/mhfs/devise-async https://github.com/mhfs/devise-async但我宁愿立即找出整个应用程序的解决方案。

我不能延长吗ActionMailer覆盖.deliver方法(就像这里https://stackoverflow.com/a/4316543/1620081 https://stackoverflow.com/a/4316543/1620081但它已经有 4 年历史了,就像我在这个主题上找到的几乎所有文档一样)?

我正在使用 Ruby 1.9 和 Rails 3.2 以及 activerecord。

感谢你的支持


一个简单的解决方案是在 Notifier 对象上编写一个实用方法,如下所示:

class Notifier

  def self.deliver(message_type, *args)
    self.delay.send(message_type, *args)
  end

end

按如下方式发送注册电子邮件:

Notifier.deliver(:signup, @user)

该实用程序方法提供了一个单点,如果需要,您可以使用 resque 或 sidekiq 解决方案替换延迟的作业。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rails - 使用delayed_job异步发送所有电子邮件 的相关文章

随机推荐