Ha! You thought we were just about games, didn’t you? The truth is that we spent lots of time working on web apps, much more than games. And a lot of times these web apps need to send an awful lot of bulk email to spam their users. So here it comes, Resque to the rescue, to aid on the process.
Configuring Resque in Heroku was a painful experience that you should avoid by reading the following instructions carefully. I’m going to be pretty specific and show the exact configuration that worked for me. I’m going to avoid the localhost configuration and only stick to how to do it on production. I’m also assuming you already have Sendgrid installed on your Heroku instance.
First things first, make sure you’re using Rails 4.2 or above and Ruby 2.2.0 or above.
Resque is a Redis-backed Ruby library for creating background jobs, placing them on multiple queues, and processing them later. As it needs Redis to work you should have the following in your Gemfile.
ruby '2.2.2'
gem 'rails', '4.2.3'
gem 'redis'
gem 'resque', "~> 1.22.0"
Why Resque 1.22.0? Because on the official Rails API it says that Active Job has adapters for Resque 1.x. This is key. Don’t use the latest version, it doesn’t work. Stick to what’s in the docs.
If you’re unfamiliar with Active Job, check out this Rails guide : http://edgeguides.rubyonrails.org/active_job_basics.html
Now, go to your application.rb file and add the following code:
# We're telling Rails that we will use Resque to process background jobs
config.active_job.queue_adapter = :resque
Next, set up Redis in your Heroku instance. I’m using rediscloud here. There are other services available, but as I told you, I’m sticking to what worked for me.
heroku addons:create rediscloud
heroku config:get REDISCLOUD_URL
Create a new redis.rb initializer in config/initializers/ and add the following code:
if ENV["REDISCLOUD_URL"]
$redis = Redis.new(:url => ENV["REDISCLOUD_URL"])
end
Create a new resque.rb initializer in config/initializers/ and add the following code:
# Establish a connection between Resque and Redis
if Rails.env == "production"
uri = URI.parse ENV["REDISCLOUD_URL"]
Resque.redis = Redis.new host:uri.host, port:uri.port, password:uri.password
else
# conf for your localhost
end
Go to your lib/tasks folder and create a new file called resque.rake and write the following code:
require 'resque/tasks'
task 'resque:setup' => :environment
Now you need to create a Procfile if you don’t have one. Just create a file called Procfile in your Rails app root folder. A Procfile is a mechanism for declaring what commands are run by your application’s dynos on the Heroku platform. Add this to your Procfile:
resque: env TERM_CHILD=1 RESQUE_TERM_TIMEOUT=7 QUEUE=* bundle exec rake resque:work
This process will run every time you push your code into production.
You’re almost set. Now Change your email deliver method from .deliver_now to .deliver_later and push your code to Heroku.
Finally, go into your app’s dashboard and make sure you see something like this:
So that’s it. Sending emails should be working as a background process. Hope this was helpful. Don’t forget to leave a comment below.