18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/delayed_job_groups/mongoid_groups.rb', line 18
def self.reserve(worker, max_run_time = Worker.max_run_time)
right_now = db_time_now
conditions = {:run_at => {"$lte" => right_now}, :failed_at => nil, :lock_group => { "$nin" => self.locked_groups } }
(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)
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}})
self.find(:first, :conditions => {:_id => result["_id"]})
rescue Mongo::OperationFailure
nil end
end
|