Method: Delayed::Backend::MongoMapper::Job#lock_exclusively!

Defined in:
lib/delayed/backend/mongo_mapper.rb

#lock_exclusively!(max_run_time, worker = worker_name) ⇒ Object

Lock this job for this worker. Returns true if we have the lock, false otherwise.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/delayed/backend/mongo_mapper.rb', line 80

def lock_exclusively!(max_run_time, worker = worker_name)
  right_now = self.class.db_time_now
  overtime = right_now - max_run_time.to_i
  
  query = "this.locked_at == null || this.locked_at < #{make_date(overtime)} || this.locked_by == #{worker.to_json}"
  conditions = {:_id => id, :run_at => {"$lte" => right_now}, "$where" => query}

  collection.update(conditions, {"$set" => {:locked_at => right_now, :locked_by => worker}})
  affected_rows = collection.find({:_id => id, :locked_by => worker}).count
  if affected_rows == 1
    self.locked_at = right_now
    self.locked_by = worker
    return true
  else
    return false
  end
end