-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathWorkerCommand.php
More file actions
175 lines (148 loc) · 6.26 KB
/
WorkerCommand.php
File metadata and controls
175 lines (148 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare(strict_types=1);
namespace Rector\Console\Command;
use Clue\React\NDJson\Decoder;
use Clue\React\NDJson\Encoder;
use React\EventLoop\StreamSelectLoop;
use React\Socket\ConnectionInterface;
use React\Socket\TcpConnector;
use Rector\Application\ApplicationFileProcessor;
use Rector\Autoloading\AdditionalAutoloader;
use Rector\Configuration\ConfigurationFactory;
use Rector\Configuration\ConfigurationRuleFilter;
use Rector\Configuration\Option;
use Rector\Console\ProcessConfigureDecorator;
use Rector\Parallel\ValueObject\Bridge;
use Rector\StaticReflection\DynamicSourceLocatorDecorator;
use Rector\Util\MemoryLimiter;
use Rector\ValueObject\Configuration;
use Rector\ValueObject\Error\SystemError;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symplify\EasyParallel\Enum\Action;
use Symplify\EasyParallel\Enum\ReactCommand;
use Symplify\EasyParallel\Enum\ReactEvent;
use Throwable;
use Webmozart\Assert\Assert;
/**
* Inspired at: https://github.com/phpstan/phpstan-src/commit/9124c66dcc55a222e21b1717ba5f60771f7dda92
* https://github.com/phpstan/phpstan-src/blob/c471c7b050e0929daf432288770de673b394a983/src/Command/WorkerCommand.php
*
* ↓↓↓
* https://github.com/phpstan/phpstan-src/commit/b84acd2e3eadf66189a64fdbc6dd18ff76323f67#diff-7f625777f1ce5384046df08abffd6c911cfbb1cfc8fcb2bdeaf78f337689e3e2
*/
final class WorkerCommand extends Command
{
private const string RESULT = 'result';
public function __construct(
private readonly AdditionalAutoloader $additionalAutoloader,
private readonly DynamicSourceLocatorDecorator $dynamicSourceLocatorDecorator,
private readonly ApplicationFileProcessor $applicationFileProcessor,
private readonly MemoryLimiter $memoryLimiter,
private readonly ConfigurationFactory $configurationFactory,
private readonly ConfigurationRuleFilter $configurationRuleFilter,
) {
parent::__construct();
}
protected function configure(): void
{
$this->setName('worker');
$this->setDescription('[INTERNAL] Support for parallel process');
ProcessConfigureDecorator::decorate($this);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$configuration = $this->configurationFactory->createFromInput($input);
$this->memoryLimiter->adjust($configuration);
$this->configurationRuleFilter->setConfiguration($configuration);
$streamSelectLoop = new StreamSelectLoop();
$parallelIdentifier = $configuration->getParallelIdentifier();
$tcpConnector = new TcpConnector($streamSelectLoop);
$promise = $tcpConnector->connect('127.0.0.1:' . $configuration->getParallelPort());
$promise->then(function (ConnectionInterface $connection) use (
$parallelIdentifier,
$configuration,
$input,
$output
): void {
$inDecoder = new Decoder($connection, true, 512, JSON_INVALID_UTF8_IGNORE);
$outEncoder = new Encoder($connection, JSON_INVALID_UTF8_IGNORE);
$outEncoder->write([
ReactCommand::ACTION => Action::HELLO,
ReactCommand::IDENTIFIER => $parallelIdentifier,
]);
$this->runWorker($outEncoder, $inDecoder, $configuration, $input, $output);
});
$streamSelectLoop->run();
return self::SUCCESS;
}
private function runWorker(
Encoder $encoder,
Decoder $decoder,
Configuration $configuration,
InputInterface $input,
OutputInterface $output
): void {
$this->additionalAutoloader->autoloadPaths();
$this->dynamicSourceLocatorDecorator->addPaths($configuration->getPaths());
if ($configuration->isDebug()) {
$preFileCallback = static function (string $filePath) use ($output): void {
$output->writeln($filePath);
};
} else {
$preFileCallback = null;
}
// 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 (
$preFileCallback,
$encoder,
$configuration,
$input
): void {
$action = $json[ReactCommand::ACTION];
if ($action !== Action::MAIN) {
return;
}
/** @var string[] $filePaths */
$filePaths = $json[Bridge::FILES] ?? [];
Assert::notEmpty($filePaths);
$processResult = $this->applicationFileProcessor->processFiles(
$filePaths,
$configuration,
$preFileCallback
);
/**
* 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(
$input->getOption(Option::OUTPUT_FORMAT) !== 'json'
),
Bridge::FILES_COUNT => count($filePaths),
Bridge::SYSTEM_ERRORS => $processResult->getSystemErrors(),
Bridge::SYSTEM_ERRORS_COUNT => count($processResult->getSystemErrors()),
Bridge::TOTAL_CHANGED => $processResult->getTotalChanged(),
],
]);
});
$decoder->on(ReactEvent::ERROR, $handleErrorCallback);
}
}