Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PidConsumerManager.php
Go to the documentation of this file.
1 <?php
7 
13 
18 {
24  const PID_FILE_EXT = '.pid';
25 
31  private $filesystem;
32 
36  public function __construct(Filesystem $filesystem)
37  {
38  $this->filesystem = $filesystem;
39  }
40 
48  public function isRun($pidFilePath)
49  {
50  $pid = $this->getPid($pidFilePath);
51  if ($pid) {
52  if (function_exists('posix_getpgid')) {
53  return (bool) posix_getpgid($pid);
54  } else {
55  return $this->checkIsProcessExists($pid);
56  }
57  }
58 
59  return false;
60  }
61 
73  private function checkIsProcessExists($pid)
74  {
75  if (!function_exists('exec')) {
76  throw new \RuntimeException('Function exec is not available');
77  }
78 
79  exec(escapeshellcmd('ps -p ' . $pid), $output, $code);
80 
81  $code = (int) $code;
82 
83  switch ($code) {
84  case 0:
85  return true;
86  break;
87  case 1:
88  return false;
89  break;
90  default:
91  throw new \RuntimeException('Exec returned non-zero code', $code);
92  break;
93  }
94  }
95 
103  public function getPid($pidFilePath)
104  {
106  $directory = $this->filesystem->getDirectoryRead(DirectoryList::VAR_DIR);
107 
108  if ($directory->isExist($pidFilePath)) {
109  return (int) $directory->readFile($pidFilePath);
110  }
111 
112  return 0;
113  }
114 
121  public function savePid($pidFilePath)
122  {
124  $directory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
125  $directory->writeFile($pidFilePath, function_exists('posix_getpid') ? posix_getpid() : getmypid(), 'w');
126  }
127 }
exec($command, array &$output=null, &$return_var=null)
$filesystem
$code
Definition: info.phtml:12