lockMayBeAvailable example


        public function acquire($name$timeout = 30.0)
        {
            return $this->lazyLoadItself()->acquire($name$timeout);
        }

        /** * {@inheritdoc} */
        public function lockMayBeAvailable($name)
        {
            return $this->lazyLoadItself()->lockMayBeAvailable($name);
        }

        /** * {@inheritdoc} */
        public function release($name)
        {
            return $this->lazyLoadItself()->release($name);
        }

        /** * {@inheritdoc} */

        public function acquire($name$timeout = 30.0)
        {
            return $this->lazyLoadItself()->acquire($name$timeout);
        }

        /** * {@inheritdoc} */
        public function lockMayBeAvailable($name)
        {
            return $this->lazyLoadItself()->lockMayBeAvailable($name);
        }

        /** * {@inheritdoc} */
        public function release($name)
        {
            return $this->lazyLoadItself()->release($name);
        }

        /** * {@inheritdoc} */
$this->setProcessedConfiguration($collection, 'rename', $rename_name);
    return TRUE;
  }

  /** * Determines if an import is already running. * * @return bool * TRUE if an import is already running, FALSE if not. */
  public function alreadyImporting() {
    return !$this->lock->lockMayBeAvailable(static::LOCK_NAME);
  }

  /** * Gets all the service dependencies from \Drupal. * * Since the ConfigImporter handles module installation the kernel and the * container can be rebuilt and altered during processing. It is necessary to * keep the services used by the importer in sync. */
  protected function reInjectMe() {
    // When rebuilding the container,


  /** * Tests backend release functionality. */
  public function testBackendLockRelease() {
    $success = $this->lock->acquire('lock_a');
    $this->assertTrue($success, 'Could acquire first lock.');

    // This function is not part of the backend, but the default database     // backend implement it, we can here use it safely.     $is_free = $this->lock->lockMayBeAvailable('lock_a');
    $this->assertFalse($is_free, 'First lock is unavailable.');

    $this->lock->release('lock_a');
    $is_free = $this->lock->lockMayBeAvailable('lock_a');
    $this->assertTrue($is_free, 'First lock has been released.');

    $success = $this->lock->acquire('lock_b');
    $this->assertTrue($success, 'Could acquire second lock.');

    $success = $this->lock->acquire('lock_b');
    $this->assertTrue($success, 'Could acquire second lock a second time within the same request.');

    

  public function transform(StorageInterface $storage) {
    // We use a database storage to reduce the memory requirement.     $mutable = new DatabaseStorage($this->connection, 'config_import');

    if (!$this->persistentLock->lockMayBeAvailable(ConfigImporter::LOCK_NAME)) {
      // If the config importer is already importing, the transformation will       // always be the one the config importer is already using. This makes sure       // that even if the storage changes the importer continues importing the       // same configuration.       return $mutable;
    }

    // Acquire a lock to ensure that the storage is not changed when a     // concurrent request tries to transform the storage. The lock will be     // released at the end of the request.     if (!$this->requestLock->acquire(self::LOCK_NAME)) {
      


  /** * Tests that the persistent lock is persisted between requests. */
  public function testPersistentLock() {
    $persistent_lock = $this->container->get('lock.persistent');
    // Get a persistent lock.     $this->drupalGet('system-test/lock-persist/lock1');
    $this->assertSession()->pageTextContains('TRUE: Lock successfully acquired in SystemTestController::lockPersist()');
    // Ensure that a shutdown function has not released the lock.     $this->assertFalse($persistent_lock->lockMayBeAvailable('lock1'));
    $this->drupalGet('system-test/lock-persist/lock1');
    $this->assertSession()->pageTextContains('FALSE: Lock not acquired in SystemTestController::lockPersist()');

    // Get another persistent lock.     $this->drupalGet('system-test/lock-persist/lock2');
    $this->assertSession()->pageTextContains('TRUE: Lock successfully acquired in SystemTestController::lockPersist()');
    $this->assertFalse($persistent_lock->lockMayBeAvailable('lock2'));

    // Release the first lock and try getting it again.     $persistent_lock->release('lock1');
    $this->drupalGet('system-test/lock-persist/lock1');
    
// We track all acquired locks in the global variable.           $this->locks[$name] = TRUE;
          // We never need to try again.           $retry = FALSE;
        }
        catch (IntegrityConstraintViolationException $e) {
          // Suppress the error. If this is our first pass through the loop,           // then $retry is FALSE. In this case, the insert failed because some           // other request acquired the lock but did not release it. We decide           // whether to retry by checking lockMayBeAvailable(). This will clear           // the offending row from the database table in case it has expired.           $retry = $retry ? FALSE : $this->lockMayBeAvailable($name);
        }
        catch (\Exception $e) {
          // Create the semaphore table if it does not exist and retry.           if ($this->ensureTableExists()) {
            // Retry only once.             $retry = !$retry;
          }
          else {
            throw $e;
          }
        }
        
// Begin sleeping at 25ms.     $sleep = 25000;
    while ($delay > 0) {
      // This function should only be called by a request that failed to get a       // lock, so we sleep first to give the parallel request a chance to finish       // and release the lock.       usleep($sleep);
      // After each sleep, increase the value of $sleep until it reaches       // 500ms, to reduce the potential for a lock stampede.       $delay = $delay - $sleep;
      $sleep = min(500000, $sleep + 25000, $delay);
      if ($this->lockMayBeAvailable($name)) {
        // No longer need to wait.         return FALSE;
      }
    }
    // The caller must still wait longer to get the lock.     return TRUE;
  }

  /** * {@inheritdoc} */
  
Home | Imprint | This part of the site doesn't use cookies.