Using Sucker Punch gem for Background Job Processing in Rails 4.1

The sucker_punch gem is easy to setup and get it working on production quickly. It does not have any dependency on Redis or some other library that requires maintenance work on the production server. It uses Celluloid for processing the background jobs in the same process as the Rails process.


Step 1

Add the gem to the Gemfile:

gem 'sucker_punch', '~> 1.0'

Step 2

From rails root directory:

$bundle install

Step 3

In app/jobs/my_background_job.rb:

class MyBackgroundJob
  include SuckerPunch::Job

  def perform(user_id)
    ActiveRecord::Base.connection_pool.with_connection do
      user = User.find(user_id)
      user.update_attributes(is_awesome: true)

Step 4

In application.rb

SuckerPunch.logger ="#{Rails.root}/log/sucker_punch.log")

Define this line outside the module-end block.

Step 5

In your controller, put the job in the background:

Step 6

You can log messages in your jobs like this: 'log some message to sucker_punch.log here'

Since it delegates the logging to Celluloid, it is duck-typed to the Rails logger and can respond to info, debug, warn, and error.

Related Articles

Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.