Delayed Job In rails3

11 January 2012 by Aniket Joshi No comments

There are times when you have to run certain tasks/jobs in background for your rails can be as simple as sending emails, Http downloads, image resizing and much more.

Commonly used gems for Background processing

1. delayed_job (
2. resque (
3. beanstalkd and stalker (http://

( stalker)

It totally depends on your needs and choice on which gem to choose for background job.But if your job queue is small with less failures i would suggest using delayed_job.

Here i would show you how i integrated delayed_job in my application for sending emails in background.

1. Installing delayed_job gem

Include the delayed_job gem in your gem file.

gem 'delayed_job'

and run bundle install .

2. We need to run the generator

this would create a delayed_job migration.
Run this command on your terminal in your project directory.

generate delayed_job

after this we need to run the migration.

rake db:migrate

3. Queuing jobs

Now we need to call the delay method on any object to run the job in the background.

this is how my controller looks

class ContactsController < ApplicationController   
  def create     
      redirect_to root_path     
      render :action => 'new'

Here you can see that I have called the delay method on UserMailer.So what we have done here is we have replaced the deliver method that we normally use for sending mail with delay method.

so instead of using UserMailer.contact_information(@contact).deliver

we use UserMailer.delay.contact_information(@contact)

4. Start the worker

In Development we can start this with this simple command

rake jobs:work

what this command will do is it will start the worker and start processing the jobs in the queue.

Great.but this won’t work in production and to make it working we need to run the delayed_job
script that is provided when we install the delayed_job gem. Its inside the /script directory.
Just run this command and it will start the worker in the background.
to start ā€“ ruby script/delayed_job start
to stop ā€“ ruby script/delayed_job stop

If you want to delete all the jobs in the queue. just run this command.

rake jobs:clear

There’s a lot more that you can do in delayed_job but i have tried to keep this short. Kindly check the documentation for more options.

I hope you find this information useful when using delayed_job in your project.

Aniket Joshi

Delivery Director, Thinker & Tinkerer at Idyllic.

Follow me on Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Blog

Get access to proven marketing ideas, latest trends and best practices.

Next up home


Lets build cool stuff

Share your contact information & we will get in touch!

I want (Tell us more about your dream project)