Skip to content

Commit 5a5b6f7

Browse files
committed
Handle exceptions thrown in child processes forked by ProcessManager, instead of handling them in the parent process execution flow
1 parent a1e5689 commit 5a5b6f7

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

app/code/Magento/Indexer/Model/ProcessManager.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
namespace Magento\Indexer\Model;
99

10+
use Magento\Framework\App\ObjectManager;
11+
use Psr\Log\LoggerInterface;
12+
1013
/**
1114
* Provide functionality for executing user functions in multi-thread mode.
1215
*/
@@ -29,15 +32,22 @@ class ProcessManager
2932
/** @var int|null */
3033
private $threadsCount;
3134

35+
/**
36+
* @var LoggerInterface
37+
*/
38+
private $logger;
39+
3240
/**
3341
* @param \Magento\Framework\App\ResourceConnection $resource
3442
* @param \Magento\Framework\Registry $registry
3543
* @param int|null $threadsCount
44+
* @param LoggerInterface|null $logger
3645
*/
3746
public function __construct(
3847
\Magento\Framework\App\ResourceConnection $resource,
3948
\Magento\Framework\Registry $registry = null,
40-
int $threadsCount = null
49+
int $threadsCount = null,
50+
LoggerInterface $logger = null
4151
) {
4252
$this->resource = $resource;
4353
if (null === $registry) {
@@ -47,6 +57,9 @@ public function __construct(
4757
}
4858
$this->registry = $registry;
4959
$this->threadsCount = (int)$threadsCount;
60+
$this->logger = $logger ?? ObjectManager::getInstance()->get(
61+
LoggerInterface::class
62+
);
5063
}
5164

5265
/**
@@ -135,11 +148,20 @@ private function isSetupMode(): bool
135148
*/
136149
private function startChildProcess(callable $userFunction)
137150
{
138-
// phpcs:ignore Magento2.Functions.DiscouragedFunction
139-
$status = call_user_func($userFunction);
140-
$status = is_int($status) ? $status : 0;
141-
// phpcs:ignore Magento2.Security.LanguageConstruct.ExitUsage
142-
exit($status);
151+
try {
152+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
153+
$status = call_user_func($userFunction);
154+
$status = is_int($status) ? $status : 0;
155+
} catch (\Throwable $e) {
156+
$status = 1;
157+
$this->logger->error(
158+
__('Child process failed with message: %1', $e->getMessage()),
159+
['exception' => $e]
160+
);
161+
} finally {
162+
// phpcs:ignore Magento2.Security.LanguageConstruct.ExitUsage
163+
exit($status);
164+
}
143165
}
144166

145167
/**

0 commit comments

Comments
 (0)