def lock_exclusively!(max_run_time, worker = worker_name)
now = self.class.db_time_now
overtime = now - max_run_time
collection = locked_by != worker ?
(self.class.all(:id => id, :run_at.lte => now) & ( self.class.all(:locked_at => nil) | self.class.all(:locked_at.lt => overtime) ) ) :
self.class.all(:id => id, :locked_by => worker)
attributes = collection.model.new(:locked_at => now, :locked_by => worker).dirty_attributes
affected_rows = self.repository.update(attributes, collection)
if affected_rows == 1
self.locked_at = now
self.locked_by = worker
return true
else
return false
end
end