-
Notifications
You must be signed in to change notification settings - Fork 0
/
CliCommandMultiProc.php
349 lines (294 loc) · 11.3 KB
/
CliCommandMultiProc.php
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* JBZoo Toolbox - Cli.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Cli
*/
declare(strict_types=1);
namespace JBZoo\Cli;
use BluePsyduck\SymfonyProcessManager\ProcessManager;
use JBZoo\Cli\ProgressBars\ProgressBarProcessManager;
use JBZoo\Utils\Cli as CliUtils;
use JBZoo\Utils\Sys;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\Process;
use function JBZoo\Utils\int;
abstract class CliCommandMultiProc extends CliCommand
{
private const PM_DEFAULT_INTERVAL = 100;
private const PM_DEFAULT_START_DELAY = 1;
private const PM_DEFAULT_TIMEOUT = 7200;
private array $procPool = [];
private ?ProgressBarProcessManager $progressBar = null;
abstract protected function executeOneProcess(string $pmThreadId): int;
/**
* @return string[]
*/
abstract protected function getListOfChildIds(): array;
/**
* {@inheritDoc}
*/
protected function configure(): void
{
$this
->addOption(
'pm-max',
null,
InputOption::VALUE_REQUIRED,
'Process Manager. The number of processes to execute in parallel (os isolated processes)',
'auto',
)
->addOption(
'pm-interval',
null,
InputOption::VALUE_REQUIRED,
'Process Manager. The interval to use for polling the processes, in milliseconds',
self::PM_DEFAULT_INTERVAL,
)
->addOption(
'pm-start-delay',
null,
InputOption::VALUE_REQUIRED,
'Process Manager. The time to delay the start of processes to space them out, in milliseconds',
self::PM_DEFAULT_START_DELAY,
)
->addOption(
'pm-max-timeout',
null,
InputOption::VALUE_REQUIRED,
'Process Manager. The max timeout for each proccess, in seconds',
self::PM_DEFAULT_TIMEOUT,
)
->addOption(
'pm-proc-id',
null,
InputOption::VALUE_REQUIRED,
'Process Manager. Unique ID of process to execute one child proccess.',
'',
);
parent::configure();
}
/**
* @phan-suppress PhanPluginPossiblyStaticProtectedMethod
*/
protected function beforeStartAllProcesses(): void
{
// noop
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @phan-suppress PhanPluginPossiblyStaticProtectedMethod
* @phan-suppress PhanUnusedProtectedNoOverrideMethodParameter
*/
protected function afterFinishAllProcesses(array $procPool): void
{
// noop
}
protected function executeAction(): int
{
$pmProcId = $this->getOptString('pm-proc-id');
if ($pmProcId !== '') {
return $this->executeOneProcess($pmProcId);
}
return $this->executeMultiProcessAction();
}
protected function executeMultiProcessAction(): int
{
$procNum = $this->getNumberOfProcesses();
$cpuCores = CliHelper::getNumberOfCpuCores();
$this->_("Max number of sub-processes: {$procNum}", OutLvl::DEBUG);
if ($procNum > $cpuCores) {
$this->_(
"The specified number of processes (--pm-max={$procNum}) "
. "is more than the found number of CPU cores in the system ({$cpuCores}).",
OutLvl::WARNING,
);
}
$procManager = $this->initProcManager($procNum, $this->gePmInterval(), $this->getPmStartDelay());
$procListIds = $this->getListOfChildIds();
if (!$this->outputMode->isProgressBarDisabled()) {
$this->progressBar = new ProgressBarProcessManager($this->outputMode);
$this->progressBar->setMax(\count($procListIds));
$this->progressBar->start();
}
foreach ($procListIds as $procListId) {
$childProcess = $this->createSubProcess($procListId);
$procManager->addProcess($childProcess);
}
$this->beforeStartAllProcesses();
$procManager->waitForAllProcesses();
if ($this->progressBar !== null) {
$this->progressBar->finish();
$this->_('');
}
$this->afterFinishAllProcesses($this->procPool);
$errorList = $this->getErrorList();
if (\count($errorList) > 0) {
throw new Exception(\implode("\n" . \str_repeat('-', 60) . "\n", $errorList));
}
$warningList = $this->getWarningList();
if (\count($warningList) > 0) {
$this->_(\implode("\n" . \str_repeat('-', 60) . "\n", $warningList), OutLvl::WARNING);
}
return 0;
}
private function initProcManager(
int $numberOfParallelProcesses,
int $pollInterval,
int $processStartDelay,
): ProcessManager {
$finishCallback = function (Process $process): void {
$virtProcId = \spl_object_id($process);
$exitCode = $process->getExitCode();
$errorOutput = \trim($process->getErrorOutput());
$stdOutput = \trim($process->getOutput());
$this->procPool[$virtProcId]['time_end'] = \microtime(true);
$this->procPool[$virtProcId]['exit_code'] = $exitCode;
$this->procPool[$virtProcId]['std_out'] = $stdOutput;
if ($exitCode > 0 || $errorOutput !== '') {
$this->procPool[$virtProcId]['err_out'] = $errorOutput;
}
if ($this->progressBar !== null) {
$this->progressBar->advance();
}
};
return (new ProcessManager())
->setPollInterval($pollInterval)
->setNumberOfParallelProcesses($numberOfParallelProcesses)
->setProcessStartDelay($processStartDelay)
->setProcessStartCallback(function (Process $process): void {
$virtProcId = \spl_object_id($process);
$this->procPool[$virtProcId]['time_start'] = \microtime(true);
})
->setProcessFinishCallback($finishCallback)
->setProcessTimeoutCallback(function (Process $process) use ($finishCallback): void {
$finishCallback($process);
$virtProcId = \spl_object_id($process);
$this->procPool[$virtProcId]['reached_timeout'] = true;
});
}
private function createSubProcess(string $procId): Process
{
// Prepare option list from the parent process
$options = \array_filter(
$this->outputMode->getInput()->getOptions(),
static fn ($optionValue): bool => $optionValue !== false && $optionValue !== '',
);
foreach (\array_keys($options) as $optionKey) {
if (!$this->getDefinition()->getOption((string)$optionKey)->acceptValue()) {
$options[$optionKey] = null;
}
}
unset($options['ansi']);
$options['no-ansi'] = null;
$options['no-interaction'] = null;
$options['pm-proc-id'] = $procId;
// Prepare $argument list from the parent process
$arguments = $this->outputMode->getInput()->getArguments();
$argumentsList = [];
foreach ($arguments as $argKey => $argValue) {
if (\is_array($argValue)) {
continue;
}
/** @var string $argValue */
if ($argKey !== 'command') {
/** @phan-suppress-next-line PhanPartialTypeMismatchArgumentInternal */
$argumentsList[] = '"' . \addcslashes($argValue, '"') . '"';
}
}
// Build full command line
$process = Process::fromShellCommandline(
CliUtils::build(
\implode(
' ',
\array_filter([
Sys::getBinary(),
CliHelper::getBinPath(),
$this->getName(),
\implode(' ', $argumentsList),
]),
),
$options,
),
CliHelper::getRootPath(),
null,
null,
$this->getMaxTimeout(),
);
$this->procPool[\spl_object_id($process)] = [
'command' => $process->getCommandLine(),
'proc_id' => $procId,
'exit_code' => null,
'std_out' => null,
'err_out' => null,
'reached_timeout' => false,
'time_start' => null,
'time_end' => null,
];
return $process;
}
private function getErrorList(): array
{
return \array_reduce($this->procPool, function (array $acc, array $procInfo): array {
if ($procInfo['reached_timeout']) {
$acc[] = \implode("\n", [
"Command : {$procInfo['command']}",
"Error : The process with ID \"{$procInfo['proc_id']}\""
. " exceeded the timeout of {$this->getMaxTimeout()} seconds.",
]);
} elseif ($procInfo['err_out'] && $procInfo['exit_code'] > 0) {
$acc[] = \implode("\n", [
"Command : {$procInfo['command']}",
"Code : {$procInfo['exit_code']}",
"Error : {$procInfo['err_out']}",
"StdOut : {$procInfo['std_out']}",
]);
}
return $acc;
}, []);
}
private function getWarningList(): array
{
return \array_reduce($this->procPool, static function (array $acc, array $procInfo): array {
if ($procInfo['err_out'] && $procInfo['exit_code'] === 0) {
$acc[] = \implode("\n", [
"Command : {$procInfo['command']}",
"Warning : {$procInfo['err_out']}",
"StdOut : {$procInfo['std_out']}",
]);
}
return $acc;
}, []);
}
private function getMaxTimeout(): int
{
$pmMaxTimeout = $this->getOptInt('pm-max-timeout');
return $pmMaxTimeout > 0 ? $pmMaxTimeout : self::PM_DEFAULT_TIMEOUT;
}
private function gePmInterval(): int
{
$pmInterval = $this->getOptInt('pm-interval');
return $pmInterval > 0 ? $pmInterval : self::PM_DEFAULT_INTERVAL;
}
private function getPmStartDelay(): int
{
$pmStartDelay = $this->getOptInt('pm-start-delay');
return $pmStartDelay > 0 ? $pmStartDelay : self::PM_DEFAULT_START_DELAY;
}
private function getNumberOfProcesses(): int
{
$pmMax = \strtolower($this->getOptString('pm-max'));
$cpuCores = CliHelper::getNumberOfCpuCores();
if ($pmMax === 'auto') {
return $cpuCores;
}
$pmMaxInt = \abs(int($pmMax));
return $pmMaxInt > 0 ? $pmMaxInt : $cpuCores;
}
}