koombea-delayed_job 1.8.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ *.gem
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Andres Pena
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.
@@ -0,0 +1,166 @@
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
+ Koombea's fork adds a user and object relationship to the table in case you need the relationships for stats or queue monitoring.
16
+
17
+
18
+ h2. Installation
19
+
20
+ To install as a gem, add the following to @config/environment.rb@:
21
+
22
+ <pre>
23
+ config.gem 'koombea-delayed_job', :lib => 'delayed_job'
24
+ </pre>
25
+
26
+ Rake tasks are not automatically loaded from gems, so you'll need to add the following to your Rakefile:
27
+
28
+ <pre>
29
+ begin
30
+ require 'delayed/tasks'
31
+ rescue LoadError
32
+ STDERR.puts "Run `rake gems:install` to install delayed_job"
33
+ end
34
+ </pre>
35
+
36
+ To install as a plugin:
37
+
38
+ <pre>
39
+ script/plugin install git://github.com/koombea/delayed_job.git
40
+ </pre>
41
+
42
+ After delayed_job is installed, run:
43
+
44
+ <pre>
45
+ script/generate delayed_job
46
+ rake db:migrate
47
+ </pre>
48
+
49
+ h2. Upgrading to 1.8
50
+
51
+ If you are upgrading from a previous release, you will need to generate the new @script/delayed_job@:
52
+
53
+ <pre>
54
+ script/generate delayed_job --skip-migration
55
+ </pre>
56
+
57
+ h2. Queuing Jobs
58
+
59
+ Call @#send_later(method, params)@ on any object and it will be processed in the background.
60
+
61
+ <pre>
62
+ # without delayed_job
63
+ Notifier.deliver_signup(@user)
64
+
65
+ # with delayed_job
66
+ Notifier.send_later :deliver_signup, @user
67
+ </pre>
68
+
69
+ If a method should always be run in the background, you can call @#handle_asynchronously@ after the method declaration:
70
+
71
+ <pre>
72
+ class Device
73
+ def deliver
74
+ # long running method
75
+ end
76
+ handle_asynchronously :deliver
77
+ end
78
+
79
+ device = Device.new
80
+ device.deliver
81
+ </pre>
82
+
83
+ h2. Running Jobs
84
+
85
+ @script/delayed_job@ can be used to manage a background process which will start working off jobs.
86
+
87
+ <pre>
88
+ $ RAILS_ENV=production script/delayed_job start
89
+ $ RAILS_ENV=production script/delayed_job stop
90
+
91
+ # Runs two workers in separate processes.
92
+ $ RAILS_ENV=production script/delayed_job -n 2 start
93
+ $ RAILS_ENV=production script/delayed_job stop
94
+ </pre>
95
+
96
+ 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.
97
+
98
+ You can also invoke @rake jobs:work@ which will start working off jobs. You can cancel the rake task with @CTRL-C@.
99
+
100
+ h2. Custom Jobs
101
+
102
+ 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.
103
+
104
+ <pre>
105
+ class NewsletterJob < Struct.new(:text, :emails)
106
+ def perform
107
+ emails.each { |e| NewsletterMailer.deliver_text_to_email(text, e) }
108
+ end
109
+ end
110
+
111
+ Delayed::Job.enqueue NewsletterJob.new('lorem ipsum...', Customers.find(:all).collect(&:email))
112
+ </pre>
113
+
114
+ == Koombea edit ==
115
+ To send user and object id's simply use:
116
+
117
+ <pre>
118
+ Delayed::Job.enqueue NewsletterJob.new('lorem ipsum...', Customers.find(:all).collect(&:email)), priority, run_at, user_id, object_id
119
+ </pre>
120
+
121
+ h2. Gory Details
122
+
123
+ The library evolves around a delayed_jobs table which looks as follows:
124
+
125
+ create_table :delayed_jobs, :force => true do |table|
126
+ table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue
127
+ table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually.
128
+ table.text :handler # YAML-encoded string of the object that will do work
129
+ table.text :last_error # reason for last failure (See Note below)
130
+ table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future.
131
+ table.datetime :locked_at # Set when a client is working on this object
132
+ table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
133
+ table.string :locked_by # Who is working on this object (if locked)
134
+ table.timestamps
135
+ end
136
+
137
+ On failure, the job is scheduled again in 5 seconds + N ** 4, where N is the number of retries.
138
+
139
+ The default Job::max_attempts is 25. After this, the job either deleted (default), or left in the database with "failed_at" set.
140
+ With the default of 25 attempts, the last retry will be 20 days later, with the last interval being almost 100 hours.
141
+
142
+ The default Job::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
143
+ make sure your job doesn't exceed this time. You should set this to the longest time you think the job could take.
144
+
145
+ By default, it will delete failed jobs (and it always deletes successful jobs). If you want to keep failed jobs, set
146
+ Delayed::Job.destroy_failed_jobs = false. The failed jobs will be marked with non-null failed_at.
147
+
148
+ Here is an example of changing job parameters in Rails:
149
+
150
+ <pre>
151
+ # config/initializers/delayed_job_config.rb
152
+ Delayed::Job.destroy_failed_jobs = false
153
+ silence_warnings do
154
+ Delayed::Worker::sleep_delay = 60
155
+ Delayed::Job::max_attempts = 3
156
+ Delayed::Job::max_run_time = 5.minutes
157
+ end
158
+ </pre>
159
+
160
+ h3. Cleaning up
161
+
162
+ You can invoke @rake jobs:clear@ to delete all jobs in the queue.
163
+
164
+ h2. Mailing List
165
+
166
+ Join us on the mailing list at http://groups.google.com/group/delayed_job
@@ -0,0 +1,34 @@
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 = "koombea-delayed_job"
11
+ s.summary = "Database-backed asynchronous priority queue system -- Extracted from Shopify"
12
+ s.email = "max@koombea.com"
13
+ s.homepage = "http://github.com/koombea/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."
15
+ s.authors = ["Andres Pena", "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/**/*']
22
+ end
23
+
24
+ require 'spec/rake/spectask'
25
+
26
+ task :default => :spec
27
+
28
+ desc 'Run the specs'
29
+ Spec::Rake::SpecTask.new(:spec) do |t|
30
+ t.libs << 'lib'
31
+ t.pattern = 'spec/**/*_spec.rb'
32
+ t.verbose = true
33
+ end
34
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.8.5
@@ -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,22 @@
1
+ class DelayedJobGenerator < Rails::Generator::Base
2
+ default_options :skip_migration => false
3
+
4
+ def manifest
5
+ record do |m|
6
+ m.template 'script', 'script/delayed_job', :chmod => 0755
7
+ unless options[:skip_migration]
8
+ m.migration_template "migration.rb", 'db/migrate',
9
+ :migration_file_name => "create_delayed_jobs"
10
+ end
11
+ end
12
+ end
13
+
14
+ protected
15
+
16
+ def add_options!(opt)
17
+ opt.separator ''
18
+ opt.separator 'Options:'
19
+ opt.on("--skip-migration", "Don't generate a migration") { |v| options[:skip_migration] = v }
20
+ end
21
+
22
+ end
@@ -0,0 +1,22 @@
1
+ class CreateDelayedJobs < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :delayed_jobs, :force => true do |table|
4
+ table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue
5
+ table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually.
6
+ table.text :handler # YAML-encoded string of the object that will do work
7
+ table.text :last_error # reason for last failure (See Note below)
8
+ table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future.
9
+ table.datetime :locked_at # Set when a client is working on this object
10
+ table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
11
+ table.string :locked_by # Who is working on this object (if locked)
12
+ table.integer :user_id # Who created this job
13
+ table.ingeter :object_id # The object related to this job
14
+ table.timestamps
15
+ end
16
+
17
+ end
18
+
19
+ def self.down
20
+ drop_table :delayed_jobs
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
4
+ require 'delayed/command'
5
+ Delayed::Command.new(ARGV).daemonize
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/lib/delayed_job'
@@ -0,0 +1,76 @@
1
+ require 'rubygems'
2
+ require 'daemons'
3
+ require 'optparse'
4
+
5
+ module Delayed
6
+ class Command
7
+ attr_accessor :worker_count
8
+
9
+ def initialize(args)
10
+ @files_to_reopen = []
11
+ @options = {:quiet => true}
12
+
13
+ @worker_count = 1
14
+
15
+ opts = OptionParser.new do |opts|
16
+ opts.banner = "Usage: #{File.basename($0)} [options] start|stop|restart|run"
17
+
18
+ opts.on('-h', '--help', 'Show this message') do
19
+ puts opts
20
+ exit 1
21
+ end
22
+ opts.on('-e', '--environment=NAME', 'Specifies the environment to run this delayed jobs under (test/development/production).') do |e|
23
+ STDERR.puts "The -e/--environment option has been deprecated and has no effect. Use RAILS_ENV and see http://github.com/collectiveidea/delayed_job/issues/#issue/7"
24
+ end
25
+ opts.on('--min-priority N', 'Minimum priority of jobs to run.') do |n|
26
+ @options[:min_priority] = n
27
+ end
28
+ opts.on('--max-priority N', 'Maximum priority of jobs to run.') do |n|
29
+ @options[:max_priority] = n
30
+ end
31
+ opts.on('-n', '--number_of_workers=workers', "Number of unique workers to spawn") do |worker_count|
32
+ @worker_count = worker_count.to_i rescue 1
33
+ end
34
+ end
35
+ @args = opts.parse!(args)
36
+ end
37
+
38
+ def daemonize
39
+ ObjectSpace.each_object(File) do |file|
40
+ @files_to_reopen << file unless file.closed?
41
+ end
42
+
43
+ worker_count.times do |worker_index|
44
+ process_name = worker_count == 1 ? "delayed_job" : "delayed_job.#{worker_index}"
45
+ Daemons.run_proc(process_name, :dir => "#{RAILS_ROOT}/tmp/pids", :dir_mode => :normal, :ARGV => @args) do |*args|
46
+ run process_name
47
+ end
48
+ end
49
+ end
50
+
51
+ def run(worker_name = nil)
52
+ Dir.chdir(RAILS_ROOT)
53
+
54
+ # Re-open file handles
55
+ @files_to_reopen.each do |file|
56
+ begin
57
+ file.reopen File.join(RAILS_ROOT, 'log', 'delayed_job.log'), 'a+'
58
+ file.sync = true
59
+ rescue ::Exception
60
+ end
61
+ end
62
+
63
+ Delayed::Worker.logger = Rails.logger
64
+ ActiveRecord::Base.connection.reconnect!
65
+
66
+ worker = Delayed::Worker.new(@options)
67
+ worker.name_prefix = "#{worker_name} "
68
+ worker.start
69
+ rescue => e
70
+ Rails.logger.fatal e
71
+ STDERR.puts e.message
72
+ exit 1
73
+ end
74
+
75
+ end
76
+ end
@@ -0,0 +1,226 @@
1
+ require 'timeout'
2
+
3
+ module Delayed
4
+
5
+ class DeserializationError < StandardError
6
+ end
7
+
8
+ # A job object that is persisted to the database.
9
+ # Contains the work object as a YAML field.
10
+ class Job < ActiveRecord::Base
11
+ @@max_attempts = 25
12
+ @@max_run_time = 4.hours
13
+
14
+ cattr_accessor :max_attempts, :max_run_time
15
+
16
+ set_table_name :delayed_jobs
17
+
18
+ # By default failed jobs are destroyed after too many attempts.
19
+ # If you want to keep them around (perhaps to inspect the reason
20
+ # for the failure), set this to false.
21
+ cattr_accessor :destroy_failed_jobs
22
+ self.destroy_failed_jobs = true
23
+
24
+ named_scope :ready_to_run, lambda {|worker_name, max_run_time|
25
+ {:conditions => ['(run_at <= ? AND (locked_at IS NULL OR locked_at < ?) OR locked_by = ?) AND failed_at IS NULL', db_time_now, db_time_now - max_run_time, worker_name]}
26
+ }
27
+ named_scope :by_priority, :order => 'priority DESC, run_at ASC'
28
+
29
+ ParseObjectFromYaml = /\!ruby\/\w+\:([^\s]+)/
30
+
31
+ cattr_accessor :min_priority, :max_priority
32
+ self.min_priority = nil
33
+ self.max_priority = nil
34
+
35
+ # When a worker is exiting, make sure we don't have any locked jobs.
36
+ def self.clear_locks!(worker_name)
37
+ update_all("locked_by = null, locked_at = null", ["locked_by = ?", worker_name])
38
+ end
39
+
40
+ def failed?
41
+ failed_at
42
+ end
43
+ alias_method :failed, :failed?
44
+
45
+ def payload_object
46
+ @payload_object ||= deserialize(self['handler'])
47
+ end
48
+
49
+ def name
50
+ @name ||= begin
51
+ payload = payload_object
52
+ if payload.respond_to?(:display_name)
53
+ payload.display_name
54
+ else
55
+ payload.class.name
56
+ end
57
+ end
58
+ end
59
+
60
+ def payload_object=(object)
61
+ self['handler'] = object.to_yaml
62
+ end
63
+
64
+ # Reschedule the job in the future (when a job fails).
65
+ # Uses an exponential scale depending on the number of failed attempts.
66
+ def reschedule(message, backtrace = [], time = nil)
67
+ self.last_error = message + "\n" + backtrace.join("\n")
68
+
69
+ if (self.attempts += 1) < max_attempts
70
+ time ||= Job.db_time_now + (attempts ** 4) + 5
71
+
72
+ self.run_at = time
73
+ self.unlock
74
+ save!
75
+ else
76
+ logger.info "* [JOB] PERMANENTLY removing #{self.name} because of #{attempts} consecutive failures."
77
+ destroy_failed_jobs ? destroy : update_attribute(:failed_at, Delayed::Job.db_time_now)
78
+ end
79
+ end
80
+
81
+
82
+ # Try to lock and run job. Returns true/false (work done/work failed) or nil if job can't be locked.
83
+ def run_with_lock(max_run_time, worker_name)
84
+ logger.info "* [JOB] acquiring lock on #{name}"
85
+ if lock_exclusively!(max_run_time, worker_name)
86
+ run(max_run_time)
87
+ else
88
+ # We did not get the lock, some other worker process must have
89
+ logger.warn "* [JOB] failed to acquire exclusive lock for #{name}"
90
+ nil # no work done
91
+ end
92
+ end
93
+
94
+ # Try to run job. Returns true/false (work done/work failed)
95
+ def run(max_run_time)
96
+ runtime = Benchmark.realtime do
97
+ Timeout.timeout(max_run_time.to_i) { invoke_job }
98
+ destroy
99
+ end
100
+ # TODO: warn if runtime > max_run_time ?
101
+ logger.info "* [JOB] #{name} completed after %.4f" % runtime
102
+ return true # did work
103
+ rescue Exception => e
104
+ reschedule e.message, e.backtrace
105
+ log_exception(e)
106
+ return false # work failed
107
+ end
108
+
109
+ # Add a job to the queue
110
+ def self.enqueue(*args, &block)
111
+ object = block_given? ? EvaledJob.new(&block) : args.shift
112
+
113
+ unless object.respond_to?(:perform) || block_given?
114
+ raise ArgumentError, 'Cannot enqueue items which do not respond to perform'
115
+ end
116
+
117
+ priority = args.first || 0
118
+ run_at = args[1]
119
+ user = args[2]
120
+ related = args[3]
121
+
122
+ Job.create(:payload_object => object, :priority => priority.to_i, :run_at => run_at, :user_id => user, :object_id => related)
123
+ end
124
+
125
+ # Find a few candidate jobs to run (in case some immediately get locked by others).
126
+ def self.find_available(worker_name, limit = 5, max_run_time = max_run_time)
127
+ scope = self.ready_to_run(worker_name, max_run_time)
128
+ scope = scope.scoped(:conditions => ['priority >= ?', min_priority]) if min_priority
129
+ scope = scope.scoped(:conditions => ['priority <= ?', max_priority]) if max_priority
130
+
131
+ ActiveRecord::Base.silence do
132
+ scope.by_priority.all(:limit => limit)
133
+ end
134
+ end
135
+
136
+ # Lock this job for this worker.
137
+ # Returns true if we have the lock, false otherwise.
138
+ def lock_exclusively!(max_run_time, worker)
139
+ now = self.class.db_time_now
140
+ affected_rows = if locked_by != worker
141
+ # We don't own this job so we will update the locked_by name and the locked_at
142
+ self.class.update_all(["locked_at = ?, locked_by = ?", now, worker], ["id = ? and (locked_at is null or locked_at < ?) and (run_at <= ?)", id, (now - max_run_time.to_i), now])
143
+ else
144
+ # We already own this job, this may happen if the job queue crashes.
145
+ # Simply resume and update the locked_at
146
+ self.class.update_all(["locked_at = ?", now], ["id = ? and locked_by = ?", id, worker])
147
+ end
148
+ if affected_rows == 1
149
+ self.locked_at = now
150
+ self.locked_by = worker
151
+ return true
152
+ else
153
+ return false
154
+ end
155
+ end
156
+
157
+ # Unlock this job (note: not saved to DB)
158
+ def unlock
159
+ self.locked_at = nil
160
+ self.locked_by = nil
161
+ end
162
+
163
+ # This is a good hook if you need to report job processing errors in additional or different ways
164
+ def log_exception(error)
165
+ logger.error "* [JOB] #{name} failed with #{error.class.name}: #{error.message} - #{attempts} failed attempts"
166
+ logger.error(error)
167
+ end
168
+
169
+ # Moved into its own method so that new_relic can trace it.
170
+ def invoke_job
171
+ payload_object.perform
172
+ end
173
+
174
+ private
175
+
176
+ def deserialize(source)
177
+ handler = YAML.load(source) rescue nil
178
+
179
+ unless handler.respond_to?(:perform)
180
+ if handler.nil? && source =~ ParseObjectFromYaml
181
+ handler_class = $1
182
+ end
183
+ attempt_to_load(handler_class || handler.class)
184
+ handler = YAML.load(source)
185
+ end
186
+
187
+ return handler if handler.respond_to?(:perform)
188
+
189
+ raise DeserializationError,
190
+ 'Job failed to load: Unknown handler. Try to manually require the appropriate file.'
191
+ rescue TypeError, LoadError, NameError => e
192
+ raise DeserializationError,
193
+ "Job failed to load: #{e.message}. Try to manually require the required file."
194
+ end
195
+
196
+ # Constantize the object so that ActiveSupport can attempt
197
+ # its auto loading magic. Will raise LoadError if not successful.
198
+ def attempt_to_load(klass)
199
+ klass.constantize
200
+ end
201
+
202
+ # Get the current time (GMT or local depending on DB)
203
+ # Note: This does not ping the DB to get the time, so all your clients
204
+ # must have syncronized clocks.
205
+ def self.db_time_now
206
+ (ActiveRecord::Base.default_timezone == :utc) ? Time.now.utc : Time.zone.now
207
+ end
208
+
209
+ protected
210
+
211
+ def before_save
212
+ self.run_at ||= self.class.db_time_now
213
+ end
214
+
215
+ end
216
+
217
+ class EvaledJob
218
+ def initialize
219
+ @job = yield
220
+ end
221
+
222
+ def perform
223
+ eval(@job)
224
+ end
225
+ end
226
+ end