function DatabaseLockBackend::lockMayBeAvailable

Same name in this branch
  1. 8.9.x core/lib/Drupal/Core/ProxyClass/Lock/DatabaseLockBackend.php \Drupal\Core\ProxyClass\Lock\DatabaseLockBackend::lockMayBeAvailable()
Same name in other branches
  1. 9 core/lib/Drupal/Core/ProxyClass/Lock/DatabaseLockBackend.php \Drupal\Core\ProxyClass\Lock\DatabaseLockBackend::lockMayBeAvailable()
  2. 9 core/lib/Drupal/Core/Lock/DatabaseLockBackend.php \Drupal\Core\Lock\DatabaseLockBackend::lockMayBeAvailable()
  3. 10 core/lib/Drupal/Core/ProxyClass/Lock/DatabaseLockBackend.php \Drupal\Core\ProxyClass\Lock\DatabaseLockBackend::lockMayBeAvailable()
  4. 10 core/lib/Drupal/Core/Lock/DatabaseLockBackend.php \Drupal\Core\Lock\DatabaseLockBackend::lockMayBeAvailable()
  5. 11.x core/lib/Drupal/Core/ProxyClass/Lock/DatabaseLockBackend.php \Drupal\Core\ProxyClass\Lock\DatabaseLockBackend::lockMayBeAvailable()
  6. 11.x core/lib/Drupal/Core/Lock/DatabaseLockBackend.php \Drupal\Core\Lock\DatabaseLockBackend::lockMayBeAvailable()

Overrides LockBackendInterface::lockMayBeAvailable

1 call to DatabaseLockBackend::lockMayBeAvailable()
DatabaseLockBackend::acquire in core/lib/Drupal/Core/Lock/DatabaseLockBackend.php
Acquires a lock.

File

core/lib/Drupal/Core/Lock/DatabaseLockBackend.php, line 111

Class

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

Namespace

Drupal\Core\Lock

Code

public function lockMayBeAvailable($name) {
    $name = $this->normalizeName($name);
    try {
        $lock = $this->database
            ->query('SELECT expire, value FROM {semaphore} WHERE name = :name', [
            ':name' => $name,
        ])
            ->fetchAssoc();
    } catch (\Exception $e) {
        $this->catchException($e);
        // If the table does not exist yet then the lock may be available.
        $lock = FALSE;
    }
    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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.