setting up delayed_job in rails3

If you want to run longrunning jobs in the background, one very easy solution is using delayed_job. One other very interesting alternative is resque, but it seemed harder to setup (it uses redis), and delayed_job seemed to be just right for my needs. In short (from the redis documentation) : Choose Resque if:

  • You need multiple queues
  • You don't care / dislike numeric priorities
  • You don't need to persist every Ruby object ever
  • You have potentially huge queues
  • You want to see what's going on
  • You expect a lot of failure / chaos
  • You can setup Redis
  • You're not running short on RAM Choose DelayedJob if:
  • You like numeric priorities
  • You're not doing a gigantic amount of jobs each day
  • Your queue stays small and nimble
  • There is not a lot failure / chaos
  • You want to easily throw anything on the queue
  • You don't want to setup Redis And in my case: a short list of outstanding jobs, i am using a database already, not sure about redis, performance is not that important, i do not expect a lot of failure, i do not want to setup redis (yet :). So to use the delayed_job gem in a rails3 you will need to use the code from github (as the gem version 2.0.3 is not yet rails3 compatible). Add the following line to your Gemfile : [ruby] gem "delayed_job", :git => 'git://github.com/collectiveidea/delayed_job.git' [/ruby] and then run [bash] bundle install [/bash] Because i am using ActiveRecord, i can just use the generator, that will create the table and add the script to run a worker: [ruby] rails generate delayed_job rake db:migrate [/ruby] Include the rake tasks from delayed-jobs into your Rakefile: [ruby] begin require 'delayed/tasks' rescue LoadError STDERR.puts "Run bundle:install to install delayed_job" end [/ruby] Create an initializer file delayed_jobs_config.rb and write [ruby] # config/initializers/delayed_job_config.rb Delayed::Worker.destroy_failed_jobs = false #Delayed::Worker.sleep_delay = 60 #Delayed::Worker.max_attempts = 3 Delayed::Worker.max_run_time = 5.minutes [/ruby] If you are happy with the defaults, you can leave it. For me it was important to keep a record of the jobs that failed. Then you can just delay any function-call like this: [ruby] envelope.delay.query_status(delivery) [/ruby] Just adding the .delay does all the magic for you! And starting a worker is as simple as [ruby] rake jobs:work [/ruby]

Comments
Vidar 2010-09-29 18:55:44 UTC

Nice writeup! When I upgraded my app to Rails3 I had some problems with this since it was easier than I thought :) I changed Notifier.deliver_system_error("message") in rails2 to Notifier.system_error("message").deliver in rails3 to Notifier.delay.system_error("message") in rails3 with delayed_job Easy peasy.

DOnald 2011-01-06 22:51:08 UTC

Nice article, thanks! Could you please write something about autoscaling Delayed Jobs? thanks!

@mgswolf 2011-02-12 18:28:21 UTC

I love your blog, congratulations!

Rubish 2011-02-21 15:27:42 UTC

Thanks, great write-up. Was struggling about how to get the tasks running.

erwin 2011-05-21 14:08:44 UTC

Thanks a lot ... Could not get it running first ... ( you are running rake 0.8.7) and I am running now the latest rake 0.9.0... need to patch the rakefile ... inserting 2 modules for MyApp and RakeFileUtils before the MyApp load tasks ... then your code as following : now running .... require File.expand_path('../config/application', __FILE__) require 'rake' module ::MyApp class Application include Rake::DSL end end module ::RakeFileUtils extend Rake::FileUtilsExt end MyApp::Application.load_tasks begin require 'delayed/tasks' rescue LoadError STDERR.puts "Run `bundle:install` to install delayed_job" end

ramprabhu 2012-03-14 10:09:24 UTC

Thanks. It works great.....

arivarasan 2012-09-06 09:42:11 UTC

nice work..

Add comment

Recent comments

Tags