Skip to content

Handle exceptions thrown in child processes forked by ProcessManager #30626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions app/code/Magento/Indexer/Model/ProcessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace Magento\Indexer\Model;

use Magento\Framework\App\ObjectManager;
use Psr\Log\LoggerInterface;

/**
* Provide functionality for executing user functions in multi-thread mode.
*/
Expand All @@ -29,15 +32,22 @@ class ProcessManager
/** @var int|null */
private $threadsCount;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param \Magento\Framework\App\ResourceConnection $resource
* @param \Magento\Framework\Registry $registry
* @param int|null $threadsCount
* @param LoggerInterface|null $logger
*/
public function __construct(
\Magento\Framework\App\ResourceConnection $resource,
\Magento\Framework\Registry $registry = null,
int $threadsCount = null
int $threadsCount = null,
LoggerInterface $logger = null
) {
$this->resource = $resource;
if (null === $registry) {
Expand All @@ -47,6 +57,9 @@ public function __construct(
}
$this->registry = $registry;
$this->threadsCount = (int)$threadsCount;
$this->logger = $logger ?? ObjectManager::getInstance()->get(
LoggerInterface::class
);
}

/**
Expand Down Expand Up @@ -135,11 +148,20 @@ private function isSetupMode(): bool
*/
private function startChildProcess(callable $userFunction)
{
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$status = call_user_func($userFunction);
$status = is_int($status) ? $status : 0;
// phpcs:ignore Magento2.Security.LanguageConstruct.ExitUsage
exit($status);
try {
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$status = call_user_func($userFunction);
$status = is_int($status) ? $status : 0;
} catch (\Throwable $e) {
$status = 1;
$this->logger->error(
__('Child process failed with message: %1', $e->getMessage()),
['exception' => $e]
);
} finally {
// phpcs:ignore Magento2.Security.LanguageConstruct.ExitUsage
exit($status);
}
}

/**
Expand Down