Playing with delayed_job plugin

The need for executing long task process is a common thing in large-scale rails projects these days. This is something any serious developer should be aware of. Make no mistake, long tasks can consume large amounts of servers resources, and they can provoke servers to crash when you request them via http.

A great solution for this problem is the delayed_job plugin by Tobias Lütke. This plugin is very easy to install, is very well documented, and provides you with everything you’ll need to execute task in background. I had the oportunity to try it out on burdastyle.com, and it worked just fine. So, congratulations Tobi for this great work and thanks for sharing.

So, let’s try delayed_job plugin out.

First thing we need to do after we install this plugin is to generate a migration to add delayed_jobs table into our database. This migration should look like this:

class CreateDelayedJobs < ActiveRecord::Migration
  def self.up
    create_table :delayed_jobs, :force => true do |table|
      table.integer  :priority, :default => 0
      table.integer  :attempts, :default => 0
      table.text     :handler
      table.string   :last_error
      table.datetime :run_at
      table.datetime :locked_at
      table.datetime :failed_at
      table.string   :locked_by
      table.timestamps
    end
  end

  def self.down
  end
end

Then, we’ll need to define our jobs. A job is just a ruby class with a method called perform. Let’s say we need to send a very simple email notification to our entire users database. We can use this little class:

class NotificationJob < Struct.new(:message)
  def perform
    total = User.count
    limit = 100
    (0..total-1).step(limit) do |offset|
       User.find(:all, :limit => limit, :offset => offset).each do |u|
         Notifier.deliver_message_to_user(u, message)
       end
    end
  end
end

Now, every time you need to send these notifications out, you just enqueue your job like this:

Delayed::Job.enqueue NotificationJob.new("hello world!")

Finally, delayed_job plugin provides you with a set of rake tasks to put enqueue jobs to work:

rake jobs:work

And that’s all folks! Have fun with delayed_job.

Happy hacking!

1 comment so far ↓

#1 Luis Lavena on 11.13.08 at 5:47 pm

Hey Juan, nice article!

You can also take a look how to make a worker daemon and control all that with God ;-)

http://livollmers.net/index.php/2008/11/05/asynchronous-mail-with-delayedjob-god-daemons/

HTH.

Leave a Comment