Background delivery of emails in Rails

On this post we are going to learn how to deliver emails in background using the Resque library and Redis database.

Setup

On the Gemfile of your project just add:

gem ‘resque’

and then execute bundle install to install all gems needed for resque to work properly.

Installing Redis

If you are using Mac just run the following commands:

$ brew install redis
$ brew services run redis // to start the redis service

If you are using GNU/Linux the recommended way is to compile from sources like this:

$ wget http://download.redis.io/redis-stable.tar.gz
$ tar xvzf redis-stable.tar.gz
$ cd redis-stable
$ make

and then start the server using:

$ redis-server

You can find more information here.

Resque worker

Before sending emails you need a worker to execute the background tasks on your server so we need to create the file /lib/tasks/resque.rake with the following content:

require 'resque/tasks'
task "resque:setup" => :environment do
ENV['QUEUE'] = '*'
ENV['TERM_CHILD'] = '1'
ENV['RESQUE_TERM_TIMEOUT'] = '10'
end
desc "Alias for resque:work (To run workers on Heroku)"
task "jobs:work" => "resque:work"

and start your worker with the following command:

$ rake resque:setup

Delivering Emails

On Rails 5 you don’t need to setup anything else to start delivering emails. Just use the deliver_later method on your Mailer. For example:

def send_signup_email_notification
MyNotifierMailer.model_signup_notification(self).deliver_later
end

Testing

If you want to test that your emails are being delivered in background you could start the web client of redis, after starting the redis service, and check with the following command:

$ resque-web

and you should get an interface like this one under the stats tab:

Then send and email and you should see somthing like this with the number of processed emails:

Sending background emails for Devise

To accomplish this you need to overwrite the send_devise_notification method on your User model like this:

def send_devise_notification(notification, *args)
devise_mailer.send(notification, self, *args).deliver_later
end

EXTRA Heroku config

First let’s add the Redis addon to our Heroku app by going to this page:

There you should see something like this:

Click on Install Heroku Redis and on the next screen enter your app name to intall the addon:

After installing you should see a screen like this one:

where you can confirm that the addon has been installed.

Finally you need to create a new file on config/initializers/redis.rb with the following content:

$redis = Resque.redis = Redis.new(url: ENV['REDIS_URL']) if ENV['REDIS_URL'].present?

This will initiliaze Redis if the environment variable variable is present.

THE END

And that’s it, you have configured background emails delivery with Resque and Redis in Rails. I hope this article has been useful to you.

If you have any question or suggestion let me know or leave a comment.

Author: Eng. Alberto Aragón Alvarez

If you like it, clap. For more histories like this one follow our publication on Medium.

If you want to collaborate with us please visit our web site:

www.alturasoluciones.com

Like what you read? Give Alberto Jose Aragon Alvarez a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.