ActionMailer,Sneakers,deliver_later功能

时间:2018-01-22 21:01:26

标签: ruby-on-rails-4 rabbitmq sneakers

我有下一个代码:

# Gemfile
gem 'sneakers'

# application.rb
config.active_job.queue_adapter = :sneakers

# application_mailer.rb
class ApplicationMailer < ActionMailer::Base
...
  def send_letter(params={})
    mail(params)
  ...
  end
end

# my_mailer_controller.rb
def send_my_mail
  ApplicationMailer.send_letter(params).deliver_later
end

已安装并启动rabbitmq-server。 当我运行'send_my_mail'函数时,我在Rails控制台消息中收到:

[ActiveJob] Enqueued ActionMailer::DeliveryJob (Job ID: b2c66a30-70d1-4bba-a88e-d6260443347d) to Sneakers(mailers) with arguments: "ApplicationMailer", "send_letter", "deliver_now", ...

但是当我使用终端命令运行ActiveJob worker时:

WORKERS=ActiveJob::QueueAdapters::SneakersAdapter::JobWrapper rake sneakers:run
没有任何反应。看起来具有正确类名的邮件程序监听器无法启动,因此不会发送字母。在Rabbitmq UI跟踪器中,我看到了消息事件的记录:

Node:         rabbit@ubuntu-trusty-64
Connection:   127.0.0.1:35834 -> 127.0.0.1:5672
Virtual host: /
User:         guest
Channel:      1
Exchange:     sneakers
Routing keys: [<<"mailers">>]
Routed queues: []
Properties:   [{<<"priority">>,signedint,0},
               {<<"delivery_mode">>,signedint,2},
               {<<"content_type">>,longstr,<<"application/octet-stream">>}]
Payload: 
{"job_class":"ActionMailer::DeliveryJob","job_id":"7a58ab27-d110-4e8b-911b-fe821735e1b7","queue_name":"mailers","arguments":["ApplicationMailer","send_newsletter","deliver_now",...

'deliver_later'活动作业和运动鞋的正确WORKER名称是什么? 谢谢。

1 个答案:

答案 0 :(得分:1)

这是因为ActionMailer使用mailers队列作为默认队列来发送电子邮件。查看this SO question

运动鞋默认使用mailers队列发送电子邮件。但是没有名为mailers的队列的工作者。

可能的方法是:

  1. mailers队列创建新工作人员并手动执行所有操作
  2. deliver_later方法中明确定义队列,如下所示:

    ApplicationMailer.send_letter(params).deliver_later(queue: 'default')
    
  3. ActionMailer

    中覆盖application.rb的默认队列名称
    config.action_mailer.deliver_later_queue_name = 'default'
    

    然后像往常一样发送电子邮件:

    ApplicationMailer.send_letter(params).deliver_later
    
  4. 希望这有帮助