6 declare(strict_types=1);
21 private $failInChildProcess =
false;
30 private $threadsCount;
38 \
Magento\Framework\
App\ResourceConnection $resource,
39 \
Magento\Framework\Registry $registry =
null,
40 int $threadsCount =
null 43 if (
null === $registry) {
45 \
Magento\Framework\Registry::class
48 $this->registry = $registry;
49 $this->threadsCount = (int)$threadsCount;
59 if ($this->threadsCount > 1 && $this->isCanBeParalleled() && !$this->isSetupMode() && PHP_SAPI ==
'cli') {
60 $this->multiThreadsExecute($userFunctions);
62 $this->simpleThreadExecute($userFunctions);
71 private function simpleThreadExecute($userFunctions)
73 foreach ($userFunctions as $userFunction) {
84 private function multiThreadsExecute($userFunctions)
86 $this->resource->closeConnection(
null);
88 foreach ($userFunctions as $userFunction) {
91 throw new \RuntimeException(
'Unable to fork a new process');
93 $this->executeParentProcess($threadNumber);
95 $this->startChildProcess($userFunction);
98 while (pcntl_waitpid(0,
$status) != -1) {
102 if ($this->failInChildProcess) {
103 throw new \RuntimeException(
'Fail in child process');
112 private function isCanBeParalleled(): bool
122 private function isSetupMode(): bool
124 return $this->registry->registry(
'setup-mode-enabled') ?:
false;
133 private function startChildProcess(callable $userFunction)
145 private function executeParentProcess(
int &$threadNumber)
148 if ($threadNumber >= $this->threadsCount) {
150 if (pcntl_wexitstatus(
$status) !== 0) {
151 $this->failInChildProcess =
true;
elseif(isset( $params[ 'redirect_parent']))
__construct(\Magento\Framework\App\ResourceConnection $resource, \Magento\Framework\Registry $registry=null, int $threadsCount=null)
call_user_func($callable, $param)