我有一个PHP守护程序,该守护程序在fork中运行Magento并打印一些PDF。 请在下面找到主要流程的代码:
class Generate_Pdf_New_Command extends Mage_Shell_Abstract
{
const SLEEP_TIME_SEC = 0.2;
const TIME_LIMIT = 600;
protected $_startTime;
protected $_childProcessList = [];
public function __construct()
{
$this->_parseArgs();
$this->_validate();
}
public function run()
{
$this->_startTime = time();
while (true) {
usleep(self::SLEEP_TIME_SEC * 1000);
$current = time();
if ($current - $this->_startTime >= self::TIME_LIMIT) {
exit(0);
}
$pid = pcntl_fork();
if ($pid === -1) {
$this->_printErr('Can\'t use forks.');
exit(0);
}
if ($pid) {
$this->_processMain($pid);
continue;
}
$this->_processChild();
exit(0);
}
}
protected function _processMain($pid)
{
$this->_childProcessList[$pid] = true;
if ($this->_canFork()) {
return;
}
$signPid = pcntl_wait($status);
if (isset($this->_childProcessList[$signPid])) {
unset($this->_childProcessList[$signPid]);
} else {
$this->_childProcessList = [];
}
}
protected function _processChild()
{
try {
(new Generate_Pdf)->run();
} catch (Throwable $e) {
$this->_printErr($e->getMessage());
}
}
protected function _canFork()
{
return count($this->_childProcessList) < $this->getArg('children');
}
protected function _printLn($msg)
{
fwrite(STDOUT, $msg.PHP_EOL);
}
protected function _printErr($msg)
{
fwrite(STDERR, $msg.PHP_EOL);
}
}
Generate_Pdf类扩展了Mage_Shell_Abstract,但没有覆盖构造函数,因此它运行Magento。 Mage_Shell_Abstract在主过程中仅用于处理args和验证。 总体来说,它可以正常工作,但有时我们在结果PDF中会遇到一些交叉。我对原因一无所知,如果有人有原因,我将不胜感激。