Skip to content

Database backed asynchronous priority queue -- Extracted from Shopify. This version is patched for being able to handle reoccuring tasks.

License

Notifications You must be signed in to change notification settings

theneubeck/delayed_job

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Delayed::Job

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

Setup

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
    table.timestamps
  end

Usage

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 original also has the possibility to do jobs at any given point.


  Delayed::Job.enqueue NewsletterJob.new('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 NewsletterJob.new('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 NewsletterJob.new('lorem ipsum...', Customers.find(:all).collect(&:email)), :every => :first_of_month
  Delayed::Job.schedule NewsletterJob.new('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'
  
  Delayed::Worker.new.start  

Cleaning up

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

About

Database backed asynchronous priority queue -- Extracted from Shopify. This version is patched for being able to handle reoccuring tasks.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%