Run Sidekiq in Digital Ocean Production Droplet for Capistrano Deployed Rails Application

enquerer > Blog > Rails > Ruby on Rails > Run Sidekiq in Digital Ocean Production Droplet for Capistrano Deployed Rails Application

We all know Sidekiq is a wonderful gem to enqueue active jobs in rails. All doubt how to run the Sidekiq server in a production environment and mostly all developers deploy in Digital Ocean server by using the Capistrano gem.

This is a step by step tutorial to run Sidekiq as a service in a digital ocean server. We concentrated on digital ocean server here, this might also help you in other servers up to an extent.

Step 1: Install Redis server on your ubuntu server.

 Sudo apt install redis-server

Step 2: add sidekiq.yml on your application /config/sidekiq.yml and add below lines and save it.

---
:verbose: false
:concurrency: 10
:pidfile: /home/user/application-name/shared/tmp/pids/sidekiq.pid
:queues:
- [critical, 2]
- default
- low
production:
:concurrency: 25
staging:
:concurrency: 15

Step 2: Deploy the app and log in to your droplet, add a sidekiq.pid by running,

sudo nano  /home/user/application-name/shared/tmp/pids/sidekiq.pid

Step 3: Change the permission of the file.

chmod 777 /home/user/application-name/shared/tmp/pids/sidekiq.pid

Step 4: In Order to start the Sidekiq as service while booting the server we need to make a Sidekiq service in our ubuntu server. So to do so, run the following in ubuntu server:

sudo nano /lib/systemd/system/sidekiq.service

Add below lines to the file and save the file.

# Customize this file based on your bundler location, app directory, etc.
# Put this in /usr/lib/systemd/system (CentOS) or /lib/systemd/system (Ubuntu).
# Run:
#   - systemctl enable sidekiq
#   - systemctl {start,stop,restart} sidekiq
#
# This file corresponds to a single Sidekiq process.  Add multiple copies
# to run multiple processes (sidekiq-1, sidekiq-2, etc).
#
# See Inspeqtor's Systemd wiki page for more detail about Systemd:
# https://github.com/mperham/inspeqtor/wiki/Systemd
#
[Unit]
Description=sidekiq
After=syslog.target network.target

[Service]
Type=simple
WorkingDirectory=/home/deploy/060-sidekiq-on-production
ExecStart=/bin/bash -lc 'bundle exec sidekiq -e production -C config/sidekiq.yml'
User=deploy
Group=deploy
UMask=0002

# if we crash, restart
RestartSec=1
#Restart=on-failure
Restart=always

# output goes to /var/log/syslog
StandardOutput=syslog
StandardError=syslog

# This will default to "bundler" if we don't specify it
SyslogIdentifier=sidekiq
[Install]
WantedBy=multi-user.target

Make changes to the above code accordingly to your application.

Step 5: To start Sidekiq when the server boots up, we need to create a symlink. In order to do so, run the below command.

 sudo systemctl enable sidekiq.service

Step 6: Now we can start the Sidekiq service.

 sudo service sidekiq start

Step 7: You can check the system log for errors.

sudo cat /var/log/syslog 

And check if Sidekiq started by

sudo ps aux | grep sidekiq 

If the Sidekiq is running you can see something like… 

user    2609  0.7  3.3 1352776 136960 ?      Ssl  06:18   0:15 sidekiq 5.2.7 application-name[0 of 25 busy]

Here it is the Sidekiq running successfully as service in your ubuntu droplet.

Now whenever the server boots up the Sidekiq server will also restart. You can manually stop, start, or restart the server by,

sudo service sidekiq start/stop/restart

Using Sidekiq, you can enqueue active jobs in rails now in a production environment. Now all complex and time taking process, calculation and logic can be done in the background in Redis-server and make your server operate in an optimized, fast and impressive way.

Leave a Comment