delayed_job_unique_key 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/delayed_job_unique_key.rb +8 -0
- data/lib/delayed_job_unique_key/active_record_job.rb +12 -0
- data/lib/delayed_job_unique_key/base.rb +52 -0
- data/lib/delayed_job_unique_key/version.rb +3 -0
- data/lib/generators/delayed_job_unique_key/install_generator.rb +17 -0
- data/lib/generators/delayed_job_unique_key/templates/add_unique_key_migration.rb +11 -0
- metadata +63 -174
- data/MIT-LICENSE +0 -20
- data/README.textile +0 -246
- data/contrib/delayed_job.monitrc +0 -14
- data/contrib/delayed_job_multiple.monitrc +0 -23
- data/lib/delayed/backend/base.rb +0 -152
- data/lib/delayed/backend/shared_spec.rb +0 -566
- data/lib/delayed/command.rb +0 -101
- data/lib/delayed/deserialization_error.rb +0 -4
- data/lib/delayed/lifecycle.rb +0 -84
- data/lib/delayed/message_sending.rb +0 -54
- data/lib/delayed/performable_mailer.rb +0 -21
- data/lib/delayed/performable_method.rb +0 -33
- data/lib/delayed/plugin.rb +0 -15
- data/lib/delayed/plugins/clear_locks.rb +0 -15
- data/lib/delayed/psych_ext.rb +0 -75
- data/lib/delayed/railtie.rb +0 -16
- data/lib/delayed/recipes.rb +0 -50
- data/lib/delayed/serialization/active_record.rb +0 -19
- data/lib/delayed/syck_ext.rb +0 -34
- data/lib/delayed/tasks.rb +0 -11
- data/lib/delayed/worker.rb +0 -222
- data/lib/delayed/yaml_ext.rb +0 -10
- data/lib/delayed_job.rb +0 -22
- data/lib/generators/delayed_job/delayed_job_generator.rb +0 -11
- data/lib/generators/delayed_job/templates/script +0 -5
- data/recipes/delayed_job.rb +0 -1
- data/spec/autoloaded/clazz.rb +0 -7
- data/spec/autoloaded/instance_clazz.rb +0 -6
- data/spec/autoloaded/instance_struct.rb +0 -6
- data/spec/autoloaded/struct.rb +0 -7
- data/spec/delayed/backend/test.rb +0 -113
- data/spec/delayed/serialization/test.rb +0 -0
- data/spec/fixtures/bad_alias.yml +0 -1
- data/spec/lifecycle_spec.rb +0 -107
- data/spec/message_sending_spec.rb +0 -116
- data/spec/performable_mailer_spec.rb +0 -46
- data/spec/performable_method_spec.rb +0 -89
- data/spec/sample_jobs.rb +0 -75
- data/spec/spec_helper.rb +0 -45
- data/spec/test_backend_spec.rb +0 -13
- data/spec/worker_spec.rb +0 -19
- data/spec/yaml_ext_spec.rb +0 -41
@@ -0,0 +1,12 @@
|
|
1
|
+
module Delayed
|
2
|
+
module Backend
|
3
|
+
module ActiveRecord
|
4
|
+
class Job
|
5
|
+
include Delayed::Backend::Base
|
6
|
+
|
7
|
+
attr_accessible :priority, :run_at, :queue, :payload_object, :failed_at, :locked_at, :locked_by, :unique_key
|
8
|
+
before_create :check_unique_key
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Delayed
|
2
|
+
module Backend
|
3
|
+
module Base
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
# Add a job to the queue
|
10
|
+
def enqueue(*args)
|
11
|
+
options = {
|
12
|
+
:priority => Delayed::Worker.default_priority
|
13
|
+
}.merge!(args.extract_options!)
|
14
|
+
|
15
|
+
options[:payload_object] ||= args.shift
|
16
|
+
|
17
|
+
if args.size > 0
|
18
|
+
warn "[DEPRECATION] Passing multiple arguments to `#enqueue` is deprecated. Pass a hash with :priority and :run_at."
|
19
|
+
options[:priority] = args.first || options[:priority]
|
20
|
+
options[:unique_key] = options[:unique_key]
|
21
|
+
options[:run_at] = args[1]
|
22
|
+
end
|
23
|
+
|
24
|
+
unless options[:payload_object].respond_to?(:perform)
|
25
|
+
raise ArgumentError, 'Cannot enqueue items which do not respond to perform'
|
26
|
+
end
|
27
|
+
|
28
|
+
if Delayed::Worker.delay_jobs
|
29
|
+
self.new(options).tap do |job|
|
30
|
+
Delayed::Worker.lifecycle.run_callbacks(:enqueue, job) do
|
31
|
+
job.hook(:enqueue)
|
32
|
+
job.save
|
33
|
+
end
|
34
|
+
end
|
35
|
+
else
|
36
|
+
Delayed::Job.new(:payload_object => options[:payload_object]).tap do |job|
|
37
|
+
job.invoke_job
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
protected
|
44
|
+
|
45
|
+
def check_unique_key
|
46
|
+
return true if !self.respond_to?(:unique_key) || self.unique_key.blank?
|
47
|
+
self.class.where(:unique_key => self.unique_key, :locked_at => nil).first.nil?
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
require 'rails/generators/active_record/migration'
|
4
|
+
|
5
|
+
|
6
|
+
module DelayedJobUniqueKey
|
7
|
+
class InstallGenerator < Rails::Generators::Base
|
8
|
+
include Rails::Generators::Migration
|
9
|
+
extend ActiveRecord::Generators::Migration
|
10
|
+
|
11
|
+
self.source_paths << File.join(File.dirname(__FILE__), 'templates')
|
12
|
+
|
13
|
+
def create_migration_file
|
14
|
+
migration_template 'add_unique_key_migration.rb', 'db/migrate/add_unique_key_delayed_jobs.rb'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class AddUniqueKeyDelayedJobs < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
add_column :delayed_jobs, :unique_key, :string
|
4
|
+
add_index :delayed_jobs, [:unique_key, :locked_at], :name => 'delayed_jobs_unique_key'
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.down
|
8
|
+
remove_index :delayed_jobs, :name => 'delayed_jobs_unique_key'
|
9
|
+
remove_column :delayed_jobs, :unique_key
|
10
|
+
end
|
11
|
+
end
|
metadata
CHANGED
@@ -1,197 +1,86 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: delayed_job_unique_key
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
8
|
-
- Matt Griffin
|
9
|
-
- Brian Ryckbost
|
10
|
-
- Steve Richert
|
11
|
-
- Chris Gaffney
|
12
|
-
- Brandon Keepers
|
13
|
-
- Tobias Lütke
|
12
|
+
authors:
|
14
13
|
- Oleg Muntyan
|
15
14
|
autorequire:
|
16
15
|
bindir: bin
|
17
16
|
cert_chain: []
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
none: false
|
24
|
-
requirements:
|
25
|
-
- - ! '>='
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
version: 1.0.10
|
28
|
-
type: :runtime
|
17
|
+
|
18
|
+
date: 2012-05-29 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: delayed_job_active_record
|
29
22
|
prerelease: false
|
30
|
-
|
31
|
-
- !ruby/object:Gem::Dependency
|
32
|
-
name: activesupport
|
33
|
-
requirement: &71120460 !ruby/object:Gem::Requirement
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
34
24
|
none: false
|
35
|
-
requirements:
|
25
|
+
requirements:
|
36
26
|
- - ~>
|
37
|
-
- !ruby/object:Gem::Version
|
38
|
-
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 23
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
- 3
|
32
|
+
- 2
|
33
|
+
version: 0.3.2
|
39
34
|
type: :runtime
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
name: activerecord
|
44
|
-
requirement: &71119860 !ruby/object:Gem::Requirement
|
45
|
-
none: false
|
46
|
-
requirements:
|
47
|
-
- - ~>
|
48
|
-
- !ruby/object:Gem::Version
|
49
|
-
version: '3.0'
|
50
|
-
type: :development
|
51
|
-
prerelease: false
|
52
|
-
version_requirements: *71119860
|
53
|
-
- !ruby/object:Gem::Dependency
|
54
|
-
name: sqlite3
|
55
|
-
requirement: &71119390 !ruby/object:Gem::Requirement
|
56
|
-
none: false
|
57
|
-
requirements:
|
58
|
-
- - ! '>='
|
59
|
-
- !ruby/object:Gem::Version
|
60
|
-
version: '0'
|
61
|
-
type: :development
|
62
|
-
prerelease: false
|
63
|
-
version_requirements: *71119390
|
64
|
-
- !ruby/object:Gem::Dependency
|
65
|
-
name: rails
|
66
|
-
requirement: &71118720 !ruby/object:Gem::Requirement
|
67
|
-
none: false
|
68
|
-
requirements:
|
69
|
-
- - ~>
|
70
|
-
- !ruby/object:Gem::Version
|
71
|
-
version: '3.0'
|
72
|
-
type: :development
|
73
|
-
prerelease: false
|
74
|
-
version_requirements: *71118720
|
75
|
-
- !ruby/object:Gem::Dependency
|
76
|
-
name: rspec
|
77
|
-
requirement: &71118440 !ruby/object:Gem::Requirement
|
78
|
-
none: false
|
79
|
-
requirements:
|
80
|
-
- - ~>
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '2.0'
|
83
|
-
type: :development
|
84
|
-
prerelease: false
|
85
|
-
version_requirements: *71118440
|
86
|
-
- !ruby/object:Gem::Dependency
|
87
|
-
name: rake
|
88
|
-
requirement: &71134500 !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
|
-
requirements:
|
91
|
-
- - ! '>='
|
92
|
-
- !ruby/object:Gem::Version
|
93
|
-
version: '0'
|
94
|
-
type: :development
|
95
|
-
prerelease: false
|
96
|
-
version_requirements: *71134500
|
97
|
-
description: ! 'This fork allows to prevent creating new job if already exists not
|
98
|
-
running one with unique key
|
99
|
-
|
100
|
-
|
101
|
-
This gem is collectiveidea''s fork (http://github.com/collectiveidea/delayed_job).'
|
102
|
-
email:
|
103
|
-
- chris@collectiveidea.com
|
104
|
-
- brandon@opensoul.org
|
35
|
+
version_requirements: *id001
|
36
|
+
description: Gem based on delayed job with active record and allows to prevent creating new job if already exists not running one with unique key
|
37
|
+
email:
|
105
38
|
- omuntyan@gmail.com
|
106
39
|
executables: []
|
40
|
+
|
107
41
|
extensions: []
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
-
|
113
|
-
- lib/
|
114
|
-
- lib/
|
115
|
-
- lib/
|
116
|
-
- lib/
|
117
|
-
- lib/
|
118
|
-
|
119
|
-
- lib/delayed/performable_mailer.rb
|
120
|
-
- lib/delayed/railtie.rb
|
121
|
-
- lib/delayed/command.rb
|
122
|
-
- lib/delayed/psych_ext.rb
|
123
|
-
- lib/delayed/tasks.rb
|
124
|
-
- lib/delayed/yaml_ext.rb
|
125
|
-
- lib/delayed/performable_method.rb
|
126
|
-
- lib/delayed/deserialization_error.rb
|
127
|
-
- lib/delayed/worker.rb
|
128
|
-
- lib/delayed/lifecycle.rb
|
129
|
-
- lib/delayed/backend/shared_spec.rb
|
130
|
-
- lib/delayed/backend/base.rb
|
131
|
-
- lib/delayed/plugins/clear_locks.rb
|
132
|
-
- lib/generators/delayed_job/templates/script
|
133
|
-
- lib/generators/delayed_job/delayed_job_generator.rb
|
134
|
-
- recipes/delayed_job.rb
|
135
|
-
- spec/yaml_ext_spec.rb
|
136
|
-
- spec/autoloaded/struct.rb
|
137
|
-
- spec/autoloaded/instance_clazz.rb
|
138
|
-
- spec/autoloaded/clazz.rb
|
139
|
-
- spec/autoloaded/instance_struct.rb
|
140
|
-
- spec/fixtures/bad_alias.yml
|
141
|
-
- spec/delayed/serialization/test.rb
|
142
|
-
- spec/delayed/backend/test.rb
|
143
|
-
- spec/worker_spec.rb
|
144
|
-
- spec/spec_helper.rb
|
145
|
-
- spec/sample_jobs.rb
|
146
|
-
- spec/lifecycle_spec.rb
|
147
|
-
- spec/performable_method_spec.rb
|
148
|
-
- spec/test_backend_spec.rb
|
149
|
-
- spec/performable_mailer_spec.rb
|
150
|
-
- spec/message_sending_spec.rb
|
151
|
-
- MIT-LICENSE
|
152
|
-
- README.textile
|
153
|
-
homepage: http://github.com/collectiveidea/delayed_job
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- lib/delayed_job_unique_key.rb
|
47
|
+
- lib/generators/delayed_job_unique_key/install_generator.rb
|
48
|
+
- lib/generators/delayed_job_unique_key/templates/add_unique_key_migration.rb
|
49
|
+
- lib/delayed_job_unique_key/version.rb
|
50
|
+
- lib/delayed_job_unique_key/active_record_job.rb
|
51
|
+
- lib/delayed_job_unique_key/base.rb
|
52
|
+
homepage: http://github.com/munya/delayed_job_unique_key
|
154
53
|
licenses: []
|
54
|
+
|
155
55
|
post_install_message:
|
156
|
-
rdoc_options:
|
157
|
-
|
158
|
-
|
159
|
-
- --inline-source
|
160
|
-
- --line-numbers
|
161
|
-
require_paths:
|
56
|
+
rdoc_options: []
|
57
|
+
|
58
|
+
require_paths:
|
162
59
|
- lib
|
163
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
164
61
|
none: false
|
165
|
-
requirements:
|
166
|
-
- -
|
167
|
-
- !ruby/object:Gem::Version
|
168
|
-
|
169
|
-
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
170
70
|
none: false
|
171
|
-
requirements:
|
172
|
-
- -
|
173
|
-
- !ruby/object:Gem::Version
|
174
|
-
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
175
78
|
requirements: []
|
79
|
+
|
176
80
|
rubyforge_project:
|
177
|
-
rubygems_version: 1.8.
|
81
|
+
rubygems_version: 1.8.24
|
178
82
|
signing_key:
|
179
83
|
specification_version: 3
|
180
|
-
summary:
|
181
|
-
test_files:
|
182
|
-
|
183
|
-
- spec/autoloaded/struct.rb
|
184
|
-
- spec/autoloaded/instance_clazz.rb
|
185
|
-
- spec/autoloaded/clazz.rb
|
186
|
-
- spec/autoloaded/instance_struct.rb
|
187
|
-
- spec/fixtures/bad_alias.yml
|
188
|
-
- spec/delayed/serialization/test.rb
|
189
|
-
- spec/delayed/backend/test.rb
|
190
|
-
- spec/worker_spec.rb
|
191
|
-
- spec/spec_helper.rb
|
192
|
-
- spec/sample_jobs.rb
|
193
|
-
- spec/lifecycle_spec.rb
|
194
|
-
- spec/performable_method_spec.rb
|
195
|
-
- spec/test_backend_spec.rb
|
196
|
-
- spec/performable_mailer_spec.rb
|
197
|
-
- spec/message_sending_spec.rb
|
84
|
+
summary: ""
|
85
|
+
test_files: []
|
86
|
+
|
data/MIT-LICENSE
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright (c) 2005 Tobias Luetke
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOa AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SaALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
DELETED
@@ -1,246 +0,0 @@
|
|
1
|
-
h1. Delayed::Job
|
2
|
-
|
3
|
-
Delayed_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
|
14
|
-
|
15
|
-
"Follow us on Twitter":https://twitter.com/delayedjob to get updates and notices about new releases.
|
16
|
-
|
17
|
-
|
18
|
-
h2. Installation
|
19
|
-
|
20
|
-
delayed_job 2.1 only supports Rails 3.0+. See the "2.0 branch":https://github.com/collectiveidea/delayed_job/tree/v2.0 for Rails 2.
|
21
|
-
|
22
|
-
To install, add delayed_job to your @Gemfile@ and run `bundle install`:
|
23
|
-
|
24
|
-
<pre>
|
25
|
-
gem 'delayed_job'
|
26
|
-
</pre>
|
27
|
-
|
28
|
-
After delayed_job is installed, you will need to setup the backend.
|
29
|
-
|
30
|
-
h3. Backends
|
31
|
-
|
32
|
-
delayed_job supports multiple backends for storing the job queue. "See the wiki for other backends":http://wiki.github.com/collectiveidea/delayed_job/backends besides Active Record.
|
33
|
-
|
34
|
-
The default is Active Record, which requires a jobs table.
|
35
|
-
|
36
|
-
<pre>
|
37
|
-
$ script/rails generate delayed_job
|
38
|
-
$ rake db:migrate
|
39
|
-
</pre>
|
40
|
-
|
41
|
-
h2. Queuing Jobs
|
42
|
-
|
43
|
-
Call @.delay.method(params)@ on any object and it will be processed in the background.
|
44
|
-
|
45
|
-
<pre>
|
46
|
-
# without delayed_job
|
47
|
-
@user.activate!(@device)
|
48
|
-
|
49
|
-
# with delayed_job
|
50
|
-
@user.delay.activate!(@device)
|
51
|
-
</pre>
|
52
|
-
|
53
|
-
If a method should always be run in the background, you can call @#handle_asynchronously@ after the method declaration:
|
54
|
-
|
55
|
-
<pre>
|
56
|
-
class Device
|
57
|
-
def deliver
|
58
|
-
# long running method
|
59
|
-
end
|
60
|
-
handle_asynchronously :deliver
|
61
|
-
end
|
62
|
-
|
63
|
-
device = Device.new
|
64
|
-
device.deliver
|
65
|
-
</pre>
|
66
|
-
|
67
|
-
handle_asynchronously can take as options anything you can pass to delay. In addition the values can be Proc objects allowing call time evaluation of the value. For some examples:
|
68
|
-
|
69
|
-
<pre>
|
70
|
-
class LongTasks
|
71
|
-
def send_mailer
|
72
|
-
# Some other code
|
73
|
-
end
|
74
|
-
handle_asynchronously :send_mailer, :priority => 20
|
75
|
-
|
76
|
-
def in_the_future
|
77
|
-
# Some other code
|
78
|
-
end
|
79
|
-
# 5.minutes.from_now will be evaluated when in_the_future is called
|
80
|
-
handle_asynchronously :in_the_future, :run_at => Proc.new { 5.minutes.from_now }
|
81
|
-
|
82
|
-
def self.when_to_run
|
83
|
-
2.hours.from_now
|
84
|
-
end
|
85
|
-
|
86
|
-
def call_a_class_method
|
87
|
-
# Some other code
|
88
|
-
end
|
89
|
-
handle_asynchronously :call_a_class_method, :run_at => Proc.new { when_to_run }
|
90
|
-
|
91
|
-
attr_reader :how_important
|
92
|
-
|
93
|
-
def call_an_instance_method
|
94
|
-
# Some other code
|
95
|
-
end
|
96
|
-
handle_asynchronously :call_an_instance_method, :priority => Proc.new {|i| i.how_important }
|
97
|
-
end
|
98
|
-
</pre>
|
99
|
-
|
100
|
-
h3. Rails 3 Mailers
|
101
|
-
|
102
|
-
Due to how mailers are implemented in Rails 3, we had to do a little work around to get delayed_job to work.
|
103
|
-
|
104
|
-
<pre>
|
105
|
-
# without delayed_job
|
106
|
-
Notifier.signup(@user).deliver
|
107
|
-
|
108
|
-
# with delayed_job
|
109
|
-
Notifier.delay.signup(@user)
|
110
|
-
</pre>
|
111
|
-
|
112
|
-
Remove the @.deliver@ method to make it work. It's not ideal, but it's the best we could do for now.
|
113
|
-
|
114
|
-
h2. Running Jobs
|
115
|
-
|
116
|
-
@script/delayed_job@ can be used to manage a background process which will start working off jobs. Make sure you've run `script/generate delayed_job`.
|
117
|
-
|
118
|
-
<pre>
|
119
|
-
$ RAILS_ENV=production script/delayed_job start
|
120
|
-
$ RAILS_ENV=production script/delayed_job stop
|
121
|
-
|
122
|
-
# Runs two workers in separate processes.
|
123
|
-
$ RAILS_ENV=production script/delayed_job -n 2 start
|
124
|
-
$ RAILS_ENV=production script/delayed_job stop
|
125
|
-
</pre>
|
126
|
-
|
127
|
-
Workers can be running on any computer, as long as they have access to the database and their clock is in sync. Keep in mind that each worker will check the database at least every 5 seconds.
|
128
|
-
|
129
|
-
You can also invoke @rake jobs:work@ which will start working off jobs. You can cancel the rake task with @CTRL-C@.
|
130
|
-
|
131
|
-
h2. Custom Jobs
|
132
|
-
|
133
|
-
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.
|
134
|
-
|
135
|
-
<pre>
|
136
|
-
class NewsletterJob < Struct.new(:text, :emails)
|
137
|
-
def perform
|
138
|
-
emails.each { |e| NewsletterMailer.deliver_text_to_email(text, e) }
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
Delayed::Job.enqueue NewsletterJob.new('lorem ipsum...', Customers.find(:all).collect(&:email))
|
143
|
-
</pre>
|
144
|
-
|
145
|
-
h2. Hooks
|
146
|
-
|
147
|
-
You can define hooks on your job that will be called at different stages in the process:
|
148
|
-
|
149
|
-
<pre>
|
150
|
-
class ParanoidNewsletterJob < NewsletterJob
|
151
|
-
def enqueue(job)
|
152
|
-
record_stat 'newsletter_job/enqueue'
|
153
|
-
end
|
154
|
-
|
155
|
-
def perform
|
156
|
-
emails.each { |e| NewsletterMailer.deliver_text_to_email(text, e) }
|
157
|
-
end
|
158
|
-
|
159
|
-
def before(job)
|
160
|
-
record_stat 'newsletter_job/start'
|
161
|
-
end
|
162
|
-
|
163
|
-
def after(job)
|
164
|
-
record_stat 'newsletter_job/after'
|
165
|
-
end
|
166
|
-
|
167
|
-
def success(job)
|
168
|
-
record_stat 'newsletter_job/success'
|
169
|
-
end
|
170
|
-
|
171
|
-
def error(job, exception)
|
172
|
-
notify_hoptoad(exception)
|
173
|
-
end
|
174
|
-
|
175
|
-
def failure
|
176
|
-
page_sysadmin_in_the_middle_of_the_night
|
177
|
-
end
|
178
|
-
end
|
179
|
-
</pre>
|
180
|
-
|
181
|
-
h2. Gory Details
|
182
|
-
|
183
|
-
The library evolves around a delayed_jobs table which looks as follows:
|
184
|
-
|
185
|
-
<pre>
|
186
|
-
create_table :delayed_jobs, :force => true do |table|
|
187
|
-
table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue
|
188
|
-
table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually.
|
189
|
-
table.text :handler # YAML-encoded string of the object that will do work
|
190
|
-
table.text :last_error # reason for last failure (See Note below)
|
191
|
-
table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future.
|
192
|
-
table.datetime :locked_at # Set when a client is working on this object
|
193
|
-
table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
|
194
|
-
table.string :locked_by # Who is working on this object (if locked)
|
195
|
-
table.timestamps
|
196
|
-
end
|
197
|
-
</pre>
|
198
|
-
|
199
|
-
On failure, the job is scheduled again in 5 seconds + N ** 4, where N is the number of retries.
|
200
|
-
|
201
|
-
The default Worker.max_attempts is 25. After this, the job either deleted (default), or left in the database with "failed_at" set.
|
202
|
-
With the default of 25 attempts, the last retry will be 20 days later, with the last interval being almost 100 hours.
|
203
|
-
|
204
|
-
The default Worker.max_run_time is 4.hours. If your job takes longer than that, another computer could pick it up. It's up to you to
|
205
|
-
make sure your job doesn't exceed this time. You should set this to the longest time you think the job could take.
|
206
|
-
|
207
|
-
By default, it will delete failed jobs (and it always deletes successful jobs). If you want to keep failed jobs, set
|
208
|
-
Delayed::Worker.destroy_failed_jobs = false. The failed jobs will be marked with non-null failed_at.
|
209
|
-
|
210
|
-
By default all jobs are scheduled with priority = 0, which is top priority. You can change this by setting Delayed::Worker.default_priority to something else. Lower numbers have higher priority.
|
211
|
-
|
212
|
-
It is possible to disable delayed jobs for testing purposes. Set Delayed::Worker.delay_jobs = false to execute all jobs realtime.
|
213
|
-
|
214
|
-
Here is an example of changing job parameters in Rails:
|
215
|
-
|
216
|
-
<pre>
|
217
|
-
# config/initializers/delayed_job_config.rb
|
218
|
-
Delayed::Worker.destroy_failed_jobs = false
|
219
|
-
Delayed::Worker.sleep_delay = 60
|
220
|
-
Delayed::Worker.max_attempts = 3
|
221
|
-
Delayed::Worker.max_run_time = 5.minutes
|
222
|
-
Delayed::Worker.delay_jobs = !Rails.env.test?
|
223
|
-
</pre>
|
224
|
-
|
225
|
-
h3. Cleaning up
|
226
|
-
|
227
|
-
You can invoke @rake jobs:clear@ to delete all jobs in the queue.
|
228
|
-
|
229
|
-
h2. Mailing List
|
230
|
-
|
231
|
-
Join us on the "mailing list":http://groups.google.com/group/delayed_job
|
232
|
-
|
233
|
-
h2. How to contribute
|
234
|
-
|
235
|
-
If you find what looks like a bug:
|
236
|
-
|
237
|
-
# Search the "mailing list":http://groups.google.com/group/delayed_job to see if anyone else had the same issue.
|
238
|
-
# Check the "GitHub issue tracker":http://github.com/collectiveidea/delayed_job/issues/ to see if anyone else has reported issue.
|
239
|
-
# If you don't see anything, create an issue with information on how to reproduce it.
|
240
|
-
|
241
|
-
If you want to contribute an enhancement or a fix:
|
242
|
-
|
243
|
-
# Fork the project on github.
|
244
|
-
# Make your changes with tests.
|
245
|
-
# Commit the changes without making changes to the Rakefile or any other files that aren't related to your enhancement or fix
|
246
|
-
# Send a pull request.
|