Rails 5: ActionMailer's “deliver_later” never delivers in production

坚强是说给别人听的谎言 提交于 2019-12-11 02:45:43

问题


I have a very basic mailer setup as follows to send out transactional mailers:

class PayoutMailer < ApplicationMailer
  default from: 'hello@groundworkai.com'

  def payout_success_email(email, payment_size, user_name)
    @payment_size = payment_size
    @user_name = user_name
    subject = 'Your Rewards Have Been Sent!'
    mail(to: email, from: 'hello@myservice.com', subject: subject)
  end
end

Which I'm testing with this line:

PayoutMailer.payout_success_email('test@example.com',
                                          200000,
                                          'test name').deliver_later

My issue is that when I use .deliver or .deliver_now, the mail sends, but when I delegate it asynchronously using deliver_later, it gets queued up but never sends. The output is:

I, [2018-01-20T15:27:44.140104 #4]  INFO -- : [ActiveJob] Enqueued ActionMailer::DeliveryJob (Job ID: 265cb31a-dec4-4adb-866d-06e44645c53a) to Async(mailers) with arguments: "PayoutMailer", "payout_success_email", "deliver_now", "test@example.com", 200000, "test name"

I know that ActionJob is handling it when I use deliver_later, as per the docs:

Active Job's default behavior is to execute jobs via the :async adapter. So, you can use deliver_later now to send emails asynchronously. Active Job's default adapter runs jobs with an in-process thread pool. It's well-suited for the development/test environments, since it doesn't require any external infrastructure, but it's a poor fit for production since it drops pending jobs on restart. If you need a persistent backend, you will need to use an Active Job adapter that has a persistent backend (Sidekiq, Resque, etc).

At this point, I don't need a persistent backend and would be fine using the in-process thread pool. Is there any way for me to use deliver_later without bringing in the external infrastructure of Sidekiq + Redis?


回答1:


Async adapter won't work from a rake task.

Try inline instead, or use deliver_now

From http://edgeguides.rubyonrails.org/active_job_basics.html#job-execution

Using the asynchronous queue from a Rake task (for example, to send an email using .deliver_later) will generally not work because Rake will likely end, causing the in-process thread pool to be deleted, before any/all of the .deliver_later emails are processed. To avoid this problem, use .deliver_now or run a persistent queue in development.



来源:https://stackoverflow.com/questions/48364051/rails-5-actionmailers-deliver-later-never-delivers-in-production

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!