Active Job , Sidekiq And Redis In Rails 4


Active Job is a framework for declaring jobs and making them run on a variety of queueing backends. These jobs can be everything from regularly scheduled clean-ups, to billing charges, to mailings. Anything that can be chopped up into small units of work and run in parallel, really.

Sidekiq simple, efficient background processing for Ruby. Sidekiq uses threads to handle many jobs at the same time in the same process. It does not require Rails but will integrate tightly with Rails 3/4 to make background processing dead simple.

Redis :-

Redis is an open source, BSD licensed, advanced key-value cache and store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets, sorted sets, bitmaps and hyperlog logs.

Other features include :-  

  1. Transactions
  2. Pub/Sub
  3. Lua scripting
  4. Keys with a limited time-to-live
  5. LRU eviction of keys
  6. Automatic failover

Installation :-

  1. wget
  2. tar xvzf redis-stable.tar.gz
  3. cd redis-stable
  4. make
  5. redis-server

Integrating Sidekiq with Rails :-

Gem file

  1. activejob_backport
  2. sidekiq-cron
  3. sinatra
  4. slim


Add this line to configure the sidekiq

include Sidekiq::Worker


ActiveJob::Base.queue_adapter = :sidekiq


:concurrency: 25

:pidfile: ./tmp/pids/

:logfile: ./log/sidekiq.log


– default

– [high_priority, 2]



require ‘sidekiq/web’

mount Sidekiq::Web, at: ‘/sidekiq’ , constraints: ->(request) {SideKiqConstraint.authenticate?(request)}

Note:- SideKiqConstraint.authenticate?(request) is the method which authenticate and ensure only user with role excelfore is able to see sidekiq GUI.

class SideKiqConstraint

def self.authenticate?(request)

if (!request.session[‘user_id’].blank? and !(user= User.find_by(id:request.session[‘user_id’])).blank?)

return user if user.excelfore?




Command to start sidekiq

In Development

sidekiq -C config/sidekiq.yml

In Production

bundle exec sidekiq -d -L sidekiq.log -q mailer,5 -q default -e production

The above steps ensure sidekiq to work background jobs. Now lets see the steps to setup sidekiq for the cron jobs.



 cron: “*/1 * * * *”

 class: “SidekiqClock1”

Add the below code in config/initializer/active_job_configuration.rb to load the jobs from the schedule.yml file


if File.exists?(schedule_file)

Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file)


Sample cron job method:-

class SidekiqClock1

include Sidekiq::Worker

def perform

ActionMailer::Base.mail(to: ‘’ ,subject: ‘Process Completed’,body:’Gurudath Test’,from:””).deliver




Google Recaptcha With Ruby On Rails Integration.

Google Recaptcha With Ruby On Rails Integration
Google Recaptcha With Ruby On Rails Integration

Google Recaptcha With Ruby On Rails Integration.

Google Recaptcha protects the websites you love from spam and abuse.

Below the steps to be followed to develop a sample ROR application with Google Recaptcha:-

Step 1. Create a Ruby on Rails application:-

a)Open a terminal, navigate to a directory where you have rights to create application and type:
rails new recap

b)After you create the application, switch to its folder:
cd recap

c)Type and run bundle install:
bundle install

Step 2. Create models, views, and controllers:-

a)For this application we will generate a scaffold, which is a starter template provided by Rails that bundles a model, a controller, and the relevant views for our application:
rails generate scaffold User name:string

b)After generating the scaffold we need to create the actual User table in our development database so we need to run migration:
rake db:migrate

c)To start the application run below command and open browser and type http://localhost:3000/users
rails s

d)Configure application with nginx or apache with application url

(For configuration settings please see the reference url at the botton of the post.)

Step 3. Integrating Google Recaptcha With Ruby On Rails:-

  a)Please login to Google Recaptcha website to register ur domain to get access.(

  b)Please login and register your site , with the details as shown in below form.

register ur domain to get access

register ur domain to get access

  c)After registeration google provide the

  • Script tag place this snippet before the closing </head> tag on your HTML template.
  • div place this snippet at the end of the <form> where you want the reCAPTCHA widget to appear
registeration google provide

registeration google provide

  d)Once the above steps are done, we can see the recaptcha in the site.

  f)For the server side validation we can use the secret key and the response which is going to be sent as parameters to the form submit action in the controller.

  g)To check whether Google has verified that user, send a GET request with these parameters:URL:

Step 4. Application Code change for server side validation.

helper to request google and get the status

helper to request google and get the status

   a)We will write a helper to request google and get the status:-

        def verify_google_recptcha(secret_key,response)
status = `curl “ secret=#{secret_key}&response=#{response}”` 
hash = JSON.parse(status)
hash[“success”] == true ? true : false

    b)Now we can use this helper the controller to verify status:-

       status = verify_google_recptcha(SECRET_KEY,params[‘g-recaptcha-response’])

where status going to be true or false. If false we will render the form page again for resubmit else we will accept the form.











To configure application with nginx or apache reference url:-

Now the google recaptcha protects your websites from spam and abuse. We will update with more information with upcoming days.


Gurudath BN