Permalink
Browse files

Change $where lookup to $or for finding unlocked jobs.

Appeared to be causing a memory leak inside of Mongo. This currently
break backwards compatability with MongoDB < 1.5.3.

Thanks to Alejando G for the change.
  • Loading branch information...
1 parent 2ae3503 commit 51b3487bf31ade477e5d71743dbe8e34a3152685 @gaffneyc gaffneyc committed Jan 5, 2011
Showing with 11 additions and 12 deletions.
  1. +11 −12 lib/delayed/backend/mongoid.rb
@@ -42,11 +42,19 @@ def self.reserve(worker, max_run_time = Worker.max_run_time)
(conditions[:priority] ||= {})['$gte'] = Worker.min_priority.to_i if Worker.min_priority
(conditions[:priority] ||= {})['$lte'] = Worker.max_priority.to_i if Worker.max_priority
- where = "this.locked_by == '#{worker.name}' || this.locked_at == null || this.locked_at < #{make_date(right_now - max_run_time)}"
- conditions.merge!('$where' => where)
+ conditions['$or'] = [
+ { :locked_by => worker.name },
+ { :locked_at => nil },
+ { :locked_at => { '$lt' => (right_now - max_run_time) }}
+ ]
begin
- 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}})
+ 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}}
+ )
+
# Return result as a Mongoid document.
# When Mongoid starts supporting findAndModify, this extra step should no longer be necessary.
self.find(:first, :conditions => {:_id => result["_id"]})
@@ -60,15 +68,6 @@ def self.clear_locks!(worker_name)
self.collection.update({:locked_by => worker_name}, {"$set" => {:locked_at => nil, :locked_by => nil}}, :multi => true)
end
- private
-
- def self.make_date(date_or_seconds)
- "new Date(#{date_or_seconds.to_f * 1000})"
- end
-
- def make_date(date)
- self.class.make_date(date)
- end
end
end
end

0 comments on commit 51b3487

Please sign in to comment.