Skip to content

Commit 3428044

Browse files
authored
Add ::configureParallelExecutorFactory (#201)
Closes #187
1 parent 8814d54 commit 3428044

File tree

4 files changed

+81
-27
lines changed

4 files changed

+81
-27
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ The library offers a wide variety of configuration settings:
198198
- `::getParallelExecutableFactory()` allows you to completely configure the
199199
`ParallelExecutorFactory` factory which goes from fragment, batch sizes, which
200200
PHP executable is used or any of the [process handling hooks](#hooks)
201+
- `::configureParallelExecutableFactory()` is a different, lighter extension
202+
point to configure the `ParallelExecutorFactory` factory.
201203
- `::getContainer()` allows you to configure which container is used. By default,
202204
it passes the application's kernel's container if there is one. This is used
203205
by the default error handler which resets the container in-between each item

src/ParallelCommand.php

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,24 @@ final protected function execute(InputInterface $input, OutputInterface $output)
8484
$commandName = $this->getName();
8585
Assert::notNull($commandName);
8686

87-
return $this
88-
->getParallelExecutableFactory(
89-
fn (InputInterface $input) => $this->fetchItems($input, $output),
90-
fn (string $item, InputInterface $input, OutputInterface $output) => $this->runSingleCommand($item, $input, $output),
91-
Closure::fromCallable([$this, 'getItemName']),
92-
$commandName,
93-
$this->getDefinition(),
94-
$this->createErrorHandler($input, $output),
95-
)
87+
$parallelExecutorFactory = $this->getParallelExecutableFactory(
88+
fn (InputInterface $input) => $this->fetchItems($input, $output),
89+
fn (
90+
string $item,
91+
InputInterface $input,
92+
OutputInterface $output
93+
) => $this->runSingleCommand($item, $input, $output),
94+
Closure::fromCallable([$this, 'getItemName']),
95+
$commandName,
96+
$this->getDefinition(),
97+
$this->createErrorHandler($input, $output),
98+
);
99+
100+
return $this->configureParallelExecutableFactory(
101+
$parallelExecutorFactory,
102+
$input,
103+
$output,
104+
)
96105
->build()
97106
->execute(
98107
$parallelizationInput,
@@ -125,6 +134,22 @@ protected function getParallelExecutableFactory(
125134
);
126135
}
127136

137+
/**
138+
* This is an alternative to overriding ::getParallelExecutableFactory() which
139+
* is a bit less convenient due to the number of parameters.
140+
*/
141+
protected function configureParallelExecutableFactory(
142+
ParallelExecutorFactory $parallelExecutorFactory,
143+
InputInterface $input,
144+
OutputInterface $output
145+
): ParallelExecutorFactory {
146+
// This method will safely never contain anything. It is only a
147+
// placeholder for the user to override so omitting
148+
// parent::configureParallelExecutableFactory() is perfectly safe.
149+
150+
return $parallelExecutorFactory;
151+
}
152+
128153
protected function createErrorHandler(InputInterface $input, OutputInterface $output): ErrorHandler
129154
{
130155
return new LoggingErrorHandler(

src/Parallelization.php

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
125125
{
126126
$parallelizationInput = ParallelizationInput::fromInput($input);
127127

128-
return $this
129-
->getParallelExecutableFactory(
130-
fn (InputInterface $input) => $this->fetchItems($input, $output),
131-
fn (string $item, InputInterface $input, OutputInterface $output) => $this->runSingleCommand($item, $input, $output),
132-
fn (int $count) => $this->getItemName($count),
133-
$this->getName(),
134-
$this->getDefinition(),
135-
$this->createErrorHandler($input, $output),
136-
)
128+
$parallelExecutorFactory = $this->getParallelExecutableFactory(
129+
fn (InputInterface $input) => $this->fetchItems($input, $output),
130+
fn (
131+
string $item,
132+
InputInterface $input,
133+
OutputInterface $output
134+
) => $this->runSingleCommand($item, $input, $output),
135+
fn (int $count) => $this->getItemName($count),
136+
$this->getName(),
137+
$this->getDefinition(),
138+
$this->createErrorHandler($input, $output),
139+
);
140+
141+
return $this->configureParallelExecutableFactory(
142+
$parallelExecutorFactory,
143+
$input,
144+
$output,
145+
)
137146
->build()
138147
->execute(
139148
$parallelizationInput,
@@ -262,6 +271,22 @@ protected function getParallelExecutableFactory(
262271
->withRunAfterBatch(Closure::fromCallable([$this, 'runAfterBatch']));
263272
}
264273

274+
/**
275+
* This is an alternative to overriding ::getParallelExecutableFactory() which
276+
* is a bit less convenient due to the number of parameters.
277+
*/
278+
protected function configureParallelExecutableFactory(
279+
ParallelExecutorFactory $parallelExecutorFactory,
280+
InputInterface $input,
281+
OutputInterface $output
282+
): ParallelExecutorFactory {
283+
// This method will safely never contain anything. It is only a
284+
// placeholder for the user to override so omitting
285+
// parent::configureParallelExecutableFactory() is perfectly safe.
286+
287+
return $parallelExecutorFactory;
288+
}
289+
265290
protected function createErrorHandler(InputInterface $input, OutputInterface $output): ErrorHandler
266291
{
267292
$errorHandler = new ThrowableCodeErrorHandler(

tests/Fixtures/Command/ImportUnknownMoviesCountCommand.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Webmozarts\Console\Parallelization\ParallelCommand;
2626
use Webmozarts\Console\Parallelization\ParallelExecutorFactory;
2727
use function file_get_contents;
28+
use function func_get_args;
2829
use function json_decode;
2930
use function realpath;
3031
use const JSON_THROW_ON_ERROR;
@@ -64,16 +65,17 @@ protected function getParallelExecutableFactory(
6465
InputDefinition $commandDefinition,
6566
ErrorHandler $errorHandler
6667
): ParallelExecutorFactory {
67-
return ParallelExecutorFactory::create(
68-
$fetchItems,
69-
$runSingleCommand,
70-
$getItemName,
71-
$commandName,
72-
$commandDefinition,
73-
$errorHandler,
74-
)
68+
return parent::getParallelExecutableFactory(...func_get_args())
7569
->withBatchSize(2)
76-
->withSegmentSize(2)
70+
->withSegmentSize(2);
71+
}
72+
73+
protected function configureParallelExecutableFactory(
74+
ParallelExecutorFactory $parallelExecutorFactory,
75+
InputInterface $input,
76+
OutputInterface $output
77+
): ParallelExecutorFactory {
78+
return $parallelExecutorFactory
7779
->withRunBeforeFirstCommand(
7880
fn () => $this->logger->recordFirstCommand(),
7981
)

0 commit comments

Comments
 (0)