You are here

public function DatabaseLockBackend::lockMayBeAvailable in Service Container 7.2

Same name and namespace in other branches
  1. 7 lib/Drupal/Core/Lock/DatabaseLockBackend.php \Drupal\Core\Lock\DatabaseLockBackend::lockMayBeAvailable()

Implements Drupal\Core\Lock\LockBackedInterface::lockMayBeAvailable().

Overrides LockBackendInterface::lockMayBeAvailable

1 call to DatabaseLockBackend::lockMayBeAvailable()
DatabaseLockBackend::acquire in lib/Drupal/Core/Lock/DatabaseLockBackend.php
Implements Drupal\Core\Lock\LockBackedInterface::acquire().

File

lib/Drupal/Core/Lock/DatabaseLockBackend.php, line 97
Contains \Drupal\Core\Lock\DatabaseLockBackend.

Class

DatabaseLockBackend
Defines the database lock backend. This is the default backend in Drupal.

Namespace

Drupal\Core\Lock

Code

public function lockMayBeAvailable($name) {
  $lock = $this->database
    ->query('SELECT expire, value FROM {semaphore} WHERE name = :name', array(
    ':name' => $name,
  ))
    ->fetchAssoc();
  if (!$lock) {
    return TRUE;
  }
  $expire = (double) $lock['expire'];
  $now = microtime(TRUE);
  if ($now > $expire) {

    // We check two conditions to prevent a race condition where another
    // request acquired the lock and set a new expire time. We add a small
    // number to $expire to avoid errors with float to string conversion.
    return (bool) $this->database
      ->delete('semaphore')
      ->condition('name', $name)
      ->condition('value', $lock['value'])
      ->condition('expire', 0.0001 + $expire, '<=')
      ->execute();
  }
  return FALSE;
}