breaking into the unknown…

sidekiq installation and authentication with devise

Leave a comment

I have explained the need of background and when to use it in this post. here I will explain installation , starting and stoping of sidekiq and then its authentication with devise.sidekiq gem is available here . A sample example code of background job using sidekiq is available here .

STEP 1 : Install the gem

Assuming you are working with Rails 3 add below to your Gemfile.

gem 'sidekiq'
gem 'sinatra', require: false
gem 'slim'
gem 'sidekiq-status'

Now, Install the gem by running budle install on the terminal

$ bundle install

STEP 2: Write routes for sidekiq

mount Sidekiq::Web => ‘/sidekiq’ # sidekiq is a separate app and can be mounted over your app

STEP 3: Add initialization code for sidekiq

create a sidekiq.rb file in config/initializer folder and add below code to it

require 'sidekiq'
require 'sidekiq-status'

Sidekiq.configure_client do |config|
       config.client_middleware do |chain|
       chain.add Sidekiq::Status::ClientMiddleware

Sidekiq.configure_server do |config|
        config.server_middleware do |chain|
        chain.add Sidekiq::Status::ServerMiddleware,
                           expiration: 30.minutes 

STEP 4: Starting and stopping sidekiq

If you are working locally you can start it with below command.

$ bundle exec sidekiq

To stop it just press cntr + c . Even if you close the terminal it get stop. This will cause problem on server as there you want it to keep running all the time. So we need to run it in background. It can be done with the below command.

$bundle exec sidekiq -d -L log/sidekiq.log #start worker in background


As per this commit of sidekiq, it iinherit development environment by default. So If you are starting sidekiq on any other environment say staging or production you must pass the environment also otherwise sidekiq not inherit the environment. So use below command, which start sidekiq say in production environment.

$bundle exec sidekiq -e staging -d -L log/sidekiq.log

since, now the job is running in background, It will always keep running, even if you close the terminal.

To kill a background, you need to first find its process id, then kill it

$ ps -ef | grep sidekiq #to see worker running at which pid
say, the above command return the pid 56778 an8 3421

kill those process with kill command as below.

$ kill -9 56778

NOTE: you should stop and restart the worker, whenever you make changes to the code executed by the worker , otherwise it will execute the old code only. So, to be on safer side when ever you deploy the code to server, restart the worker.

STEP 5: see the sidekiq worker dasboard is up and running

Go to browser and go to http:://your_site_url/sidekiq you will see the sidekiq dashboard. but the problem here is that everyone can access this page. It is the page which have detail of sensitive data and running process, so we need to make it accessible only to admin.

STEP 6: Authenticating and authorization sidekiq with devise

The logic to authenticate and authorize a user to access sidekiq dashboard is to make the mounting of sidekiq engine conditional one as below, by modifying the routes we have written in step 2

constraint = lambda { |request| request.env["warden"].authenticate? and 
                      (request.env['warden'].user.admin? or request.env['warden'].user.monitor?)}

constraints constraint do
mount Sidekiq::Web => '/sidekiq'

Thus I have first checked that the user should be loged in and when loged in they should be either admin or a monitor. you must have admin? and monitor? method defined in your user model, which contain logic to check whether a user is admin or not. For example, Iam using rolify gem and so I have implemented my method as below.

class User < ActiveRecord::Base

 def admin?

    self.has_role?  :admin #has_role? is method provided by rollify gem


  def monitor?

    self.has_role?  :monitor #has_role? is method provided by rollify gem



So, now if a user is not logged in or do not have admin or monitor role, when try to access /sidekiq he will get page not found error


Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s