1
0
mirror of synced 2024-11-25 14:26:08 +03:00
service-bundle/Messenger/MessageHandler/InNewProcessRunner.php

81 lines
2.4 KiB
PHP
Raw Normal View History

2021-03-31 11:00:48 +03:00
<?php
namespace RetailCrm\ServiceBundle\Messenger\MessageHandler;
use Psr\Log\LoggerInterface;
use RetailCrm\ServiceBundle\Messenger\CommandMessage;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
class InNewProcessRunner implements JobRunner
{
/** @var int Default timeout for process */
public const DEFAULT_TIMEOUT = 3600;
2022-07-20 14:38:42 +03:00
private LoggerInterface $logger;
2021-03-31 11:00:48 +03:00
2022-07-20 14:38:42 +03:00
private KernelInterface $kernel;
2021-03-31 11:00:48 +03:00
2022-07-20 14:38:42 +03:00
private int $timeout = self::DEFAULT_TIMEOUT;
2021-03-31 11:00:48 +03:00
public function __construct(
LoggerInterface $logger,
KernelInterface $kernel,
?int $timeout = null
) {
$this->logger = $logger;
$this->kernel = $kernel;
if (null !== $timeout) {
$this->timeout = $timeout;
}
}
public function run(CommandMessage $message): void
{
$phpBinaryPath = (new PhpExecutableFinder)->find();
$consoleCommand = [
'php' => $phpBinaryPath ?: 'php',
'console' => sprintf('%s/bin/console', $this->kernel->getContainer()->getParameter('kernel.project_dir')),
'command' => $message->getCommandName()
];
$process = new Process(
array_merge(
array_values($consoleCommand),
array_values($message->getArguments()),
array_values($this->getOptions($message)),
)
);
try {
$process
->setTimeout($this->timeout)
->run(static function(string $type, string $buffer) {
echo $buffer;
})
;
} catch (ProcessTimedOutException $processTimedOutException) {
$this->logger->error(
sprintf(
'Process "%s" killed after %d seconds of execution',
$processTimedOutException->getProcess()->getCommandLine(),
$processTimedOutException->getProcess()->getTimeout()
)
);
}
}
private function getOptions(CommandMessage $message): array
{
$options = [];
foreach ($message->getFormattedOptions() as $option => $value) {
$options[] = $option . '=' . $value;
}
return $options;
}
}