Skip to content

Active Job on Heroku with Sidekiq

Here is a quick guide to setting up a more robust Active Job backend on Heroku’s free dynos. To do this we will employ the help of Sidekiq and Redis.

The local setup

First things first, we will need to install Redis, a Sidekiq dependency. For simplicity lets do this through Homebrew.

$ brew install redis

Next add Sidekiq to the Gemfile.

gem 'sidekiq'

And install.

$ bundle install

Now we need to tell Rails to use Sidekiq for Active Job by defining the queue adapter in config/application.rb.

config.active_job.queue_adapter = :sidekiq

Then in our Procfile we will need to add the worker process.

worker: bundle exec sidekiq -q default -q mailers

The only thing to note in the last step is that we have defined two queues with the -q flag. Active Job’s default queue, which is conveniently named default, and the Action Mailer queue called mailers. More information about queues can be found at RubyOnRails.org and on Sidekiq’s GitHub Wiki.

Run it

Our setup should be complete. To test it out we will need to first start Redis.

$ redis-server

In another terminal session we run our app as we normally would.

$ heroku local

Now we can test in the browser or run some Active Job tasks from the Rails console to ensure our worker process is executing properly.

It should be noted that running Redis and Sidekiq for local development is not completey necessary. However, in the name of parity, it is a good practice to run your local environment as much like production as possible.

Deploying to Heroku

Before we deploy our updates, first we need to setup Redis and our worker dyno on Heroku. In the example below I will use Heroku Redis, which has a free plan, but you can use whichever Redis addon you are comfortable with.

$ heroku addons:create heroku-redis:hobby-dev
$ heroku ps:scale worker=1

Once that is done we can commit and deploy our updates to the Heroku app as we normally would. Congratulations, you should now have a solid Active Job backend for your app.

Feel free to drop me a line with any feedback. Stay tuned for my next post which will tackle scheduling jobs on Heroku with Clockwork.


References