Deploying Sidekiq with Capistrano

So you're using Capistrano to deploy your Rails application and now you also want to deploy updates to your worker server running Sidekiq. There are a few things that need to be set up and a couple things to watch out for when getting this set up.

The Setup

Note: This setup assumes you're already deploying your Rails application using Capistrano. If not check out this great tutorial to get started.

Now that you have your Rails app deploying with Capistrano it's time to get the code updates pushed to your sidekiq worker server.

First add capistrano-sidekiq to your Gemfile:

gem 'capistrano-sidekiq'

After you do a:

bundle install

Require capistrano-sidekiq in your Capfile:

require 'capistrano/sidekiq'

Open up your deploy/environment.rb and set up a role for your sidekiq workers and one for your Rails app. As you can see for the Revaluate setup app is our single Sidekiq worker and we have 2 Rails servers running Puma.

# Simple Role Syntax
# ==================
# Supports bulk-adding hosts to roles, the primary server in each group
# is considered to be the first unless any hosts have the primary
# property set.  Don't declare `role :all`, it's a meta role.

role :app, %w{[email protected]}  
role :web, %w{[email protected] [email protected]}  

Open up your deploy.rb and set up your sidekiq_role to match what you have configured in deploy/production.rb.

set :sidekiq_role, :app  
set :sidekiq_config, "#{current_path}/config/sidekiq.yml"  
set :sidekiq_env, 'production'  

Now when you do a bundle exec cap production deploy it should only run the sidekiq commands on your app server and the commands configured for your web server on the 2 web servers.

Gotchas

Here at Revaluate we run a weekly job in Sidekiq that recalculates the score for every building in our MongoDB database based on any new data we have.

The problem is when Sidekiq is processing jobs and we do a deploy, we typically get about 30-40 failed workers while the code is deploying. They typically retry and are fine but if you don't want to get an Errbit/AirBrake error email for all of them you can do the following:

Go into your "Busy" workers list and find your worker and click the "Quiet" button.

Sidekiq Workers

You'll eventually see the busy number drop to 0. At that point you're safe to deploy. After the deployment Sidekiq should automatically be started back up and processing your remaining jobs.

Tim Segraves
Tim Segraves

I'm Co-Founder and CTO of Revaluate. I love writing code, helping others learn to code and building awesome things.