delayed_job_on_steroids 1.7.1 → 1.7.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -26,6 +26,7 @@ The created table looks as follows:
26
26
  table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue
27
27
  table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually.
28
28
  table.text :handler # YAML-encoded string of the object that will do work
29
+ table.string :job_type # Class name of the job object, for type-specific workers
29
30
  table.string :last_error # reason for last failure (See Note below)
30
31
  table.datetime :run_at # When to run. Could be Time.now for immediately, or sometime in the future.
31
32
  table.datetime :locked_at # Set when a client is working on this object
@@ -104,6 +105,13 @@ Keep in mind that each worker will check the database at least every 5 seconds.
104
105
 
105
106
  Note: The rake task will exit if the database has any network connectivity problems.
106
107
 
108
+ If you only want to run specific types of jobs in a given worker, include them when initializing the worker:
109
+
110
+ <pre><code>
111
+ Delayed::Worker.new(:job_types => "SimpleJob").start
112
+ Delayed::Worker.new(:job_types => ["SimpleJob", "NewsletterJob"]).start
113
+ </pre></code>
114
+
107
115
  h3. Cleaning up
108
116
 
109
117
  You can invoke @rake jobs:clear@ to delete all jobs in the queue.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.7.1
1
+ 1.7.2
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{delayed_job_on_steroids}
8
- s.version = "1.7.1"
8
+ s.version = "1.7.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tobias L\303\274tke", "Aleksey Palazhchenko"]
data/lib/delayed/job.rb CHANGED
@@ -27,9 +27,10 @@ module Delayed
27
27
 
28
28
  ParseObjectFromYaml = /\!ruby\/\w+\:([^\s]+)/
29
29
 
30
- cattr_accessor :min_priority, :max_priority
30
+ cattr_accessor :min_priority, :max_priority, :job_types
31
31
  self.min_priority = nil
32
32
  self.max_priority = nil
33
+ self.job_types = nil
33
34
 
34
35
  # When a worker is exiting, make sure we don't have any locked jobs.
35
36
  def self.clear_locks!
@@ -57,7 +58,8 @@ module Delayed
57
58
  end
58
59
 
59
60
  def payload_object=(object)
60
- self['handler'] = object.to_yaml
61
+ self['job_type'] = object.class.to_s
62
+ self['handler'] = object.to_yaml
61
63
  end
62
64
 
63
65
  # Reschedule the job in the future (when a job fails).
@@ -117,7 +119,6 @@ module Delayed
117
119
  end
118
120
 
119
121
  # Find a few candidate jobs to run (in case some immediately get locked by others).
120
- # Return in random order prevent everyone trying to do same head job at once.
121
122
  def self.find_available(limit = 5, max_run_time = MAX_RUN_TIME)
122
123
 
123
124
  time_now = db_time_now
@@ -136,13 +137,16 @@ module Delayed
136
137
  conditions << max_priority
137
138
  end
138
139
 
140
+ if self.job_types
141
+ sql << ' AND (job_type IN (?))'
142
+ conditions << job_types
143
+ end
144
+
139
145
  conditions.unshift(sql)
140
146
 
141
- records = ActiveRecord::Base.silence do
147
+ ActiveRecord::Base.silence do
142
148
  find(:all, :conditions => conditions, :order => NextTaskOrder, :limit => limit)
143
149
  end
144
-
145
- records.sort_by { rand() }
146
150
  end
147
151
 
148
152
  # Run the next job we can get an exclusive lock on.
@@ -13,6 +13,7 @@ module Delayed
13
13
  @quiet = options[:quiet]
14
14
  Delayed::Job.min_priority = options[:min_priority] if options.has_key?(:min_priority)
15
15
  Delayed::Job.max_priority = options[:max_priority] if options.has_key?(:max_priority)
16
+ Delayed::Job.job_types = options[:job_types] if options.has_key?(:job_types)
16
17
  end
17
18
 
18
19
  def start
data/spec/database.rb CHANGED
@@ -19,6 +19,7 @@ ActiveRecord::Schema.define do
19
19
  table.integer :priority, :default => 0
20
20
  table.integer :attempts, :default => 0
21
21
  table.text :handler
22
+ table.string :job_type
22
23
  table.string :last_error
23
24
  table.datetime :run_at
24
25
  table.datetime :locked_at
data/spec/job_spec.rb CHANGED
@@ -69,8 +69,31 @@ describe Delayed::Job do
69
69
 
70
70
  SimpleJob.runs.should == 1
71
71
  end
72
-
73
-
72
+
73
+ it "should work on specified job types" do
74
+ SimpleJob.runs.should == 0
75
+
76
+ Delayed::Job.job_types = "SimpleJob"
77
+ Delayed::Job.enqueue SimpleJob.new
78
+ Delayed::Job.work_off
79
+
80
+ SimpleJob.runs.should == 1
81
+
82
+ Delayed::Job.job_types = nil
83
+ end
84
+
85
+ it "should not work on unspecified job types" do
86
+ SimpleJob.runs.should == 0
87
+
88
+ Delayed::Job.job_types = "AnotherJob"
89
+ Delayed::Job.enqueue SimpleJob.new
90
+ Delayed::Job.work_off
91
+
92
+ SimpleJob.runs.should == 0
93
+
94
+ Delayed::Job.job_types = nil
95
+ end
96
+
74
97
  it "should work with eval jobs" do
75
98
  $eval_job_ran = false
76
99
 
@@ -269,7 +292,21 @@ describe Delayed::Job do
269
292
  Delayed::Job.work_off
270
293
 
271
294
  SimpleJob.runs.should == 1
272
- end
295
+ end
296
+
297
+ it "should fetch jobs ordered by priority" do
298
+ NUM = 10
299
+ NUM.times { Delayed::Job.enqueue SimpleJob.new, rand(NUM) }
300
+ jobs = Delayed::Job.find_available(NUM)
301
+ ordered = true
302
+ jobs[0..-2].each_index do |i|
303
+ if (jobs[i].priority < jobs[i+1].priority)
304
+ ordered = false
305
+ break
306
+ end
307
+ end
308
+ ordered.should == true
309
+ end
273
310
 
274
311
  end
275
312
 
data/tasks/tasks.rb CHANGED
@@ -8,8 +8,9 @@ namespace :jobs do
8
8
  Delayed::Job.delete_all
9
9
  end
10
10
 
11
- desc "Start a delayed_job worker."
11
+ desc "Start a delayed_job worker. Options: MIN_PRIORITY, MAX_PRIORITY, JOB_TYPES."
12
12
  task :work => [:merb_env, :environment] do
13
- Delayed::Worker.new(:min_priority => ENV['MIN_PRIORITY'], :max_priority => ENV['MAX_PRIORITY']).start
13
+ Delayed::Worker.new(:min_priority => ENV['MIN_PRIORITY'], :max_priority => ENV['MAX_PRIORITY'],
14
+ :job_types => ENV['JOB_TYPES']).start
14
15
  end
15
16
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 7
8
- - 1
9
- version: 1.7.1
8
+ - 2
9
+ version: 1.7.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Tobias L\xC3\xBCtke"