Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LockerProcess.php
Go to the documentation of this file.
1 <?php
7 
14 
19 {
23  const LOCK_EXTENSION = '.lock';
24 
28  const MAX_LOCK_TIME = 30;
29 
33  private $filesystem;
34 
38  private $lockFilePath;
39 
43  private $tmpDirectory;
44 
48  private $state;
49 
55  public function __construct(Filesystem $filesystem)
56  {
57  $this->filesystem = $filesystem;
58  }
59 
63  public function lockProcess($lockName)
64  {
65  if ($this->getState()->getMode() == State::MODE_PRODUCTION) {
66  return;
67  }
68 
69  $this->tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
70  $this->lockFilePath = $this->getFilePath($lockName);
71 
72  while ($this->isProcessLocked()) {
73  usleep(1000);
74  }
75 
76  $this->tmpDirectory->writeFile($this->lockFilePath, time());
77  }
78 
83  public function unlockProcess()
84  {
85  if ($this->getState()->getMode() == State::MODE_PRODUCTION) {
86  return;
87  }
88 
89  $this->tmpDirectory->delete($this->lockFilePath);
90  }
91 
97  private function isProcessLocked()
98  {
99  if ($this->tmpDirectory->isExist($this->lockFilePath)) {
100  try {
101  $lockTime = (int)$this->tmpDirectory->readFile($this->lockFilePath);
102  if ((time() - $lockTime) >= self::MAX_LOCK_TIME) {
103  $this->tmpDirectory->delete($this->lockFilePath);
104 
105  return false;
106  }
107  } catch (FileSystemException $e) {
108  return false;
109  }
110 
111  return true;
112  }
113 
114  return false;
115  }
116 
123  private function getFilePath($name)
124  {
125  return DirectoryList::TMP . DIRECTORY_SEPARATOR . $name . self::LOCK_EXTENSION;
126  }
127 
132  private function getState()
133  {
134  if (null === $this->state) {
135  $this->state = ObjectManager::getInstance()->get(State::class);
136  }
137  return $this->state;
138  }
139 }
$filesystem
if(!isset($_GET['name'])) $name
Definition: log.php:14