delayed_job_mongoid 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +3 -3
  2. data/lib/delayed/backend/mongoid.rb +20 -28
  3. metadata +9 -4
data/README.md CHANGED
@@ -4,10 +4,10 @@
4
4
 
5
5
  Add the gems to your Gemfile:
6
6
 
7
- gem 'delayed_job', '2.1.0.pre2'
8
- gem 'delayed_job_mongoid', '1.0.0.rc'
7
+ gem 'delayed_job'
8
+ gem 'delayed_job_mongoid'
9
9
 
10
- Create the indexes:
10
+ After running `bundle install`, create the indexes (and don't forget to do this on your production database):
11
11
 
12
12
  script/rails runner 'Delayed::Backend::Mongoid::Job.create_indexes'
13
13
 
@@ -14,7 +14,7 @@ module Delayed
14
14
  field :failed_at, :type=> Time
15
15
  field :last_error, :type=> String
16
16
 
17
- index :locked_by
17
+ index ([[:locked_by, -1], [:priority, 1], [:run_at, 1]])
18
18
 
19
19
  before_save :set_default_run_at
20
20
 
@@ -30,45 +30,37 @@ module Delayed
30
30
  Time.now.utc
31
31
  end
32
32
 
33
- def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time)
33
+ # Reserves this job for the worker.
34
+ #
35
+ # Uses Mongo's findAndModify operation to atomically pick and lock one
36
+ # job from from the collection. findAndModify is not yet available
37
+ # directly thru Mongoid so go down to the Mongo Ruby driver instead.
38
+ def self.reserve(worker, max_run_time = Worker.max_run_time)
34
39
  right_now = db_time_now
35
40
 
36
41
  conditions = {:run_at => {"$lte" => right_now}, :failed_at => nil}
37
42
  (conditions[:priority] ||= {})['$gte'] = Worker.min_priority.to_i if Worker.min_priority
38
43
  (conditions[:priority] ||= {})['$lte'] = Worker.max_priority.to_i if Worker.max_priority
39
44
 
45
+ where = "this.locked_by == '#{worker.name}' || this.locked_at == null || this.locked_at < #{make_date(right_now - max_run_time)}"
46
+ conditions.merge!('$where' => where)
40
47
 
41
- where = "this.locked_at == null || this.locked_at < #{make_date(right_now - max_run_time)}"
42
- results = self.where(conditions.merge(:locked_by => worker_name)).limit(-limit).order_by([['priority', 1], ['run_at', 1]]).to_a
43
- results += self.where(conditions.merge('$where' => where)).limit(-limit+results.size).order_by([['priority', 1], ['run_at', 1]]).to_a if results.size < limit
44
- results
48
+ begin
49
+ result = self.db.collection(self.collection.name).find_and_modify(:query => conditions, :sort => [['locked_by', -1], ['priority', 1], ['run_at', 1]], :update => {"$set" => {:locked_at => right_now, :locked_by => worker.name}})
50
+ # Return result as a Mongoid document.
51
+ # When Mongoid starts supporting findAndModify, this extra step should no longer be necessary.
52
+ self.find(:first, :conditions => {:_id => result["_id"]})
53
+ rescue Mongo::OperationFailure
54
+ nil # no jobs available
55
+ end
45
56
  end
57
+
46
58
  # When a worker is exiting, make sure we don't have any locked jobs.
47
59
  def self.clear_locks!(worker_name)
48
60
  self.collection.update({:locked_by => worker_name}, {"$set" => {:locked_at => nil, :locked_by => nil}}, :multi => true)
49
61
  end
50
62
 
51
- # Lock this job for this worker.
52
- # Returns true if we have the lock, false otherwise.
53
- def lock_exclusively!(max_run_time, worker = worker_name)
54
- right_now = self.class.db_time_now
55
- overtime = right_now - max_run_time.to_i
56
-
57
- query = "this.locked_at == null || this.locked_at < #{make_date(overtime)} || this.locked_by == #{worker.to_json}"
58
- conditions = {:_id => id, :run_at => {"$lte" => right_now}, "$where" => query}
59
-
60
- self.collection.update(conditions, {"$set" => {:locked_at => right_now, :locked_by => worker}})
61
- affected_rows = self.collection.find({:_id => id, :locked_by => worker}).count
62
- if affected_rows == 1
63
- self.locked_at = right_now
64
- self.locked_by = worker
65
- return true
66
- else
67
- return false
68
- end
69
- end
70
-
71
- private
63
+ private
72
64
 
73
65
  def self.make_date(date_or_seconds)
74
66
  "new Date(#{date_or_seconds.to_f * 1000})"
@@ -80,4 +72,4 @@ module Delayed
80
72
  end
81
73
  end
82
74
  end
83
- end
75
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 0
9
- version: 1.0.0
8
+ - 1
9
+ version: 1.0.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brandon Keepers
@@ -14,13 +14,14 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-11-14 00:00:00 -06:00
17
+ date: 2010-12-01 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: mongoid
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
24
25
  requirements:
25
26
  - - ~>
26
27
  - !ruby/object:Gem::Version
@@ -34,6 +35,7 @@ dependencies:
34
35
  name: delayed_job
35
36
  prerelease: false
36
37
  requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
37
39
  requirements:
38
40
  - - ~>
39
41
  - !ruby/object:Gem::Version
@@ -48,6 +50,7 @@ dependencies:
48
50
  name: rspec
49
51
  prerelease: false
50
52
  requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
51
54
  requirements:
52
55
  - - ">="
53
56
  - !ruby/object:Gem::Version
@@ -85,6 +88,7 @@ rdoc_options:
85
88
  require_paths:
86
89
  - lib
87
90
  required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
88
92
  requirements:
89
93
  - - ">="
90
94
  - !ruby/object:Gem::Version
@@ -92,6 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
96
  - 0
93
97
  version: "0"
94
98
  required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
95
100
  requirements:
96
101
  - - ">="
97
102
  - !ruby/object:Gem::Version
@@ -101,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
106
  requirements: []
102
107
 
103
108
  rubyforge_project:
104
- rubygems_version: 1.3.6
109
+ rubygems_version: 1.3.7
105
110
  signing_key:
106
111
  specification_version: 3
107
112
  summary: Mongoid backend for delayed_job