Posted by tobi — 04:04 PM Feb 17
I finally got a github invitation and used the opportunity to release another Shopify extractions.
Delayed::Job or DJ is a asynchronous priority queue which only relies on a simple database table. It doesn’t require you to run a dedicated server like many other systems do.
We use for a lot of longer running tasks in Shopify such as sending newsletters, uploading files to s3, downloading images from urls, indexing products to Solr and so on.
There are two ways to add jobs to the queue:
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 < Struct.new(:text, :emails)
def perform
emails.each { |e| NewsletterMailer.deliver_text_to_email(text, e) }
end
end
Delayed::Job.enqueue NewsletterJob.new('lorem ipsum...', Customers.find(:all).collect(&:email))
There is also a second way to get jobs in the queue: send_later.
BatchImporter.new(Shop.find(1)).send_later(:import_massive_csv, 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 plugin can be found on github.

Ryan Lowe 18 Feb 00:17
Nice—I’ll try it out soon on one of my projects, thanks guys!
Ben 19 Feb 10:11
Excellent! I’m in the midst of writing some code that’ll need just this functionality in a later iteration, and I was convinced I’d have to write it myself. Thanks!
John 21 Feb 17:12
Hey Tobi -
I created the database and simply wanted to run the rake task, to process my jobs.
AM I doing something crazy?
john@emopop:~svn/app> rake jobs:work (in svn/app) rake aborted! undefined method `zero?’ for #<delayed::job::runner:0x2458e70>
There is nothing in the queue yet, wanted to let it run, until I added something.
John
tictacbum 29 Feb 05:13
you can fix the zero? error making Delayed::Job::Runner run method return self.runs instead of self ( lib/delayed/job.rb:49 )
Joran 05 Mar 09:03
The plugin README mentions that you run a script called job_runner which is being invoked by runnit. What is this runnit thing?
Greetings, Joran.
Luke 17 Mar 23:17
I’m also unsure as to where to find the ‘runnit’ that Tobias talks about. Other than that its working like a charm. Much easier than the other async stuff I found for rails.
Luke