Skip to content

Commit

Permalink
Handle exceptions thrown in child processes forked by ProcessManager,…
Browse files Browse the repository at this point in the history
… instead of handling them in the parent process execution flow
  • Loading branch information
pawel-siejba committed Nov 16, 2020
1 parent a1e5689 commit 5a5b6f7
Showing 1 changed file with 28 additions and 6 deletions.
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

0 comments on commit 5a5b6f7

Please sign in to comment.