lib/delayed/job.rb
# This method is used internally by reserve method to ensure exclusive access
# to the given job. It will rise a LockError if it cannot get this lock. 
This method is used internally by reserve method to ensure exclusive access
to the given job. It will rise a LockError if it cannot get this lock.
This method is used internally by reserve method to ensure exclusive access to the given job . It will rise a LockError if it cannot get this lock .
def lock_exclusively!(max_run_time, worker = worker_name)
      now = self.class.db_time_now                 
      affected_rows = if locked_by != worker                  
        connection.update(<<-end_sql, "#{self.class.name} Update to aquire exclusive lock")
          UPDATE #{self.class.table_name}
          SET `locked_at`=#{quote_value(now)}, `locked_by`=#{quote_value(worker)} 
          WHERE #{self.class.primary_key} = #{quote_value(id)} AND (`locked_at` IS NULL OR `locked_at` < #{quote_value(now + max_run_time)})
        end_sql
      else          
        connection.update(<<-end_sql, "#{self.class.name} Update exclusive lock")
          UPDATE #{self.class.table_name}
          SET `locked_at`=#{quote_value(now)} 
          WHERE #{self.class.primary_key} = #{quote_value(id)} AND (`locked_by`=#{quote_value(worker)})
        end_sql
      end
      unless affected_rows == 1
        raise LockError, "Attempted to aquire exclusive lock failed"
      end      
      self.locked_at    = now
      self.locked_by    = worker      
    end
def lock_exclusively! ( max_run_time , worker = worker_name ) now = self . class . db_time_now affected_rows = if locked_by != worker connection . update ( <<-end_sql , "#{self.class.name} Update to aquire exclusive lock" ) 
          UPDATE  #{ self . class . table_name } 
          SET `locked_at`= #{ quote_value ( now ) } , `locked_by`= #{ quote_value ( worker ) }  
          WHERE  #{ self . class . primary_key }  =  #{ quote_value ( id ) }  AND (`locked_at` IS NULL OR `locked_at` <  #{ quote_value ( now + max_run_time ) } )
         end_sql else connection . update ( <<-end_sql , "#{self.class.name} Update exclusive lock" ) 
          UPDATE  #{ self . class . table_name } 
          SET `locked_at`= #{ quote_value ( now ) }  
          WHERE  #{ self . class . primary_key }  =  #{ quote_value ( id ) }  AND (`locked_by`= #{ quote_value ( worker ) } )
         end_sql end unless affected_rows == 1 raise LockError , "Attempted to aquire exclusive lock failed" end self . locked_at = now self . locked_by = worker end
This method is used internally by reserve method to ensure exclusive access
to the given job.
This method is used internally by reserve method to ensure exclusive access to the given job .
# We don't own this job so we will update the locked_by name and the locked_at # We alrady own this job, this may happen if the job queue crashes.  # Simply resume and update the locked_at
max_run_time
<not_specific>
worker
<not_specific>
worker_name
<not_specific>