Updated Rector to commit 0c5ad104aee04d680816bbed6224470c7acfb895

0c5ad104ae [DX] Merge WorkerRunner to WorkerCommand, to make context closer (#4972)
This commit is contained in:
Tomas Votruba 2023-09-10 19:16:46 +00:00
parent 1a37c5b7e2
commit 4b84a2b441
8 changed files with 87 additions and 115 deletions

View File

@ -1,64 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\Parallel;
use RectorPrefix202309\Clue\React\NDJson\Decoder;
use RectorPrefix202309\Clue\React\NDJson\Encoder;
use Rector\Core\Application\ApplicationFileProcessor;
use Rector\Core\StaticReflection\DynamicSourceLocatorDecorator;
use Rector\Core\ValueObject\Configuration;
use Rector\Core\ValueObject\Error\SystemError;
use Rector\Parallel\ValueObject\Bridge;
use RectorPrefix202309\Symplify\EasyParallel\Enum\Action;
use RectorPrefix202309\Symplify\EasyParallel\Enum\ReactCommand;
use RectorPrefix202309\Symplify\EasyParallel\Enum\ReactEvent;
use Throwable;
final class WorkerRunner
{
/**
* @readonly
* @var \Rector\Core\StaticReflection\DynamicSourceLocatorDecorator
*/
private $dynamicSourceLocatorDecorator;
/**
* @readonly
* @var \Rector\Core\Application\ApplicationFileProcessor
*/
private $applicationFileProcessor;
/**
* @var string
*/
private const RESULT = 'result';
public function __construct(DynamicSourceLocatorDecorator $dynamicSourceLocatorDecorator, ApplicationFileProcessor $applicationFileProcessor)
{
$this->dynamicSourceLocatorDecorator = $dynamicSourceLocatorDecorator;
$this->applicationFileProcessor = $applicationFileProcessor;
}
public function run(Encoder $encoder, Decoder $decoder, Configuration $configuration) : void
{
$this->dynamicSourceLocatorDecorator->addPaths($configuration->getPaths());
// 1. handle system error
$handleErrorCallback = static function (Throwable $throwable) use($encoder) : void {
$systemError = new SystemError($throwable->getMessage(), $throwable->getFile(), $throwable->getLine());
$encoder->write([ReactCommand::ACTION => Action::RESULT, self::RESULT => [Bridge::SYSTEM_ERRORS => [$systemError], Bridge::FILES_COUNT => 0, Bridge::SYSTEM_ERRORS_COUNT => 1]]);
$encoder->end();
};
$encoder->on(ReactEvent::ERROR, $handleErrorCallback);
// 2. collect diffs + errors from file processor
$decoder->on(ReactEvent::DATA, function (array $json) use($encoder, $configuration) : void {
$action = $json[ReactCommand::ACTION];
if ($action !== Action::MAIN) {
return;
}
/** @var string[] $filePaths */
$filePaths = $json[Bridge::FILES] ?? [];
$processResult = $this->applicationFileProcessor->processFiles($filePaths, $configuration);
/**
* this invokes all listeners listening $decoder->on(...) @see \Symplify\EasyParallel\Enum\ReactEvent::DATA
*/
$encoder->write([ReactCommand::ACTION => Action::RESULT, self::RESULT => [Bridge::FILE_DIFFS => $processResult->getFileDiffs(), Bridge::FILES_COUNT => \count($filePaths), Bridge::SYSTEM_ERRORS => $processResult->getSystemErrors(), Bridge::SYSTEM_ERRORS_COUNT => \count($processResult->getSystemErrors()), Bridge::COLLECTED_DATA => $processResult->getCollectedData()]]);
});
$decoder->on(ReactEvent::ERROR, $handleErrorCallback);
}
}

View File

@ -101,10 +101,25 @@ final class ApplicationFileProcessor
return new ProcessResult([], [], []);
}
$this->configureCustomErrorHandler();
if ($configuration->isParallel()) {
$processResult = $this->runParallel($filePaths, $configuration, $input);
/**
* Mimic @see https://github.com/phpstan/phpstan-src/blob/ab154e1da54d42fec751e17a1199b3e07591e85e/src/Command/AnalyseApplication.php#L188C23-L244
*/
if ($configuration->shouldShowProgressBar()) {
$fileCount = \count($filePaths);
$this->symfonyStyle->progressStart($fileCount);
$this->symfonyStyle->progressAdvance(0);
$postFileCallback = function (int $stepCount) : void {
$this->symfonyStyle->progressAdvance($stepCount);
// running in parallel here → nothing else to do
};
} else {
$processResult = $this->processFiles($filePaths, $configuration, \false);
$postFileCallback = static function (int $stepCount) : void {
};
}
if ($configuration->isParallel()) {
$processResult = $this->runParallel($filePaths, $input, $postFileCallback);
} else {
$processResult = $this->processFiles($filePaths, $configuration, $postFileCallback);
}
$processResult->addSystemErrors($this->systemErrors);
$this->restoreErrorHandler();
@ -112,16 +127,10 @@ final class ApplicationFileProcessor
}
/**
* @param string[] $filePaths
* @param callable(int $fileCount): void|null $postFileCallback
*/
public function processFiles(array $filePaths, Configuration $configuration, bool $isParallel = \true) : ProcessResult
public function processFiles(array $filePaths, Configuration $configuration, ?callable $postFileCallback = null) : ProcessResult
{
$shouldShowProgressBar = $configuration->shouldShowProgressBar();
// progress bar on parallel handled on runParallel()
if (!$isParallel && $shouldShowProgressBar) {
$fileCount = \count($filePaths);
$this->symfonyStyle->progressStart($fileCount);
$this->symfonyStyle->progressAdvance(0);
}
/** @var SystemError[] $systemErrors */
$systemErrors = [];
/** @var FileDiff[] $fileDiffs */
@ -138,10 +147,9 @@ final class ApplicationFileProcessor
$fileDiffs[] = $currentFileDiff;
}
$collectedData = \array_merge($collectedData, $fileProcessResult->getCollectedData());
// progress bar +1,
// progress bar on parallel handled on runParallel()
if (!$isParallel && $shouldShowProgressBar) {
$this->symfonyStyle->progressAdvance();
if (\is_callable($postFileCallback)) {
$postFileCallback(1);
}
} catch (Throwable $throwable) {
$this->changedFilesDetector->invalidateFile($filePath);
@ -199,22 +207,11 @@ final class ApplicationFileProcessor
}
/**
* @param string[] $filePaths
* @param callable(int $stepCount): void $postFileCallback
*/
private function runParallel(array $filePaths, Configuration $configuration, InputInterface $input) : ProcessResult
private function runParallel(array $filePaths, InputInterface $input, callable $postFileCallback) : ProcessResult
{
$schedule = $this->scheduleFactory->create($this->cpuCoreCountProvider->provide(), SimpleParameterProvider::provideIntParameter(Option::PARALLEL_JOB_SIZE), SimpleParameterProvider::provideIntParameter(Option::PARALLEL_MAX_NUMBER_OF_PROCESSES), $filePaths);
if ($configuration->shouldShowProgressBar()) {
$fileCount = \count($filePaths);
$this->symfonyStyle->progressStart($fileCount);
$this->symfonyStyle->progressAdvance(0);
$postFileCallback = function (int $stepCount) : void {
$this->symfonyStyle->progressAdvance($stepCount);
// running in parallel here → nothing else to do
};
} else {
$postFileCallback = static function (int $stepCount) : void {
};
}
$mainScript = $this->resolveCalledRectorBinary();
if ($mainScript === null) {
throw new ParallelShouldNotHappenException('[parallel] Main script was not found');
@ -231,10 +228,10 @@ final class ApplicationFileProcessor
if (!isset($_SERVER[self::ARGV][0])) {
return null;
}
$potentialEcsBinaryPath = $_SERVER[self::ARGV][0];
if (!\file_exists($potentialEcsBinaryPath)) {
$potentialRectorBinaryPath = $_SERVER[self::ARGV][0];
if (!\file_exists($potentialRectorBinaryPath)) {
return null;
}
return $potentialEcsBinaryPath;
return $potentialRectorBinaryPath;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'e4eec89c47fb767dbadb65662c12e2e3b6650cd5';
public const PACKAGE_VERSION = '0c5ad104aee04d680816bbed6224470c7acfb895';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-09-11 01:59:13';
public const RELEASE_DATE = '2023-09-10 21:14:02';
/**
* @var int
*/

View File

@ -8,15 +8,21 @@ use RectorPrefix202309\Clue\React\NDJson\Encoder;
use RectorPrefix202309\React\EventLoop\StreamSelectLoop;
use RectorPrefix202309\React\Socket\ConnectionInterface;
use RectorPrefix202309\React\Socket\TcpConnector;
use Rector\Core\Application\ApplicationFileProcessor;
use Rector\Core\Configuration\ConfigurationFactory;
use Rector\Core\Console\ProcessConfigureDecorator;
use Rector\Core\StaticReflection\DynamicSourceLocatorDecorator;
use Rector\Core\Util\MemoryLimiter;
use Rector\Parallel\WorkerRunner;
use Rector\Core\ValueObject\Configuration;
use Rector\Core\ValueObject\Error\SystemError;
use Rector\Parallel\ValueObject\Bridge;
use RectorPrefix202309\Symfony\Component\Console\Command\Command;
use RectorPrefix202309\Symfony\Component\Console\Input\InputInterface;
use RectorPrefix202309\Symfony\Component\Console\Output\OutputInterface;
use RectorPrefix202309\Symplify\EasyParallel\Enum\Action;
use RectorPrefix202309\Symplify\EasyParallel\Enum\ReactCommand;
use RectorPrefix202309\Symplify\EasyParallel\Enum\ReactEvent;
use Throwable;
/**
* Inspired at: https://github.com/phpstan/phpstan-src/commit/9124c66dcc55a222e21b1717ba5f60771f7dda92
* https://github.com/phpstan/phpstan-src/blob/c471c7b050e0929daf432288770de673b394a983/src/Command/WorkerCommand.php
@ -28,9 +34,14 @@ final class WorkerCommand extends Command
{
/**
* @readonly
* @var \Rector\Parallel\WorkerRunner
* @var \Rector\Core\StaticReflection\DynamicSourceLocatorDecorator
*/
private $workerRunner;
private $dynamicSourceLocatorDecorator;
/**
* @readonly
* @var \Rector\Core\Application\ApplicationFileProcessor
*/
private $applicationFileProcessor;
/**
* @readonly
* @var \Rector\Core\Util\MemoryLimiter
@ -41,9 +52,14 @@ final class WorkerCommand extends Command
* @var \Rector\Core\Configuration\ConfigurationFactory
*/
private $configurationFactory;
public function __construct(WorkerRunner $workerRunner, MemoryLimiter $memoryLimiter, ConfigurationFactory $configurationFactory)
/**
* @var string
*/
private const RESULT = 'result';
public function __construct(DynamicSourceLocatorDecorator $dynamicSourceLocatorDecorator, ApplicationFileProcessor $applicationFileProcessor, MemoryLimiter $memoryLimiter, ConfigurationFactory $configurationFactory)
{
$this->workerRunner = $workerRunner;
$this->dynamicSourceLocatorDecorator = $dynamicSourceLocatorDecorator;
$this->applicationFileProcessor = $applicationFileProcessor;
$this->memoryLimiter = $memoryLimiter;
$this->configurationFactory = $configurationFactory;
parent::__construct();
@ -66,11 +82,36 @@ final class WorkerCommand extends Command
$promise->then(function (ConnectionInterface $connection) use($parallelIdentifier, $configuration) : void {
$inDecoder = new Decoder($connection, \true, 512, \JSON_INVALID_UTF8_IGNORE);
$outEncoder = new Encoder($connection, \JSON_INVALID_UTF8_IGNORE);
// handshake?
$outEncoder->write([ReactCommand::ACTION => Action::HELLO, ReactCommand::IDENTIFIER => $parallelIdentifier]);
$this->workerRunner->run($outEncoder, $inDecoder, $configuration);
$this->runWorker($outEncoder, $inDecoder, $configuration);
});
$streamSelectLoop->run();
return self::SUCCESS;
}
private function runWorker(Encoder $encoder, Decoder $decoder, Configuration $configuration) : void
{
$this->dynamicSourceLocatorDecorator->addPaths($configuration->getPaths());
// 1. handle system error
$handleErrorCallback = static function (Throwable $throwable) use($encoder) : void {
$systemError = new SystemError($throwable->getMessage(), $throwable->getFile(), $throwable->getLine());
$encoder->write([ReactCommand::ACTION => Action::RESULT, self::RESULT => [Bridge::SYSTEM_ERRORS => [$systemError], Bridge::FILES_COUNT => 0, Bridge::SYSTEM_ERRORS_COUNT => 1]]);
$encoder->end();
};
$encoder->on(ReactEvent::ERROR, $handleErrorCallback);
// 2. collect diffs + errors from file processor
$decoder->on(ReactEvent::DATA, function (array $json) use($encoder, $configuration) : void {
$action = $json[ReactCommand::ACTION];
if ($action !== Action::MAIN) {
return;
}
/** @var string[] $filePaths */
$filePaths = $json[Bridge::FILES] ?? [];
$processResult = $this->applicationFileProcessor->processFiles($filePaths, $configuration);
/**
* this invokes all listeners listening $decoder->on(...) @see \Symplify\EasyParallel\Enum\ReactEvent::DATA
*/
$encoder->write([ReactCommand::ACTION => Action::RESULT, self::RESULT => [Bridge::FILE_DIFFS => $processResult->getFileDiffs(), Bridge::FILES_COUNT => \count($filePaths), Bridge::SYSTEM_ERRORS => $processResult->getSystemErrors(), Bridge::SYSTEM_ERRORS_COUNT => \count($processResult->getSystemErrors()), Bridge::COLLECTED_DATA => $processResult->getCollectedData()]]);
});
$decoder->on(ReactEvent::ERROR, $handleErrorCallback);
}
}

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit0e72cd77db4f8f5a6ba9bdc34539aa26::getLoader();
return ComposerAutoloaderInit5034a22d527007e796e7effa1686f173::getLoader();

View File

@ -1746,7 +1746,6 @@ return array(
'Rector\\Parallel\\Command\\WorkerCommandLineFactory' => $baseDir . '/packages/Parallel/Command/WorkerCommandLineFactory.php',
'Rector\\Parallel\\ValueObject\\Bridge' => $baseDir . '/packages/Parallel/ValueObject/Bridge.php',
'Rector\\Parallel\\ValueObject\\Name' => $baseDir . '/packages/Parallel/ValueObject/Name.php',
'Rector\\Parallel\\WorkerRunner' => $baseDir . '/packages/Parallel/WorkerRunner.php',
'Rector\\Php52\\Rector\\Property\\VarToPublicPropertyRector' => $baseDir . '/rules/Php52/Rector/Property/VarToPublicPropertyRector.php',
'Rector\\Php52\\Rector\\Switch_\\ContinueToBreakInSwitchRector' => $baseDir . '/rules/Php52/Rector/Switch_/ContinueToBreakInSwitchRector.php',
'Rector\\Php53\\Rector\\FuncCall\\DirNameFileConstantToDirConstantRector' => $baseDir . '/rules/Php53/Rector/FuncCall/DirNameFileConstantToDirConstantRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit0e72cd77db4f8f5a6ba9bdc34539aa26
class ComposerAutoloaderInit5034a22d527007e796e7effa1686f173
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit0e72cd77db4f8f5a6ba9bdc34539aa26
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit0e72cd77db4f8f5a6ba9bdc34539aa26', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit5034a22d527007e796e7effa1686f173', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit0e72cd77db4f8f5a6ba9bdc34539aa26', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit5034a22d527007e796e7effa1686f173', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit0e72cd77db4f8f5a6ba9bdc34539aa26::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit5034a22d527007e796e7effa1686f173::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit0e72cd77db4f8f5a6ba9bdc34539aa26::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit5034a22d527007e796e7effa1686f173::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit0e72cd77db4f8f5a6ba9bdc34539aa26
class ComposerStaticInit5034a22d527007e796e7effa1686f173
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -1966,7 +1966,6 @@ class ComposerStaticInit0e72cd77db4f8f5a6ba9bdc34539aa26
'Rector\\Parallel\\Command\\WorkerCommandLineFactory' => __DIR__ . '/../..' . '/packages/Parallel/Command/WorkerCommandLineFactory.php',
'Rector\\Parallel\\ValueObject\\Bridge' => __DIR__ . '/../..' . '/packages/Parallel/ValueObject/Bridge.php',
'Rector\\Parallel\\ValueObject\\Name' => __DIR__ . '/../..' . '/packages/Parallel/ValueObject/Name.php',
'Rector\\Parallel\\WorkerRunner' => __DIR__ . '/../..' . '/packages/Parallel/WorkerRunner.php',
'Rector\\Php52\\Rector\\Property\\VarToPublicPropertyRector' => __DIR__ . '/../..' . '/rules/Php52/Rector/Property/VarToPublicPropertyRector.php',
'Rector\\Php52\\Rector\\Switch_\\ContinueToBreakInSwitchRector' => __DIR__ . '/../..' . '/rules/Php52/Rector/Switch_/ContinueToBreakInSwitchRector.php',
'Rector\\Php53\\Rector\\FuncCall\\DirNameFileConstantToDirConstantRector' => __DIR__ . '/../..' . '/rules/Php53/Rector/FuncCall/DirNameFileConstantToDirConstantRector.php',
@ -2599,9 +2598,9 @@ class ComposerStaticInit0e72cd77db4f8f5a6ba9bdc34539aa26
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit0e72cd77db4f8f5a6ba9bdc34539aa26::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit0e72cd77db4f8f5a6ba9bdc34539aa26::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit0e72cd77db4f8f5a6ba9bdc34539aa26::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit5034a22d527007e796e7effa1686f173::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit5034a22d527007e796e7effa1686f173::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit5034a22d527007e796e7effa1686f173::$classMap;
}, null, ClassLoader::class);
}