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

Defined in:
lib/delayed/backend/data_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.

[View source]

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/delayed/backend/data_mapper.rb', line 71

def lock_exclusively!(max_run_time, worker = worker_name)

  now = self.class.db_time_now
  overtime = now - max_run_time
  
  # FIXME - this is a bit gross
  # DM doesn't give us the number of rows affected by a collection update
  # so we have to circumvent some niceness in DM::Collection here
  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