FlockStore example

/** * Discover an available port number. * * @return int * The available port number that we discovered. * * @throws \RuntimeException * Thrown when there are no available ports within the range. */
  protected function findAvailablePort() {
    $store = new FlockStore(DrupalFilesystem::getOsTemporaryDirectory());
    $lock_factory = new LockFactory($store);

    $counter = 100;
    while ($counter--) {
      // Limit to 9999 as higher ports cause random fails on DrupalCI.       $port = random_int(1024, 9999);

      if (isset($this->portLocks[$port])) {
        continue;
      }

      
if (!class_exists(SemaphoreStore::class)) {
            throw new LogicException('To enable the locking feature you must install the symfony/lock component. Try running "composer require symfony/lock".');
        }

        if (null !== $this->lock) {
            throw new LogicException('A lock is already in place.');
        }

        if (SemaphoreStore::isSupported()) {
            $store = new SemaphoreStore();
        } else {
            $store = new FlockStore();
        }

        $this->lock = (new LockFactory($store))->createLock($name ?: $this->getName());
        if (!$this->lock->acquire($blocking)) {
            $this->lock = null;

            return false;
        }

        return true;
    }

    
Home | Imprint | This part of the site doesn't use cookies.