Skip to content
This repository
Newer
Older
100644 75 lines (49 sloc) 3.098 kb
d0378cd3 »
2008-03-23 Lets try this again as textile instead of markdown
1 h1. Delayed::Job
75b49dc1 »
2008-02-17 Initial extraction
2
3 Delated_job (or DJ) encapsulates the common pattern of asynchronously executing longer tasks in the background.
4
5 It is a direct extraction from Shopify where the job table is responsible for a multitude of core tasks. Amongst those tasks are:
6
7 * sending massive newsletters
8 * image resizing
9 * http downloads
10 * updating smart collections
11 * updating solr, our search server, after product changes
12 * batch imports
13 * spam checks
8ec934ee »
2008-03-23 Removed the global lock. Jobs can now processed in parallel by runnin…
14
d0378cd3 »
2008-03-23 Lets try this again as textile instead of markdown
15 h2. Changes
59989313 »
2008-06-21 Renamed locked_until to locked_at. We now store when we start a given…
16
450908d2 »
2008-10-01 Refactored jobs:work, added jobs:clear.
17 * 1.6 Renamed locked_until to locked_at. We now store when we start a given job instead of how long it will be locked by the worker. This allows us to get a reading on how long a job took to execute.
d0378cd3 »
2008-03-23 Lets try this again as textile instead of markdown
18 * 1.5 Job runners can now be run in parallel. Two new database columns are needed: locked_until and locked_by. This allows us to use pessimistic locking, which enables us to run as many worker processes as we need to speed up queue processing.
19 * 1.0 Initial release
75b49dc1 »
2008-02-17 Initial extraction
20
d0378cd3 »
2008-03-23 Lets try this again as textile instead of markdown
21 h2. Setup
75b49dc1 »
2008-02-17 Initial extraction
22
23 The library evolves around a delayed_jobs table which looks as follows:
24
25 create_table :delayed_jobs, :force => true do |table|
ce63517d »
2008-09-30 Update the README's create_table to match the schema.
26 table.integer :priority, :default => 0
27 table.integer :attempts, :default => 0
28 table.text :handler
29 table.string :last_error
30 table.datetime :run_at
31 table.datetime :locked_at
32 table.string :locked_by
75b49dc1 »
2008-02-17 Initial extraction
33 table.timestamps
ce63517d »
2008-09-30 Update the README's create_table to match the schema.
34 end
75b49dc1 »
2008-02-17 Initial extraction
35
d0378cd3 »
2008-03-23 Lets try this again as textile instead of markdown
36 h2. Usage
75b49dc1 »
2008-02-17 Initial extraction
37
38 Jobs are simple ruby objects with a method called perform. Any object which responds to perform can be stuffed into the jobs table.
39 Job objects are serialized to yaml so that they can later be resurrected by the job runner.
40
41 class NewsletterJob < Struct.new(:text, :emails)
42 def perform
43 emails.each { |e| NewsletterMailer.deliver_text_to_email(text, e) }
44 end
45 end
46
47 Delayed::Job.enqueue NewsletterJob.new('lorem ipsum...', Customers.find(:all).collect(&:email))
48
49 There is also a second way to get jobs in the queue: send_later.
50
51
52 BatchImporter.new(Shop.find(1)).send_later(:import_massive_csv, massive_csv)
53
54
55 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
56 which are stored as their text representation and loaded from the database fresh when the job is actually run later.
57
58
450908d2 »
2008-10-01 Refactored jobs:work, added jobs:clear.
59 h2. Running the jobs
75b49dc1 »
2008-02-17 Initial extraction
60
450908d2 »
2008-10-01 Refactored jobs:work, added jobs:clear.
61 You can invoke @rake jobs:work@ which will start working off jobs. You can cancel the rake task with @CTRL-C@.
75b49dc1 »
2008-02-17 Initial extraction
62
450908d2 »
2008-10-01 Refactored jobs:work, added jobs:clear.
63 You can also run by writing a simple @script/job_runner@, and invoking it externally:
0880b0f0 »
2008-03-23 More formatting updates for readme
64
65 <pre><code>
75b49dc1 »
2008-02-17 Initial extraction
66 #!/usr/bin/env ruby
67 require File.dirname(__FILE__) + '/../config/environment'
0880b0f0 »
2008-03-23 More formatting updates for readme
68
450908d2 »
2008-10-01 Refactored jobs:work, added jobs:clear.
69 Delayed::Worker.new.start
0880b0f0 »
2008-03-23 More formatting updates for readme
70 </code></pre>
450908d2 »
2008-10-01 Refactored jobs:work, added jobs:clear.
71
72 h3. Cleaning up
73
74 You can invoke @rake jobs:clear@ to delete all jobs in the queue.
Something went wrong with that request. Please try again.