Breyten Ernsting

Webdeveloper at Open State Foundation.

Sending Emails Asynchronously With Devise and Sidekiq

If you’re building a website that supports user logins and use Devise to manage that, you’ll soon find out that it sends out emails synchronously by default. This is not a good practice, as it’s not something that a user visiting your site should have to wait for. There is a solution to make this work asynchronously, however.

There is a page in the Devise wiki that describes how to configure it for sending emails asynchronously. It involves using the devise-async gem and it’s rather flexible and supports Resque, Sidekiq and Delayed::Job. You should start by following the instructions on that page if you’re using Sidekiq.

However, there is a thing that you might overlook: by default it’ll enqueue the jobs in the mailer queue. This means you’ll have to either configure you Sidekiq worker to look for jobs in that queue as well, or instruct devise-async to enqueue in a different queue.

The latter is simple and is described in the README:

1
2
# config/initializers/devise_async.rb
Devise::Async.queue = :my_custom_queue

Configuring the Sidekiq worker to look into more than one queue is only slightly more complicated and looks something like the following:

1
2
3
4
5
6
7
8
9
10
11
---
:concurrency: 5
:pidfile: tmp/pids/sidekiq.pid
staging:
  :concurrency: 10
production:
  :concurrency: 20
:queues:
  - default
  - mailer
  - [myqueue, 2]

You should save this as config/sidekiq.yml and then restart the worker as well as the server and then your emails will be sent asynchronously.

Comments