Skip to content

Commit

Permalink
feat: add option --json to save the output of parallel runs into a js…
Browse files Browse the repository at this point in the history
…on file
  • Loading branch information
stmh committed Sep 8, 2023
1 parent da90f0c commit 7bf1610
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
11 changes: 9 additions & 2 deletions src/Command/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ protected function configure()
'Run the command on a given set of blueprints simultaneously',
null
)
->addOption(
'json',
null,
InputOption::VALUE_OPTIONAL,
'Save the output as json into given file. (Works only when using variants)',
null
)
->addOption(
'set',
's',
Expand Down Expand Up @@ -370,7 +377,7 @@ private function handleVariants($variants, InputInterface $input, OutputInterfac
$cmd[] = '-v';
}

$cmd_lines[] = $cmd;
$cmd_lines[$v] = $cmd;

$rows[] = [$v, implode(' ', $cmd)];
}
Expand All @@ -381,7 +388,7 @@ private function handleVariants($variants, InputInterface $input, OutputInterfac
if ($input->getOption('force') !== false || $io->confirm('Do you want to run these commands? ', false)) {
$io->comment('Running ...');
$executor = new ParallelExecutor($cmd_lines, $output, $input->getOption('num-threads'));
return $executor->execute($input, $output);
return $executor->execute($input, $output, $input->getOption('json'));
}


Expand Down
16 changes: 14 additions & 2 deletions src/Utilities/ParallelExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public function __construct($command_lines, OutputInterface $output, $max_simult
$this->pool = new PriorityPool();
$this->pool->setMaxSimultaneous($max_simultaneous_processes);

foreach ($command_lines as $cmd) {
foreach ($command_lines as $identifier => $cmd) {
$this->add(new ParallelExecutorRun(
$identifier,
$cmd,
$output instanceof ConsoleOutput
? $output->section()
Expand All @@ -35,7 +36,7 @@ public function __construct($command_lines, OutputInterface $output, $max_simult
}
}

public function execute(InputInterface $input, OutputInterface $output)
public function execute(InputInterface $input, OutputInterface $output, ?string $save_as_json)
{

$progress_section = $output instanceof ConsoleOutput
Expand All @@ -60,9 +61,16 @@ public function execute(InputInterface $input, OutputInterface $output)
$progress->finish();
$style = new SymfonyStyle($input, $output);

$data = [];
foreach ($this->pool->getAll() as $run) {
if ($run instanceof ParallelExecutorRun) {
$style->section(sprintf('Results of `%s`', $run->getCommandLine()));
$data[$run->getIdentifier()] = [
'command' => $run->getCommandLine(),
'exit_code' => $run->getProcess()->getExitCode(),
'output' => $run->getProcess()->getOutput(),
'error_output' => $run->getProcess()->getErrorOutput(),
];
$style->writeln($run->getProcess()->getOutput());
$error = $run->getProcess()->getErrorOutput();
if (!empty($error)) {
Expand All @@ -72,6 +80,10 @@ public function execute(InputInterface $input, OutputInterface $output)
}
}

if ($save_as_json) {
file_put_contents($save_as_json, json_encode($data, JSON_PRETTY_PRINT));
}

return $this->pool->isSuccessful();
}

Expand Down
16 changes: 12 additions & 4 deletions src/Utilities/ParallelExecutorRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ class ParallelExecutorRun extends ProcessRun
private $output;
private $commandLine;

public function __construct($command_line, ConsoleSectionOutput $output = null)
protected $identifier;

public function __construct(string $identifier, $command_line, ConsoleSectionOutput $output = null)
{
$this->identifier = $identifier;
$this->output = $output;
$this->commandLine = implode(' ', $command_line);

Expand All @@ -28,7 +31,7 @@ public function __construct($command_line, ConsoleSectionOutput $output = null)
}
}

public function addListeners()
public function addListeners(): void
{
$this->writeln("<fg=blue>~ waiting</>");

Expand All @@ -52,13 +55,18 @@ function (RunEvent $event) {
);
}

public function writeln($message)
public function writeln($message): void
{
$this->output->overwrite($this->commandLine . ': ' . $message);
}

public function getCommandLine()
public function getCommandLine(): string
{
return $this->commandLine;
}

public function getIdentifier(): string
{
return $this->identifier;
}
}
10 changes: 6 additions & 4 deletions src/Utilities/PluginDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ public static function discoverFromFabfile(
$methods = $config->getMethodFactory();

foreach ($result as $plugin) {
$output->writeln(sprintf(
"<fg=Blue>Registering found plugin <fg=yellow>%s</> ...</>",
$plugin->getName()
));
if ($output->isVerbose()) {
$output->writeln(sprintf(
"<fg=Blue>Registering found plugin <fg=yellow>%s</> ...</>",
$plugin->getName()
));
}
foreach ($plugin->getMethods() as $class_name) {
$methods->addMethod(new $class_name($logger));
}
Expand Down

0 comments on commit 7bf1610

Please sign in to comment.