Skip to content

How To: Send devise emails in background (Resque, Sidekiq and Delayed::Job)

Stefan Wrobel edited this page Sep 14, 2019 · 9 revisions

ActiveJob

If you are using Rails >= 4.2 with ActiveJob, there's a README Section that explains what to do.


Since sending email synchronously is not a good idea, you'll probably want to have Devise enqueuing it's notification emails for background processing.

Although Devise doesn't support this out of the box you can achieve it easily by using the devise-async gem.

To do so, first add it to your Gemfile:

gem "devise-async"

For Devise >= 2.1.1

Add :async to the list of modules to include in your model's devise call.

class User < ActiveRecord::Base
  devise :database_authenticatable, :async, :confirmable # etc ...

end

For Devise < 2.1.1

Tell Devise to use the proxy mailer in config/initializers/devise.rb:

# Configure the class responsible to send e-mails.
config.mailer = "Devise::Async::Proxy"

For all versions

And last but not least, set your queuing backend by creating config/initializers/devise_async.rb:

# Supported options: :resque, :sidekiq, :delayed_job
Devise::Async.backend = :resque

If you're getting undefined method 'backend=' for Devise::Async:Module, just skip this step and refer to this issue.

At least for :sidekiq, you will also need to add the mailers queue to your Sidekiq config. That's explained here.

Your notification emails should now be gracefully enqueued for background processing.

Clone this wiki locally