filesystem = $filesystem; } /** * @inheritdoc */ public function lockProcess($lockName) { if ($this->getState()->getMode() == State::MODE_PRODUCTION) { return; } $this->tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR); $this->lockFilePath = $this->getFilePath($lockName); while ($this->isProcessLocked()) { usleep(1000); } $this->tmpDirectory->writeFile($this->lockFilePath, time()); } /** * @inheritdoc * @throws FileSystemException */ public function unlockProcess() { if ($this->getState()->getMode() == State::MODE_PRODUCTION) { return; } $this->tmpDirectory->delete($this->lockFilePath); } /** * Check whether generation process has already locked * * @return bool */ private function isProcessLocked() { if ($this->tmpDirectory->isExist($this->lockFilePath)) { try { $lockTime = (int)$this->tmpDirectory->readFile($this->lockFilePath); if ((time() - $lockTime) >= self::MAX_LOCK_TIME) { $this->tmpDirectory->delete($this->lockFilePath); return false; } } catch (FileSystemException $e) { return false; } return true; } return false; } /** * Get name of lock file * * @param string $name * @return string */ private function getFilePath($name) { return DirectoryList::TMP . DIRECTORY_SEPARATOR . $name . self::LOCK_EXTENSION; } /** * @return State * @deprecated 100.1.1 */ private function getState() { if (null === $this->state) { $this->state = ObjectManager::getInstance()->get(State::class); } return $this->state; } }