breaking into the unknown…

resque installation and authentication with devise

Leave a comment

I have explained the need of background job and when to use it in this post. here I will explain installation , starting and stopping of resque and then its authentication with devise. resque gem is available here . You will also need resque-status to know the status of running job. resque status gem is available here.

STEP 1 : Install the gem

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

gem 'resque'
gem "resque-scheduler"
gem "resque-status"

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

$ bundle install

STEP 2: Write routes for rersque

Say, if my application is created with the name MyFinance, my routes will look like this.

require 'resque/server'

MyFinance::Application.routes.draw do

--------other routes or mount-----
  mount Resque::Server, :at => "/resque"

--------other routes or mount-----

STEP 3: Add initialization code for resque

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

require 'resque/job_with_status'
Resque.redis = "localhost:6379" # this is the default value, 
                              change if redis running on some other
Resque::Plugins::Status::Hash.expire_in = (24 * 60 * 60) # 24hrs in seconds

STEP 4: Adding resque rake task to your environment

resque comes with a number of rake task to perform different resque task from the commandline, like starting stopping the worker etc. To load these task, you need to add below line to Rakefile.

require 'resque/tasks' # this will load all the resque task

task "resque:preload" => :environment. 
# it will load resque environment i,e all the files needed by it

Once , you have added above two line to the RakeFile, You can see all the available resque rake task with below command.

$ rake -vT | grep resque # will list all the available resque task as below
rake resque:work      # Start a Resque worker
rake resque:workers   # Start multiple Resque workers.

1-> If you do not add require ‘resque/tasks’ to rake file , you will get the below error , when try to run any resque rake command
Don’t know how to build task ‘resque:work’
2 -> If you do not add task “resque:preload” => :environment, you will get below error, when you try to start the resque worker
No such file to load — bootstrap_flash_helper

STEP 5: Starting and stopping sidekiq

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

$ QUEUE=* rake resque:work

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. I do not get much time to explore running the resque worker in the background. Some good information is available here on the stackoverflow. I will update it here some other time, when I implement it myself

STEP 6: see the resque worker dasboard is up and running

Go to browser and go to http:://your_site_url/resque you will see the resque 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 7: Authenticating and authorization sidekiq with devise

You can introduce simple http authentication by creating a file with any name(by convention use name which reflect content and purpose of file so call it  resque_admin.rb) in config/initializer and add the below lines to it.

Resque::Server.use(Rack::Auth::Basic) do |user, password|
user = "resque_admin"  
password == "secret"

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

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

constraints constraint do
 mount Resque::Server, :at => "/resque"

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s