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:
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
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.
Nice article, thanks! Could you please write something about autoscaling Delayed Jobs? thanks!
I love your blog, congratulations!
Thanks, great write-up. Was struggling about how to get the tasks running.
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
Thanks. It works great.....
nice work..
Add comment