【问题标题】:Why is email not being sent with ActiveJob?为什么不使用 ActiveJob 发送电子邮件?
【发布时间】:2016-01-12 22:20:36
【问题描述】:

我正在将应用程序升级到 Rails 4.2,以便我们可以利用 ActionMailer 的 deliver_later 方法。一切都在开发中运行良好。当我使用capistrano-sidekiq gem 部署到我们的登台服务器时,似乎没有发送电子邮件。根据此日志,该作业似乎已排队并正确执行。

服务器是 Ubuntu 14.04,通过 apt-get 安装了 redis。

[ActiveJob] Enqueued ActionMailer::DeliveryJob (Job ID: 51ddade2-4689-40fd-aeda-7e94f7260e43) to Sidekiq(mailers) with arguments: 
"ApplicationMailer", "admin_message", "deliver_now", "Broken Link", "Testing an email from staging."
[ActiveJob] [ActionMailer::DeliveryJob] [51ddade2-4689-40fd-aeda-7e94f7260e43] Performing ActionMailer::DeliveryJob from Sidekiq(mailers) 
with arguments: "ApplicationMailer", "admin_message", "deliver_now", "Broken Link", "Testing an email from staging."
[ActiveJob] [ActionMailer::DeliveryJob] [51ddade2-4689-40fd-aeda-7e94f7260e43]   Rendered application_mailer/admin_message.html.erb within  layouts/mailer (1.9ms)
[ActiveJob] [ActionMailer::DeliveryJob] [51ddade2-4689-40fd-aeda-7e94f7260e43]   Rendered layouts/mailer.html.erb (8.8ms)
[ActiveJob] [ActionMailer::DeliveryJob] [51ddade2-4689-40fd-aeda-7e94f7260e43]   Rendered application_mailer/admin_message.text.erb within layouts/mailer (0.8ms)
[ActiveJob] [ActionMailer::DeliveryJob] [51ddade2-4689-40fd-aeda-7e94f7260e43]   Rendered layouts/mailer.text.erb (3.6ms)
[ActiveJob] [ActionMailer::DeliveryJob] [51ddade2-4689-40fd-aeda-7e94f7260e43]
Sent mail to email-testing@loamstudios.com (216.8ms)
[ActiveJob] [ActionMailer::DeliveryJob] [51ddade2-4689-40fd-aeda-7e94f7260e43] Performed ActionMailer::DeliveryJob from Sidekiq(mailers) in 796.8ms

我检查了sidekiq是否使用ps aux | [sS]idekiq运行并且有一个条目:

deploy   15104  0.4  7.4 972692 152452 ?       Sl   10:07   0:03 sidekiq 3.5.1 sustainabilityassessment_staging [0 of 25 busy]

我还确保邮件队列存在,set :sidekiq_queue, ["default", "mailers"] 在我的deploy.rb 中。

我的邮寄方式:

def admin_message(subject, message)
    @message = message
    mail(to: "<address>", subject: subject)
  end

知道发生了什么或接下来我可以解决什么问题吗?

编辑: 做了一些更多的挖掘。我在appname/current 中使用RAILS_ENV=staging bundle exec sidekiq -q mailers -q default 开始sidekiq 并尝试再次发送电子邮件。这次发送的电子邮件,因此 capistrano-sidekiq gem 启动 sidekiq 的方式似乎有问题。

编辑: 设置 sidekiq 的监控应用程序,看看我是否可以学到任何新东西。似乎正在处理作业,没有问题。仍然没有收到电子邮件...

编辑:在config/environments/staging.rb 中发现了一个额外的config.action_mailer.raise_delivery_errors = false,它阻止我看到我的错误。我现在可以看到电子邮件被拒绝了:

Net::SMTPServerBusy: 454 4.7.1 <email address>: Relay access denied

编辑:还有一条信息,在同一个邮件程序上调用deliver_now。调用deliver_later 会导致电子邮件被拒绝。这让我认为该应用程序的 SMTP 设置正在运行。 deliver_later 调用中发生了一些事情,以阻止发送电子邮件。

【问题讨论】:

  • 确保你的 Rails 配置中有config.action_mailer.raise_delivery_errors = true。如果 ActionMailer 遇到错误,这将帮助您进行故障排除。
  • 谢谢。添加了行。经测试。没有这样的运气。相同的日志条目。
  • 使用deliver_now时邮件是否发送成功?这将有助于缩小是配置问题还是 sidekiq 问题。
  • deliver_now 工作。
  • 您介意发布您的邮件方法的内容吗?我注意到使用 ActiveJob 会导致mailer methods to be executed twice。您的邮件方法在第二次执行时是否有可能表现不同?

标签: ruby-on-rails capistrano sidekiq


【解决方案1】:

我遇到了同样的问题。这修复了它:

在 config/deploy.rb 中:

set :sidekiq_queue, "mailers"

显然,ActiveJob 将作业发送到“邮件”队列,但如果未在配置中指定,sidekiq gem 只会创建默认队列和通知队列。

【讨论】:

  • 我在 config/deploy.rb 中有 set :sidekiq_queue, ["default", "mailers"]
【解决方案2】:

我最近在我们的一台服务器上遇到了这个问题。它的配置方式与另一台服务器完全相同,但没有发送电子邮件,即使看起来 Resque 已成功处理它们。

原来是我们的监控工具God 存在问题。单独运行 sudo service god restart 并没有正确重置工作人员。解决方案是使用sudo god stop 停止所有工作人员,使用ps aux | grep resque 验证他们是否已停止,然后使用sudo service god stop 关闭上帝并使用sudo service god start 启动它。

如果您使用某种工具来监控、启动和停止 Sidekiq,我会调查并确保正确停止和重新启动工作器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 2017-11-28
    • 2017-08-07
    • 2016-09-04
    • 2010-10-17
    • 1970-01-01
    • 2016-05-02
    相关资源
    最近更新 更多