Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Nov 25, 2021
1 parent 38b1dc3 commit fd3533b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
36 changes: 34 additions & 2 deletions src/CliCommandMultiProc.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use BluePsyduck\SymfonyProcessManager\ProcessManager;
use JBZoo\Utils\Cli;
use JBZoo\Utils\Sys;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\Process;

Expand All @@ -39,6 +40,11 @@ abstract class CliCommandMultiProc extends CliCommand
*/
private $procPool = [];

/**
* @var ProgressBar|null
*/
private $progressBar;

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -79,6 +85,12 @@ protected function configure(): void
InputOption::VALUE_REQUIRED,
'Process Manager. Unique ID of process to execute one child proccess.',
''
)
->addOption(
'pm-no-progress',
null,
InputOption::VALUE_NONE,
'Process Manager. Show progress bar to additinal information.'
);

parent::configure();
Expand Down Expand Up @@ -138,13 +150,24 @@ protected function executeMultiProcessAction(): int
);

$procListIds = $this->getListOfChildIds();

if (!$this->getOptBool('pm-no-progress')) {
$this->progressBar = new ProgressBar($this->output, count($procListIds));
$this->progressBar->start();
}

foreach ($procListIds as $procListId) {
$childProcess = $this->createSubProcess($procListId);
$procManager->addProcess($childProcess);
}

$this->beforeStartAllProcesses();
$procManager->waitForAllProcesses();
if ($this->progressBar) {
$this->progressBar->finish();
$this->_('');
}

$this->afterFinishAllProcesses($this->procPool);

$errorList = $this->getErrorList();
Expand Down Expand Up @@ -187,6 +210,10 @@ private function initProcManager(
} elseif ($errorOutput) {
$this->procPool[$virtProcId]['err_out'] = $errorOutput;
}

if ($this->progressBar) {
$this->progressBar->advance();
}
};

return (new ProcessManager())
Expand Down Expand Up @@ -217,11 +244,16 @@ private function createSubProcess(string $procId): Process
return $optionValue !== false && $optionValue !== '';
});

unset($options['ansi']);
foreach (array_keys($options) as $optionKey) {
if (!$this->getDefinition()->getOption((string)$optionKey)->acceptValue()) {
$options[$optionKey] = null;
}
}

$options['pm-proc-id'] = $procId;
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->input->getArguments();
Expand Down
17 changes: 15 additions & 2 deletions src/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
*/
class ProgressBar
{
public const BREAK = '<yellow>Progress stopped</yellow>';
public const BREAK = '<yellow>Progress stopped</yellow>';

private const MAX_LINE_LENGTH = 80;

/**
Expand Down Expand Up @@ -210,7 +211,7 @@ public static function run(
/**
* @return bool
*/
public function execute(): bool
public function init(): bool
{
if ($this->max <= 0) {
$this->helper->_($this->title
Expand All @@ -227,6 +228,18 @@ public function execute(): bool
: "Number of steps: <blue>{$this->max}</blue>.");
}

return true;
}

/**
* @return bool
*/
public function execute(): bool
{
if (!$this->init()) {
return false;
}

$exceptionMessages = [];
$isSkipped = false;

Expand Down
11 changes: 7 additions & 4 deletions tests/Cli/CliMultiProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ class CliMultiProcessTest extends PHPUnit
public function testAsRealExecution()
{
$start = microtime(true);
$result = Helper::executeReal('test:sleep-multi 123 " qwerty "', ['sleep' => 1], 'JBZOO_TEST_VAR=123456');
$result = Helper::executeReal(
'test:sleep-multi 123 " qwerty "',
['sleep' => 1, 'pm-no-progress' => null],
'JBZOO_TEST_VAR=123456'
);

$time = microtime(true) - $start;

Expand Down Expand Up @@ -57,7 +61,7 @@ public function testAsRealExecution()
public function testAsVirtalExecution()
{
$start = microtime(true);
$result = Helper::executeVirtaul('test:sleep-multi', ['sleep' => 1]);
$result = Helper::executeVirtaul('test:sleep-multi', ['sleep' => 1, 'pm-no-progress' => null]);
$time = microtime(true) - $start;

$outputAsArray = json($result)->getArrayCopy();
Expand All @@ -84,7 +88,7 @@ public function testAsVirtalExecution()
public function testException()
{
$start = microtime(true);
$result = Helper::executeReal('test:sleep-multi 123 456 789', ['sleep' => 2]);
$result = Helper::executeReal('test:sleep-multi 123 456 789', ['sleep' => 2, 'pm-no-progress' => null]);
$time = microtime(true) - $start;

$outputAsArray = json($result[1])->getArrayCopy();
Expand All @@ -106,7 +110,6 @@ public function testException()
], $outputAsArray);
isContain('Exception messsage', $result[2]);


isTrue($time < 5, "Total time: {$time}");
}
}
13 changes: 9 additions & 4 deletions tests/fake-app/Commands/TestSleepMulti.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ protected function configure(): void
$this
->setName('test:sleep-multi')
->addOption('sleep', null, InputOption::VALUE_REQUIRED, 'Timeout in seconds to sleep the PHP script', 5)
->addOption('random-sleep', null, InputOption::VALUE_NONE, 'Random sleep timer (1-5)')
->addArgument('arg-1', InputArgument::OPTIONAL, 'Custom Agrument #1', 'QWERTY-1')
->addArgument('arg-2', InputArgument::OPTIONAL, 'Custom Agrument #2', 'QWERTY-2')
->addArgument('arg-3', InputArgument::OPTIONAL, 'Custom Agrument #3', 'QWERTY-3');
Expand All @@ -51,21 +52,25 @@ protected function configure(): void
*/
protected function executeOneProcessAction(string $pmThreadId): int
{
$sleep = $this->getOptString('sleep');
if ($sleep === '2' && $pmThreadId === '2') {
$sleep = $this->getOptInt('sleep');
if ($this->getOptBool('random-sleep')) {
$sleep = random_int(1, 5);
}

if ($sleep === 2 && $pmThreadId === '2') {
throw new Exception('Exception messsage');
}

$this->_([
"Started: {$pmThreadId}",
'Sleep : ' . $this->getOptString('sleep'),
'Sleep : ' . $sleep,
'Arg #1: ' . $this->input->getArgument('arg-1'),
'Arg #2: ' . $this->input->getArgument('arg-2'),
'Arg #3: ' . $this->input->getArgument('arg-3'),
'Env Var: ' . Env::string('JBZOO_TEST_VAR'),
]);

sleep($this->getOptInt('sleep'));
sleep($sleep);

$this->_("Finished: {$pmThreadId}");

Expand Down

0 comments on commit fd3533b

Please sign in to comment.