Skip to content

Commit

Permalink
Add ::configureParallelExecutorFactory (#201)
Browse files Browse the repository at this point in the history
Closes #187
  • Loading branch information
theofidry authored Dec 11, 2022
1 parent 8814d54 commit 3428044
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 27 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ The library offers a wide variety of configuration settings:
- `::getParallelExecutableFactory()` allows you to completely configure the
`ParallelExecutorFactory` factory which goes from fragment, batch sizes, which
PHP executable is used or any of the [process handling hooks](#hooks)
- `::configureParallelExecutableFactory()` is a different, lighter extension
point to configure the `ParallelExecutorFactory` factory.
- `::getContainer()` allows you to configure which container is used. By default,
it passes the application's kernel's container if there is one. This is used
by the default error handler which resets the container in-between each item
Expand Down
43 changes: 34 additions & 9 deletions src/ParallelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,24 @@ final protected function execute(InputInterface $input, OutputInterface $output)
$commandName = $this->getName();
Assert::notNull($commandName);

return $this
->getParallelExecutableFactory(
fn (InputInterface $input) => $this->fetchItems($input, $output),
fn (string $item, InputInterface $input, OutputInterface $output) => $this->runSingleCommand($item, $input, $output),
Closure::fromCallable([$this, 'getItemName']),
$commandName,
$this->getDefinition(),
$this->createErrorHandler($input, $output),
)
$parallelExecutorFactory = $this->getParallelExecutableFactory(
fn (InputInterface $input) => $this->fetchItems($input, $output),
fn (
string $item,
InputInterface $input,
OutputInterface $output
) => $this->runSingleCommand($item, $input, $output),
Closure::fromCallable([$this, 'getItemName']),
$commandName,
$this->getDefinition(),
$this->createErrorHandler($input, $output),
);

return $this->configureParallelExecutableFactory(
$parallelExecutorFactory,
$input,
$output,
)
->build()
->execute(
$parallelizationInput,
Expand Down Expand Up @@ -125,6 +134,22 @@ protected function getParallelExecutableFactory(
);
}

/**
* This is an alternative to overriding ::getParallelExecutableFactory() which
* is a bit less convenient due to the number of parameters.
*/
protected function configureParallelExecutableFactory(
ParallelExecutorFactory $parallelExecutorFactory,
InputInterface $input,
OutputInterface $output
): ParallelExecutorFactory {
// This method will safely never contain anything. It is only a
// placeholder for the user to override so omitting
// parent::configureParallelExecutableFactory() is perfectly safe.

return $parallelExecutorFactory;
}

protected function createErrorHandler(InputInterface $input, OutputInterface $output): ErrorHandler
{
return new LoggingErrorHandler(
Expand Down
43 changes: 34 additions & 9 deletions src/Parallelization.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$parallelizationInput = ParallelizationInput::fromInput($input);

return $this
->getParallelExecutableFactory(
fn (InputInterface $input) => $this->fetchItems($input, $output),
fn (string $item, InputInterface $input, OutputInterface $output) => $this->runSingleCommand($item, $input, $output),
fn (int $count) => $this->getItemName($count),
$this->getName(),
$this->getDefinition(),
$this->createErrorHandler($input, $output),
)
$parallelExecutorFactory = $this->getParallelExecutableFactory(
fn (InputInterface $input) => $this->fetchItems($input, $output),
fn (
string $item,
InputInterface $input,
OutputInterface $output
) => $this->runSingleCommand($item, $input, $output),
fn (int $count) => $this->getItemName($count),
$this->getName(),
$this->getDefinition(),
$this->createErrorHandler($input, $output),
);

return $this->configureParallelExecutableFactory(
$parallelExecutorFactory,
$input,
$output,
)
->build()
->execute(
$parallelizationInput,
Expand Down Expand Up @@ -262,6 +271,22 @@ protected function getParallelExecutableFactory(
->withRunAfterBatch(Closure::fromCallable([$this, 'runAfterBatch']));
}

/**
* This is an alternative to overriding ::getParallelExecutableFactory() which
* is a bit less convenient due to the number of parameters.
*/
protected function configureParallelExecutableFactory(
ParallelExecutorFactory $parallelExecutorFactory,
InputInterface $input,
OutputInterface $output
): ParallelExecutorFactory {
// This method will safely never contain anything. It is only a
// placeholder for the user to override so omitting
// parent::configureParallelExecutableFactory() is perfectly safe.

return $parallelExecutorFactory;
}

protected function createErrorHandler(InputInterface $input, OutputInterface $output): ErrorHandler
{
$errorHandler = new ThrowableCodeErrorHandler(
Expand Down
20 changes: 11 additions & 9 deletions tests/Fixtures/Command/ImportUnknownMoviesCountCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Webmozarts\Console\Parallelization\ParallelCommand;
use Webmozarts\Console\Parallelization\ParallelExecutorFactory;
use function file_get_contents;
use function func_get_args;
use function json_decode;
use function realpath;
use const JSON_THROW_ON_ERROR;
Expand Down Expand Up @@ -64,16 +65,17 @@ protected function getParallelExecutableFactory(
InputDefinition $commandDefinition,
ErrorHandler $errorHandler
): ParallelExecutorFactory {
return ParallelExecutorFactory::create(
$fetchItems,
$runSingleCommand,
$getItemName,
$commandName,
$commandDefinition,
$errorHandler,
)
return parent::getParallelExecutableFactory(...func_get_args())
->withBatchSize(2)
->withSegmentSize(2)
->withSegmentSize(2);
}

protected function configureParallelExecutableFactory(
ParallelExecutorFactory $parallelExecutorFactory,
InputInterface $input,
OutputInterface $output
): ParallelExecutorFactory {
return $parallelExecutorFactory
->withRunBeforeFirstCommand(
fn () => $this->logger->recordFirstCommand(),
)
Expand Down

0 comments on commit 3428044

Please sign in to comment.