jmcnevin-delayed_job 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/.gitignore +3 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.textile +213 -0
  4. data/Rakefile +45 -0
  5. data/VERSION +1 -0
  6. data/benchmarks.rb +33 -0
  7. data/contrib/delayed_job.monitrc +14 -0
  8. data/contrib/delayed_job_multiple.monitrc +23 -0
  9. data/delayed_job.gemspec +107 -0
  10. data/generators/delayed_job/delayed_job_generator.rb +21 -0
  11. data/generators/delayed_job/templates/migration.rb +21 -0
  12. data/init.rb +1 -0
  13. data/lib/delayed/backend/active_record.rb +90 -0
  14. data/lib/delayed/backend/base.rb +111 -0
  15. data/lib/delayed/backend/data_mapper.rb +125 -0
  16. data/lib/delayed/backend/mongo_mapper.rb +110 -0
  17. data/lib/delayed/message_sending.rb +22 -0
  18. data/lib/delayed/performable_method.rb +62 -0
  19. data/lib/delayed/railtie.rb +10 -0
  20. data/lib/delayed/recipes.rb +31 -0
  21. data/lib/delayed/tasks.rb +15 -0
  22. data/lib/delayed/worker.rb +199 -0
  23. data/lib/delayed_job.rb +14 -0
  24. data/rails/init.rb +5 -0
  25. data/recipes/delayed_job.rb +1 -0
  26. data/spec/backend/active_record_job_spec.rb +46 -0
  27. data/spec/backend/data_mapper_job_spec.rb +16 -0
  28. data/spec/backend/mongo_mapper_job_spec.rb +94 -0
  29. data/spec/backend/shared_backend_spec.rb +265 -0
  30. data/spec/delayed_method_spec.rb +59 -0
  31. data/spec/performable_method_spec.rb +42 -0
  32. data/spec/sample_jobs.rb +25 -0
  33. data/spec/setup/active_record.rb +33 -0
  34. data/spec/setup/data_mapper.rb +8 -0
  35. data/spec/setup/mongo_mapper.rb +17 -0
  36. data/spec/spec_helper.rb +26 -0
  37. data/spec/story_spec.rb +17 -0
  38. data/spec/worker_spec.rb +216 -0
  39. data/tasks/jobs.rake +1 -0
  40. metadata +241 -0
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ *.swp
3
+ .DS_Store
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
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 ADDED
@@ -0,0 +1,213 @@
1
+ h1. Delayed::Job
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
14
+
15
+ h2. Installation
16
+
17
+ To install as a gem, add the following to @config/environment.rb@:
18
+
19
+ <pre>
20
+ config.gem 'delayed_job'
21
+ </pre>
22
+
23
+ Rake tasks are not automatically loaded from gems, so you'll need to add the following to your Rakefile:
24
+
25
+ <pre>
26
+ begin
27
+ require 'delayed/tasks'
28
+ rescue LoadError
29
+ STDERR.puts "Run `rake gems:install` to install delayed_job"
30
+ end
31
+ </pre>
32
+
33
+ To install as a plugin:
34
+
35
+ <pre>
36
+ script/plugin install git://github.com/collectiveidea/delayed_job.git
37
+ </pre>
38
+
39
+ After delayed_job is installed, you will need to setup the backend.
40
+
41
+ h2. Backends
42
+
43
+ delayed_job supports multiple backends for storing the job queue. There are currently implementations for Active Record, MongoMapper, and DataMapper.
44
+
45
+ h3. Active Record
46
+
47
+ The default is Active Record, which requires a jobs table.
48
+
49
+ <pre>
50
+ $ script/generate delayed_job
51
+ $ rake db:migrate
52
+ </pre>
53
+
54
+ h3. MongoMapper
55
+
56
+ <pre>
57
+ # config/initializers/delayed_job.rb
58
+ Delayed::Worker.backend = :mongo_mapper
59
+ </pre>
60
+
61
+ h3. DataMapper
62
+
63
+ <pre>
64
+ # config/initializers/delayed_job.rb
65
+ Delayed::Worker.backend = :data_mapper
66
+ Delayed::Worker.backend.auto_upgrade!
67
+ </pre>
68
+
69
+ h2. Queuing Jobs
70
+
71
+ Call @#send_later(method, params)@ on any object and it will be processed in the background.
72
+
73
+ <pre>
74
+ # without delayed_job
75
+ Notifier.deliver_signup(@user)
76
+
77
+ # with delayed_job
78
+ Notifier.send_later :deliver_signup, @user
79
+ </pre>
80
+
81
+ If a method should always be run in the background, you can call @#handle_asynchronously@ after the method declaration:
82
+
83
+ <pre>
84
+ class Device
85
+ def deliver
86
+ # long running method
87
+ end
88
+ handle_asynchronously :deliver
89
+ end
90
+
91
+ device = Device.new
92
+ device.deliver
93
+ </pre>
94
+
95
+ h2. Running Jobs
96
+
97
+ @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`.
98
+
99
+ <pre>
100
+ $ RAILS_ENV=production script/delayed_job start
101
+ $ RAILS_ENV=production script/delayed_job stop
102
+
103
+ # Runs two workers in separate processes.
104
+ $ RAILS_ENV=production script/delayed_job -n 2 start
105
+ $ RAILS_ENV=production script/delayed_job stop
106
+ </pre>
107
+
108
+ 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.
109
+
110
+ You can also invoke @rake jobs:work@ which will start working off jobs. You can cancel the rake task with @CTRL-C@.
111
+
112
+ h2. Custom Jobs
113
+
114
+ 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.
115
+
116
+ <pre>
117
+ class NewsletterJob < Struct.new(:text, :emails)
118
+ def perform
119
+ emails.each { |e| NewsletterMailer.deliver_text_to_email(text, e) }
120
+ end
121
+ end
122
+
123
+ Delayed::Job.enqueue NewsletterJob.new('lorem ipsum...', Customers.find(:all).collect(&:email))
124
+ </pre>
125
+
126
+ You can also add an optional on_permanent_failure method which will run if the job has failed too many times to be retried:
127
+
128
+ <pre>
129
+ class ParanoidNewsletterJob < NewsletterJob
130
+ def perform
131
+ emails.each { |e| NewsletterMailer.deliver_text_to_email(text, e) }
132
+ end
133
+
134
+ def on_permanent_failure
135
+ page_sysadmin_in_the_middle_of_the_night
136
+ end
137
+ end
138
+ </pre>
139
+
140
+ h2. Gory Details
141
+
142
+ The library evolves around a delayed_jobs table which looks as follows:
143
+
144
+ <pre>
145
+ create_table :delayed_jobs, :force => true do |table|
146
+ table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue
147
+ table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually.
148
+ table.text :handler # YAML-encoded string of the object that will do work
149
+ table.text :last_error # reason for last failure (See Note below)
150
+ table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future.
151
+ table.datetime :locked_at # Set when a client is working on this object
152
+ table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
153
+ table.string :locked_by # Who is working on this object (if locked)
154
+ table.timestamps
155
+ end
156
+ </pre>
157
+
158
+ On failure, the job is scheduled again in 5 seconds + N ** 4, where N is the number of retries.
159
+
160
+ The default Worker.max_attempts is 25. After this, the job either deleted (default), or left in the database with "failed_at" set.
161
+ With the default of 25 attempts, the last retry will be 20 days later, with the last interval being almost 100 hours.
162
+
163
+ 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
164
+ make sure your job doesn't exceed this time. You should set this to the longest time you think the job could take.
165
+
166
+ By default, it will delete failed jobs (and it always deletes successful jobs). If you want to keep failed jobs, set
167
+ Delayed::Worker.destroy_failed_jobs = false. The failed jobs will be marked with non-null failed_at.
168
+
169
+ Here is an example of changing job parameters in Rails:
170
+
171
+ <pre>
172
+ # config/initializers/delayed_job_config.rb
173
+ Delayed::Worker.destroy_failed_jobs = false
174
+ Delayed::Worker.sleep_delay = 60
175
+ Delayed::Worker.max_attempts = 3
176
+ Delayed::Worker.max_run_time = 5.minutes
177
+ </pre>
178
+
179
+ h3. Cleaning up
180
+
181
+ You can invoke @rake jobs:clear@ to delete all jobs in the queue.
182
+
183
+ h2. Mailing List
184
+
185
+ Join us on the mailing list at http://groups.google.com/group/delayed_job
186
+
187
+ h2. How to contribute
188
+
189
+ If you find what looks like a bug:
190
+
191
+ # Check the GitHub issue tracker to see if anyone else has had the same issue.
192
+ http://github.com/collectiveidea/delayed_job/issues/
193
+ # If you don't see anything, create an issue with information on how to reproduce it.
194
+
195
+ If you want to contribute an enhancement or a fix:
196
+
197
+ # Fork the project on github.
198
+ http://github.com/collectiveidea/delayed_job/
199
+ # Make your changes with tests.
200
+ # Commit the changes without making changes to the Rakefile, VERSION, or any other files that aren't related to your enhancement or fix
201
+ # Send a pull request.
202
+
203
+ h3. Changes
204
+
205
+ * 1.7.0: Added failed_at column which can optionally be set after a certain amount of failed job attempts. By default failed job attempts are destroyed after about a month.
206
+
207
+ * 1.6.0: 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.
208
+
209
+ * 1.5.0: 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 instead of relying on row level locks. This enables us to run as many worker processes as we need to speed up queue processing.
210
+
211
+ * 1.2.0: Added #send_later to Object for simpler job creation
212
+
213
+ * 1.0.0: Initial release
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ # -*- encoding: utf-8 -*-
2
+ begin
3
+ require 'jeweler'
4
+ rescue LoadError
5
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
6
+ exit 1
7
+ end
8
+
9
+ Jeweler::Tasks.new do |s|
10
+ s.name = "delayed_job"
11
+ s.summary = "Database-backed asynchronous priority queue system -- Extracted from Shopify"
12
+ s.email = "tobi@leetsoft.com"
13
+ s.homepage = "http://github.com/collectiveidea/delayed_job"
14
+ s.description = "Delayed_job (or DJ) encapsulates the common pattern of asynchronously executing longer tasks in the background. It is a direct extraction from Shopify where the job table is responsible for a multitude of core tasks.\n\nThis gem is collectiveidea's fork (http://github.com/collectiveidea/delayed_job)."
15
+ s.authors = ["Brandon Keepers", "Tobias Lütke"]
16
+
17
+ s.has_rdoc = true
18
+ s.rdoc_options = ["--main", "README.textile", "--inline-source", "--line-numbers"]
19
+ s.extra_rdoc_files = ["README.textile"]
20
+
21
+ s.test_files = Dir['spec/*_spec.rb']
22
+
23
+ s.add_development_dependency "rspec"
24
+ s.add_development_dependency "sqlite3-ruby"
25
+ s.add_development_dependency "mongo_mapper"
26
+ s.add_development_dependency "dm-core"
27
+ s.add_development_dependency "dm-observer"
28
+ s.add_development_dependency "dm-aggregates"
29
+ s.add_development_dependency "dm-validations"
30
+ s.add_development_dependency "do_sqlite3"
31
+ s.add_development_dependency "database_cleaner"
32
+ end
33
+
34
+ require 'spec/rake/spectask'
35
+
36
+ task :default => :spec
37
+
38
+ desc 'Run the specs'
39
+ Spec::Rake::SpecTask.new(:spec) do |t|
40
+ t.libs << 'lib'
41
+ t.pattern = 'spec/*_spec.rb'
42
+ t.verbose = true
43
+ end
44
+ task :spec => :check_dependencies
45
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 2.0.3
data/benchmarks.rb ADDED
@@ -0,0 +1,33 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/lib')
2
+ require 'rubygems'
3
+ require 'logger'
4
+ require 'delayed_job'
5
+ require 'benchmark'
6
+
7
+ RAILS_ENV = 'test'
8
+
9
+ Delayed::Worker.logger = Logger.new('/dev/null')
10
+
11
+ BACKENDS = []
12
+ Dir.glob("#{File.dirname(__FILE__)}/spec/setup/*.rb") do |backend|
13
+ begin
14
+ backend = File.basename(backend, '.rb')
15
+ require "spec/setup/#{backend}"
16
+ BACKENDS << backend.to_sym
17
+ rescue LoadError
18
+ puts "Unable to load #{backend} backend! #{$!}"
19
+ end
20
+ end
21
+
22
+
23
+ Benchmark.bm(10) do |x|
24
+ BACKENDS.each do |backend|
25
+ require "spec/setup/#{backend}"
26
+ Delayed::Worker.backend = backend
27
+
28
+ n = 10000
29
+ n.times { "foo".send_later :length }
30
+
31
+ x.report(backend.to_s) { Delayed::Worker.new(:quiet => true).work_off(n) }
32
+ end
33
+ end
@@ -0,0 +1,14 @@
1
+ # an example Monit configuration file for delayed_job
2
+ # See: http://stackoverflow.com/questions/1226302/how-to-monitor-delayedjob-with-monit/1285611
3
+ #
4
+ # To use:
5
+ # 1. copy to /var/www/apps/{app_name}/shared/delayed_job.monitrc
6
+ # 2. replace {app_name} as appropriate
7
+ # 3. add this to your /etc/monit/monitrc
8
+ #
9
+ # include /var/www/apps/{app_name}/shared/delayed_job.monitrc
10
+
11
+ check process delayed_job
12
+ with pidfile /var/www/apps/{app_name}/shared/pids/delayed_job.pid
13
+ start program = "/usr/bin/env RAILS_ENV=production /var/www/apps/{app_name}/current/script/delayed_job start"
14
+ stop program = "/usr/bin/env RAILS_ENV=production /var/www/apps/{app_name}/current/script/delayed_job stop"
@@ -0,0 +1,23 @@
1
+ # an example Monit configuration file for delayed_job running multiple processes
2
+ #
3
+ # To use:
4
+ # 1. copy to /var/www/apps/{app_name}/shared/delayed_job.monitrc
5
+ # 2. replace {app_name} as appropriate
6
+ # 3. add this to your /etc/monit/monitrc
7
+ #
8
+ # include /var/www/apps/{app_name}/shared/delayed_job.monitrc
9
+
10
+ check process delayed_job_0
11
+ with pidfile /var/www/apps/{app_name}/shared/pids/delayed_job.0.pid
12
+ start program = "/usr/bin/env RAILS_ENV=production /var/www/apps/{app_name}/current/script/delayed_job start -i 0"
13
+ stop program = "/usr/bin/env RAILS_ENV=production /var/www/apps/{app_name}/current/script/delayed_job stop -i 0"
14
+
15
+ check process delayed_job_1
16
+ with pidfile /var/www/apps/{app_name}/shared/pids/delayed_job.1.pid
17
+ start program = "/usr/bin/env RAILS_ENV=production /var/www/apps/{app_name}/current/script/delayed_job start -i 1"
18
+ stop program = "/usr/bin/env RAILS_ENV=production /var/www/apps/{app_name}/current/script/delayed_job stop -i 1"
19
+
20
+ check process delayed_job_2
21
+ with pidfile /var/www/apps/{app_name}/shared/pids/delayed_job.2.pid
22
+ start program = "/usr/bin/env RAILS_ENV=production /var/www/apps/{app_name}/current/script/delayed_job start -i 2"
23
+ stop program = "/usr/bin/env RAILS_ENV=production /var/www/apps/{app_name}/current/script/delayed_job stop -i 2"
@@ -0,0 +1,107 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{jmcnevin-delayed_job}
5
+ s.version = "2.0.4"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Brandon Keepers", "Tobias L\303\274tke", "Jeremy McNevin"]
9
+ s.date = %q{2010-08-26}
10
+ s.description = %q{Delayed_job (or DJ) encapsulates the common pattern of asynchronously executing longer tasks in the background. It is a direct extraction from Shopify where the job table is responsible for a multitude of core tasks.
11
+
12
+ This gem is jmcnevin's fork (http://github.com/jmcnevin/delayed_job).}
13
+ s.email = %q{tobi@leetsoft.com}
14
+ s.extra_rdoc_files = [
15
+ "README.textile"
16
+ ]
17
+ s.files = [
18
+ ".gitignore",
19
+ "MIT-LICENSE",
20
+ "README.textile",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "benchmarks.rb",
24
+ "contrib/delayed_job.monitrc",
25
+ "contrib/delayed_job_multiple.monitrc",
26
+ "delayed_job.gemspec",
27
+ "generators/delayed_job/delayed_job_generator.rb",
28
+ "generators/delayed_job/templates/migration.rb",
29
+ "init.rb",
30
+ "lib/delayed/backend/active_record.rb",
31
+ "lib/delayed/backend/base.rb",
32
+ "lib/delayed/backend/data_mapper.rb",
33
+ "lib/delayed/backend/mongo_mapper.rb",
34
+ "lib/delayed/message_sending.rb",
35
+ "lib/delayed/performable_method.rb",
36
+ "lib/delayed/railtie.rb",
37
+ "lib/delayed/recipes.rb",
38
+ "lib/delayed/tasks.rb",
39
+ "lib/delayed/worker.rb",
40
+ "lib/delayed_job.rb",
41
+ "rails/init.rb",
42
+ "recipes/delayed_job.rb",
43
+ "spec/backend/active_record_job_spec.rb",
44
+ "spec/backend/data_mapper_job_spec.rb",
45
+ "spec/backend/mongo_mapper_job_spec.rb",
46
+ "spec/backend/shared_backend_spec.rb",
47
+ "spec/delayed_method_spec.rb",
48
+ "spec/performable_method_spec.rb",
49
+ "spec/sample_jobs.rb",
50
+ "spec/setup/active_record.rb",
51
+ "spec/setup/data_mapper.rb",
52
+ "spec/setup/mongo_mapper.rb",
53
+ "spec/spec_helper.rb",
54
+ "spec/story_spec.rb",
55
+ "spec/worker_spec.rb",
56
+ "tasks/jobs.rake"
57
+ ]
58
+ s.homepage = %q{http://github.com/jmcnevin/delayed_job}
59
+ s.rdoc_options = ["--main", "README.textile", "--inline-source", "--line-numbers"]
60
+ s.require_paths = ["lib"]
61
+ s.rubygems_version = %q{1.3.6}
62
+ s.summary = %q{Database-backed asynchronous priority queue system -- Extracted from Shopify}
63
+ s.test_files = [
64
+ "spec/delayed_method_spec.rb",
65
+ "spec/performable_method_spec.rb",
66
+ "spec/story_spec.rb",
67
+ "spec/worker_spec.rb"
68
+ ]
69
+
70
+ if s.respond_to? :specification_version then
71
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
72
+ s.specification_version = 3
73
+
74
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
75
+ s.add_development_dependency(%q<rspec>, [">= 0"])
76
+ s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
77
+ s.add_development_dependency(%q<mongo_mapper>, [">= 0"])
78
+ s.add_development_dependency(%q<dm-core>, [">= 0"])
79
+ s.add_development_dependency(%q<dm-observer>, [">= 0"])
80
+ s.add_development_dependency(%q<dm-aggregates>, [">= 0"])
81
+ s.add_development_dependency(%q<dm-validations>, [">= 0"])
82
+ s.add_development_dependency(%q<do_sqlite3>, [">= 0"])
83
+ s.add_development_dependency(%q<database_cleaner>, [">= 0"])
84
+ else
85
+ s.add_dependency(%q<rspec>, [">= 0"])
86
+ s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
87
+ s.add_dependency(%q<mongo_mapper>, [">= 0"])
88
+ s.add_dependency(%q<dm-core>, [">= 0"])
89
+ s.add_dependency(%q<dm-observer>, [">= 0"])
90
+ s.add_dependency(%q<dm-aggregates>, [">= 0"])
91
+ s.add_dependency(%q<dm-validations>, [">= 0"])
92
+ s.add_dependency(%q<do_sqlite3>, [">= 0"])
93
+ s.add_dependency(%q<database_cleaner>, [">= 0"])
94
+ end
95
+ else
96
+ s.add_dependency(%q<rspec>, [">= 0"])
97
+ s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
98
+ s.add_dependency(%q<mongo_mapper>, [">= 0"])
99
+ s.add_dependency(%q<dm-core>, [">= 0"])
100
+ s.add_dependency(%q<dm-observer>, [">= 0"])
101
+ s.add_dependency(%q<dm-aggregates>, [">= 0"])
102
+ s.add_dependency(%q<dm-validations>, [">= 0"])
103
+ s.add_dependency(%q<do_sqlite3>, [">= 0"])
104
+ s.add_dependency(%q<database_cleaner>, [">= 0"])
105
+ end
106
+ end
107
+