The Big Three-Oh

Delayed Job turned 3 over the holidays. It’s been a long time coming but it finally happened. A huge thanks goes out to betamatt for his contributions and help in maintaining this project over the past year, as well as bernardelli for adding named queues. Thanks guys!

Changes

Here’s a quick rundown of the changes. You can see the full list on GitHub.

  • New: Named queues [ bernardelli ]
  • New: Job/Worker lifecycle callbacks [ betamatt ]
  • Change: daemons is no longer a runtime dependency
  • Change: Active Record backend support is provided by a separate gem
  • Change: Enqueue hook is called before jobs are saved so that they may be modified
  • Fix: Problem deserializing models that use a custom primary key column
  • Fix: Deserializing AR models when the object isn’t in the default scope
  • Fix: Hooks not getting called when delay_jobs is false

DJ 3 introduces Resque-style named queues while still retaining DJ-style priority. The goal is to provide a system for grouping tasks to be worked by separate pools of workers.

Jobs can be assigned to a queue by setting the queue option:

object.delay(:queue => 'tracking').method
Delayed::Job.enqueue job, :queue => 'tracking'
handle_asynchronously :tweet_later, :queue => 'tweets'

If you’re using rake, you can work off those queues by setting the QUEUE or QUEUES environment variable. If you’re using daemons, set the --queue or --queues option.

# with rake
$ QUEUE=tracking rake jobs:work
$ QUEUES=mailers,tasks rake jobs:work

# with daemons
$ RAILS_ENV=production delayed_job --queue=tracking start
$ RAILS_ENV=production delayed_job --queues=mailers,tasks start

DJ 3 no longer has daemons as a runtime dependency. You’ll need to add gem 'daemons' to your Gemfile if you want to use script/delayed_job. Check the comments on the commit for further explanation.

As always, feedback and patches are welcomed. :)

bryckbost@gmail.com

Comments

  1. rneville@eircom.net
    ray
    June 06, 2012 at 14:20 PM

    Hi Guys,
    Thanks for fork of delayed_job.
    Having a bit of trouble.
    Changed from Bj(no longer supported in Rail3) to Delayed_job but seems not to handle rails runner.

    lib/report_job.rb 
    class ReportJob < Struct.new(:prawn_script_name , :account_id ) 
    def perform 
    bundle exec rails runner “#{Rails.root}/jobs/#{prawn_script_name}.rb #{account_id}” 
    end
    Error in last_error field. 
    {undefined method `runner’ for #ReportJob:0xc28f080

    OUCH! …. will try Resque.
    Any chance it can take rails runner in def perform?
    rgs ray

  2. June 07, 2012 at 18:00 PM

    Delayed_job and Prawn scripts:
    One suggestion is to use
    def perform
    load “#{Rails.root}/jobs/#{prawn_script_name}.rb”
    end
    which works 
    http://stackoverflow.com/questions/10930300/delayed-job-and-prawn-scripts

    BUT 
    i need to pass arguments to the Prawn pdf scripts to scope the data.
    (runner allows ARGs to be passed but Load does not)
    This is my next hurdle to overcome.
    rgs ray

  3. chris@collectiveidea.com
    Chris Gaffney
    June 07, 2012 at 18:26 PM

    Ray: You’ll have better luck at the delayed job google group (https://groups.google.com/forum/?fromgroups#!forum/delayed_job)