3
votes

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

class PayoutMailer < ApplicationMailer
  default from: '[email protected]'

  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: '[email protected]', subject: subject)
  end
end

Which I'm testing with this line:

PayoutMailer.payout_success_email('[email protected]',
                                          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", "[email protected]", 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

1 Answers

9
votes

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.