Skip to content
This repository
Browse code

Mongoid 3 support

  • Loading branch information...
commit b24f871849b34cc9e9cf86e2e26cfbeeb21a45ce 1 parent 68bcf62
Alexey Savartsov authored May 23, 2012 sferik committed July 26, 2012
43  lib/delayed/backend/mongoid.rb
@@ -24,14 +24,6 @@ class Job
24 24
 
25 25
         before_save :set_default_run_at
26 26
 
27  
-        def self.before_fork
28  
-          ::Mongoid.master.connection.close
29  
-        end
30  
-
31  
-        def self.after_fork
32  
-          ::Mongoid.master.connection.connect
33  
-        end
34  
-
35 27
         def self.db_time_now
36 28
           Time.now.utc
37 29
         end
@@ -39,40 +31,31 @@ def self.db_time_now
39 31
         # Reserves this job for the worker.
40 32
         #
41 33
         # Uses Mongo's findAndModify operation to atomically pick and lock one
42  
-        # job from from the collection. findAndModify is not yet available
43  
-        # directly thru Mongoid so go down to the Mongo Ruby driver instead.
  34
+        # job from from the collection. 
44 35
         def self.reserve(worker, max_run_time = Worker.max_run_time)
45 36
           right_now = db_time_now
46 37
 
47  
-          conditions = {:run_at  => {"$lte" => right_now}, :failed_at => nil}
48  
-          (conditions[:priority] ||= {})['$gte'] = Worker.min_priority.to_i if Worker.min_priority
49  
-          (conditions[:priority] ||= {})['$lte'] = Worker.max_priority.to_i if Worker.max_priority
50  
-          (conditions[:queue] ||= {})['$in'] = Worker.queues if Worker.queues.any?
51  
-
52  
-          conditions['$or'] = [
  38
+          criteria = self.where(
  39
+            :run_at => {"$lte" => right_now}, 
  40
+            :failed_at => nil
  41
+          ).any_of(
53 42
             { :locked_by => worker.name },
54 43
             { :locked_at => nil },
55 44
             { :locked_at => { '$lt' => (right_now - max_run_time) }}
56  
-          ]
  45
+          )
57 46
 
58  
-          begin
59  
-            result = self.db.collection(self.collection.name).find_and_modify(
60  
-              :query  => conditions,
61  
-              :sort   => [['locked_by', -1], ['priority', 1], ['run_at', 1]],
62  
-              :update => {"$set" => {:locked_at => right_now, :locked_by => worker.name}}
63  
-            )
  47
+          criteria = criteria.where(:priority => {"$gte" => Worker.min_priority.to_i}) if Worker.min_priority
  48
+          criteria = criteria.where(:priority => {"$lte" => Worker.max_priority.to_i}) if Worker.max_priority
  49
+          criteria = criteria.any_in(:queue => Worker.queues) if Worker.queues.any?
64 50
 
65  
-            # Return result as a Mongoid document.
66  
-            # When Mongoid starts supporting findAndModify, this extra step should no longer be necessary.
67  
-            self.find(result["_id"]) unless result.nil?
68  
-          rescue Mongo::OperationFailure
69  
-            nil # no jobs available
70  
-          end
  51
+          criteria.desc(:locked_by).asc(:priority).asc(:run_at).find_and_modify(
  52
+            "$set" => {:locked_at => right_now, :locked_by => worker.name}
  53
+          )
71 54
         end
72 55
 
73 56
         # When a worker is exiting, make sure we don't have any locked jobs.
74 57
         def self.clear_locks!(worker_name)
75  
-          self.collection.update({:locked_by => worker_name}, {"$set" => {:locked_at => nil, :locked_by => nil}}, :multi => true)
  58
+          self.where(:locked_by => worker_name).update_all({:locked_at => nil, :locked_by => nil})
76 59
         end
77 60
 
78 61
         def reload(*args)
26  spec/delayed_job_mongoid_spec.rb
@@ -2,28 +2,4 @@
2 2
 
3 3
 describe Delayed::Backend::Mongoid::Job do
4 4
   it_should_behave_like 'a delayed_job backend'
5  
-  
6  
-  describe "before_fork" do
7  
-    after do
8  
-      ::Mongoid.master.connection.close
9  
-    end
10  
-    
11  
-    it "should disconnect" do
12  
-      lambda do
13  
-        Delayed::Backend::Mongoid::Job.before_fork
14  
-      end.should change { !!Mongoid.master.connection.connected? }.from(true).to(false)
15  
-    end
16  
-  end
17  
-
18  
-  describe "after_fork" do
19  
-    before do
20  
-      ::Mongoid.master.connection.close
21  
-    end
22  
-    
23  
-    it "should call reconnect" do
24  
-      lambda do
25  
-        Delayed::Backend::Mongoid::Job.after_fork
26  
-      end.should change { !!Mongoid.master.connection.connected? }.from(false).to(true)
27  
-    end
28  
-  end
29  
-end
  5
+end
2  spec/spec_helper.rb
@@ -7,7 +7,7 @@
7 7
 require 'delayed/backend/shared_spec'
8 8
 
9 9
 Mongoid.configure do |config|
10  
-  config.master = config.master = Mongo::Connection.new.db('dl_spec')
  10
+  config.connect_to("dl_spec")
11 11
 end
12 12
 
13 13
 class Story

0 notes on commit b24f871

Please sign in to comment.
Something went wrong with that request. Please try again.