use properties over referecnes

This commit is contained in:
TomasVotruba 2020-03-29 16:26:45 +02:00
parent 69436fc827
commit 2517480b5a
7 changed files with 72 additions and 74 deletions

View File

@ -1,4 +1,4 @@
name: Check Fixtures
name: Validate Fixtures
on:
pull_request: null

View File

@ -1,4 +1,4 @@
name: Check Services
name: Validate Services
on:
pull_request: null
@ -7,7 +7,7 @@ on:
- master
jobs:
check_services:
validate_services:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
@ -16,4 +16,4 @@ jobs:
php-version: 7.2
coverage: none # disable xdebug, pcov
- run: composer install --no-progress
- run: bin/console validate-services-in-sets --ansi
- run: bin/rector validate-services-in-sets --ansi

View File

@ -1,4 +1,4 @@
name: Check Sets
name: Validate Sets
on:
pull_request: null
@ -7,7 +7,7 @@ on:
- master
jobs:
check_sets:
validate_sets:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

View File

@ -24,7 +24,6 @@ services:
parameters:
paths:
- "ci"
- "bin"
- "src"
- "packages"

View File

@ -16,7 +16,6 @@ parameters:
- compiler/src
paths:
- ci
- bin
- src
- rules

View File

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Rector\Utils\ProjectValidator;
use Nette\Utils\FileSystem;
use Nette\Utils\Strings;
use Rector\Utils\ProjectValidator\Exception\CouldNotDeterminedCpuCoresException;
final class CpuCoreCountResolver
@ -18,7 +20,9 @@ final class CpuCoreCountResolver
{
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
$str = trim(shell_exec('wmic cpu get NumberOfCores 2>&1'));
if (! preg_match('#(\d+)#', $str, $matches)) {
$matches = Strings::match($str, '#(\d+)#');
if (! $matches) {
throw new CouldNotDeterminedCpuCoresException('wmic failed to get number of cpu cores on windows!');
}
@ -34,13 +38,13 @@ final class CpuCoreCountResolver
}
if (is_readable('/proc/cpuinfo')) {
$cpuinfo = file_get_contents('/proc/cpuinfo');
$cpuinfo = FileSystem::read('/proc/cpuinfo');
$count = substr_count($cpuinfo, 'processor');
if ($count > 0) {
return $count;
}
}
throw new CouldNotDeterminedCpuCoresException('Failed to detect number of CPUs!');
throw new CouldNotDeterminedCpuCoresException('Failed to detect number of CPUs');
}
}

View File

