Database backed asynchronous priority queue -- Extracted from Shopify. This version is patched for being able to handle reoccuring tasks.
Switch branches/tags
Nothing to show
Pull request Compare This branch is 5 commits ahead, 23 commits behind tobi:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Delated_job (or DJ) encapsulates the common pattern of asynchronously executing longer tasks in the background.

This is a patch of Shopify’s Delayed::Job which also handles reoccuring tasks, thus removes the needs for crontasks.
The original is a direct extraction from Shopify where the job table is responsible for a multitude of core tasks. Amongst those tasks are:

  • sending massive newsletters
  • image resizing
  • http downloads
  • updating smart collections
  • updating solr, our search server, after product changes
  • batch imports
  • spam checks


The library evolves around a delayed_jobs table which looks as follows:

  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.string   :reoccur_in
    table.datetime :last_run_at


Jobs are simple ruby objects with a method called perform. Any object which responds to perform can be stuffed into the jobs table.
Job objects are serialized to yaml so that they can later be resurrected by the job runner.

  class NewsletterJob <, :emails)
    def perform
      emails.each { |e| NewsletterMailer.deliver_text_to_email(text, e) }
  Delayed::Job.enqueue'lorem ipsum...', Customers.find(:all).collect(&:email))

There is also a second way to get jobs in the queue: send_later., massive_csv)                                                    

This will simply create a Delayed::PerformableMethod job in the jobs table which serializes all the parameters you pass to it. There are some special smarts for active record objects
which are stored as their text representation and loaded from the database fresh when the job is actually run later.

The original also has the possibility to do jobs at any given point.

  Delayed::Job.enqueue'lorem ipsum...', Customers.find(:all).collect(&:email)), 0,  4.weeks.from_now

This patch added the possibility to make the task reoccur:

  Delayed::Job.schedule'lorem ipsum...', Customers.find(:all).collect(&:email)), :every => 3.hours

We also found the need for have reoccuring tasks every 1st and last of months, therefore you can do

  Delayed::Job.schedule'lorem ipsum...', Customers.find(:all).collect(&:email)), :every => :first_of_month
  Delayed::Job.schedule'lorem ipsum...', Customers.find(:all).collect(&:email)), :every => :last_of_month

And the task will be rescheduled to the last of this/next month alt. the first of next month.

Running the jobs

You can invoke rake jobs:work which will start working off jobs. You can cancel the rake task with CTRL-C.

You can also run by writing a simple script/job_runner, and invoking it externally:

  #!/usr/bin/env ruby
  require File.dirname(__FILE__) + '/../config/environment'  

Cleaning up

You can invoke rake jobs:clear to delete all jobs in the queue.