Skip to content

Introduce the ParallelExecutorFactory #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 104 additions & 42 deletions src/ParallelExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,50 @@
use function array_merge;
use function array_slice;
use function implode;
use function mb_strlen;
use function sprintf;
use const STDIN;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
use function trim;
use Webmozart\Assert\Assert;
use Webmozarts\Console\Parallelization\ErrorHandler\ItemProcessingErrorHandler;
use Webmozarts\Console\Parallelization\Logger\Logger;

final class ParallelExecutor
{
private string $progressSymbol;
/**
* @var callable(InputInterface):list<string>
*/
private $fetchItems;

/**
* @var positive-int
* @var callable(string, InputInterface, OutputInterface):void
*/
private int $batchSize;
private $runSingleCommand;

/**
* @var callable(int): string
*/
private $getItemName;

private string $commandName;

private InputDefinition $commandDefinition;

private ItemProcessingErrorHandler $errorHandler;

/**
* @var positive-int
*/
private int $segmentSize;
private int $batchSize;

/**
* @var callable(InputInterface):list<string>
* @var positive-int
*/
private $fetchItems;
private int $segmentSize;

/**
* @var callable(InputInterface, OutputInterface):void
Expand All @@ -66,78 +83,72 @@ final class ParallelExecutor
*/
private $runAfterBatch;

/**
* @var callable(string, InputInterface, OutputInterface):void
*/
private $runSingleCommand;
private string $progressSymbol;

private string $scriptPath;
private string $phpExecutable;
private string $commandName;

private string $scriptPath;

private string $workingDirectory;

/**
* @var array<string, string>|null
*/
private ?array $extraEnvironmentVariables;

private InputDefinition $commandDefinition;

/**
* @var callable(int): string
*/
private $getItemName;

private ItemProcessingErrorHandler $errorHandler;

/**
* @param callable(InputInterface):list<string> $fetchItems
* @param callable(string, InputInterface, OutputInterface):void $runSingleCommand
* @param callable(int):string $getItemName
* @param positive-int $batchSize
* @param positive-int $segmentSize
* @param callable(InputInterface):list<string> $fetchItems
* @param callable(InputInterface, OutputInterface):void $runBeforeFirstCommand
* @param callable(InputInterface, OutputInterface):void $runAfterLastCommand
* @param callable(InputInterface, OutputInterface, list<string>):void $runBeforeBatch
* @param callable(InputInterface, OutputInterface, list<string>):void $runAfterBatch
* @param callable(string, InputInterface, OutputInterface):void $runSingleCommand
* @param callable(int):string $getItemName
* @param array<string, string> $extraEnvironmentVariables
*/
public function __construct(
string $progressSymbol,
callable $fetchItems,
callable $runSingleCommand,
callable $getItemName,
string $commandName,
InputDefinition $commandDefinition,
ItemProcessingErrorHandler $errorHandler,
int $batchSize,
int $segmentSize,
callable $fetchItems,
callable $runBeforeFirstCommand,
callable $runAfterLastCommand,
callable $runBeforeBatch,
callable $runAfterBatch,
callable $runSingleCommand,
callable $getItemName,
string $scriptPath,
string $progressSymbol,
string $phpExecutable,
string $commandName,
string $scriptPath,
string $workingDirectory,
?array $extraEnvironmentVariables,
InputDefinition $commandDefinition,
ItemProcessingErrorHandler $errorHandler
?array $extraEnvironmentVariables
) {
$this->progressSymbol = $progressSymbol;
$this->batchSize = $batchSize;
self::validateBatchSize($batchSize);
self::validateSegmentSize($segmentSize);
self::validateScriptPath($scriptPath);
self::validateProgressSymbol($progressSymbol);

$this->fetchItems = $fetchItems;
$this->runSingleCommand = $runSingleCommand;
$this->getItemName = $getItemName;
$this->commandName = $commandName;
$this->commandDefinition = $commandDefinition;
$this->errorHandler = $errorHandler;
$this->batchSize = $batchSize;
$this->segmentSize = $segmentSize;
$this->runBeforeFirstCommand = $runBeforeFirstCommand;
$this->runAfterLastCommand = $runAfterLastCommand;
$this->runBeforeBatch = $runBeforeBatch;
$this->runAfterBatch = $runAfterBatch;
$this->runSingleCommand = $runSingleCommand;
$this->segmentSize = $segmentSize;
$this->scriptPath = $scriptPath;
$this->progressSymbol = $progressSymbol;
$this->phpExecutable = $phpExecutable;
$this->commandName = $commandName;
$this->scriptPath = $scriptPath;
$this->workingDirectory = $workingDirectory;
$this->extraEnvironmentVariables = $extraEnvironmentVariables;
$this->commandDefinition = $commandDefinition;
$this->getItemName = $getItemName;
$this->errorHandler = $errorHandler;
}

public function execute(
Expand Down Expand Up @@ -340,4 +351,55 @@ private function processChildOutput(

$logger->advance($chars);
}

private static function validateBatchSize(int $batchSize): void
{
Assert::greaterThan(
$batchSize,
0,
sprintf(
'Expected the batch size to be 1 or greater. Got "%s".',
$batchSize,
),
);
}

private static function validateSegmentSize(int $segmentSize): void
{
Assert::greaterThan(
$segmentSize,
0,
sprintf(
'Expected the segment size to be 1 or greater. Got "%s".',
$segmentSize,
),
);
}

private static function validateScriptPath(string $scriptPath): void
{
Assert::fileExists(
$scriptPath,
sprintf(
'The script file could not be found at the path "%s" (working directory: %s)',
$scriptPath,
getcwd(),
),
);
}

private static function validateProgressSymbol(string $progressSymbol): void
{
$symbolLength = mb_strlen($progressSymbol);

Assert::same(
1,
$symbolLength,
sprintf(
'Expected the progress symbol length to be 1. Got "%s" for "%s".',
$symbolLength,
$progressSymbol,
),
);
}
}
Loading