@ -33,6 +33,26 @@ final class ParallelTaskRunner
*/
private $symfonyStyle;
/**
* @var Process[]
*/
private $runningProcesses = [];
/**
* @var bool
*/
private $isSuccessful = true;
/**
* @var int
*/
private $finishedProcessCount = 0;
/**
* @var SetTask[]
*/
private $remainingTasks = [];
public function __construct(SymfonyStyle $symfonyStyle)
{
$this->phpExecutable = 'php';
@ -48,26 +68,19 @@ final class ParallelTaskRunner
{
$this->printInfo($tasks, $maxProcesses);
$success = true;
/** @var Process[] $runningProcesses */
$runningProcesses = [];
$remainingTasks = $tasks;
$finished = 0;
$this->remainingTasks = $tasks;
$total = count($tasks);
do {
$this->sleepIfNecessary($remainingTasks, $runningProcesses, $maxProcesses, $sleepInSeconds);
$this->sleepIfNecessary($maxProcesses, $sleepInSeconds);
$this->startProcess($maxProcesses, $total);
$this->evaluateRunningProcesses($total);
$this->startProcess($remainingTasks, $runningProcesses, $success, $maxProcesses, $total, $finished);
$this->evaluateRunningProcesses($runningProcesses, $success, $total, $finished);
$someProcessesAreStillRunning = count($runningProcesses) > 0;
$notAllProcessesAreStartedYet = count($remainingTasks) > 0;
$someProcessesAreStillRunning = count($this->runningProcesses) > 0;
$notAllProcessesAreStartedYet = count($this->remainingTasks) > 0;
} while ($someProcessesAreStillRunning || $notAllProcessesAreStartedYet);
return $success;
return $this->isSuccessful;
}
/**
@ -76,80 +89,63 @@ final class ParallelTaskRunner
* we can't start another processes:
* either because none are left or
* because we reached the threshold of allowed processes
*
* @param string[] $taskIdsToRuns
* @param Process[] $runningProcesses
*/
private function sleepIfNecessary(
array $taskIdsToRuns,
array $runningProcesses,
int $maxProcesses,
int $secondsToSleep
): void {
$noMoreProcessesAreLeft = count($taskIdsToRuns) === 0;
$maxNumberOfProcessesAreRunning = count($runningProcesses) >= $maxProcesses;
private function sleepIfNecessary(int $maxProcesses, int $secondsToSleep): void
{
$noMoreProcessesAreLeft = count($this->remainingTasks) === 0;
$maxNumberOfProcessesAreRunning = count($this->runningProcesses) >= $maxProcesses;
if ($noMoreProcessesAreLeft || $maxNumberOfProcessesAreRunning) {
sleep($secondsToSleep);
}
}
/**
* @param SetTask[] $remainingTasks
* @param SetTask[] $runningProcesses
*/
private function startProcess(
array &$remainingTasks,
array &$runningProcesses,
bool &$success,
int $maxProcesses,
int $total,
int $finished
): void {
if ($this->canStartAnotherProcess($remainingTasks, $runningProcesses, $maxProcesses)) {
$setName = array_key_first($remainingTasks);
$task = array_shift($remainingTasks);
private function startProcess(int $maxProcesses, int $total): void
{
if ($this->canStartAnotherProcess($maxProcesses)) {
$setName = array_key_first($this->remainingTasks);
$task = array_shift($this->remainingTasks);
try {
$process = $this->createProcessFromSetTask($task);
$process->start();
$runningProcesses[$setName] = $process;
$this->runningProcesses[$setName] = $process;
} catch (Throwable $throwable) {
$success = false;
$this->printError($setName, $throwable->getMessage(), $total, $finished);
$this->isSuccessful = false;
$this->printError($setName, $throwable->getMessage(), $total);
}
}
}
private function evaluateRunningProcesses(
array &$runningProcesses,
bool &$success,
int $total,
int &$finished
): void {
foreach ($runningProcesses as $setName => $process) {
private function evaluateRunningProcesses(int $total): void
{
foreach ($this->runningProcesses as $setName => $process) {
if ($process->isRunning()) {
continue;
}
$finished++;
unset($runningProcesses[$setName]);
++$this->finishedProcessCount;
unset($this->runningProcesses[$setName]);
try {
$this->evaluateProcess($process);
$this->printSuccess($setName, $total, $finished);
$this->printSuccess($setName, $total);
} catch (Throwable $throwable) {
$success = false;
$this->printError($setName, $process->getOutput() . $process->getErrorOutput(), $total, $finished);
$this->isSuccessful = false;
$this->printError($setName, $process->getOutput() . $process->getErrorOutput(), $total);
}
}
}
private function canStartAnotherProcess(array $remainingTasks, array $runningProcesses, int $max): bool
private function canStartAnotherProcess(int $max): bool
{
$hasOpenTasks = count($remainingTasks) > 0;
$moreProcessesCanBeStarted = count($runningProcesses) < $max;
return $hasOpenTasks && $moreProcessesCanBeStarted;
$hasOpenTasks = count($this->remainingTasks) > 0;
if (! $hasOpenTasks) {
return false;
}
return count($this->runningProcesses) < $max;
}
private function createProcessFromSetTask(SetTask $setTask): Process
@ -167,15 +163,15 @@ final class ParallelTaskRunner
return new Process($command, $this->cwd);
}
private function printSuccess(string $set, int $totalTasks, int $finishedTasks): void
private function printSuccess(string $set, int $totalTasks): void
{
$message = sprintf('(%d/%d) Set "%s" is OK', $finishedTasks, $totalTasks, $set);
$message = sprintf('(%d/%d) Set "%s" is OK', $this->finishedProcessCount, $totalTasks, $set);
$this->symfonyStyle->success($message);
}
private function printError(string $set, string $output, int $totalTasks, int $finishedTasks): void
private function printError(string $set, string $output, int $totalTasks): void
{
$message = sprintf('(%d/%d) Set "%s" failed: %s', $finishedTasks, $totalTasks, $set, $output);
$message = sprintf('(%d/%d) Set "%s" failed: %s', $this->finishedProcessCount, $totalTasks, $set, $output);
$this->symfonyStyle->error($message);
}