Skip to content

Commit

Permalink
[ENH] move finished processes to top
Browse files Browse the repository at this point in the history
  • Loading branch information
n3o77 committed Apr 2, 2022
1 parent f210acc commit 5389dc0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Command/ProcessRunnerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ abstract class ProcessRunnerCommand extends Command implements SignalableCommand
protected bool $showDetailedLogs = false;

protected array $subProcesses = [];
protected array $inactiveSubProcesses = [];

protected string $projectDirectory;
protected CarbonImmutable $startTime;
protected InputInterface $input;
protected ConsoleOutputInterface $output;
protected bool $terminate = false;

protected int $totalStartedProcesses = 0;
protected int $totalFinishedProcesses = 0;

public function __construct(
string $projectDir
Expand Down Expand Up @@ -113,14 +116,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

$newInactiveProcesses = [];
foreach ($this->subProcesses as $subProcess) {
if ($subProcess->endTime === null
|| SubprocessInfo::STATUS_RUNNING === $subProcess->getStatus())
{
$preEndTime = $subProcess->endTime;
$subProcess->update();
if (null === $preEndTime && null !== $subProcess->endTime) {
$this->inactiveSubProcesses[] = $subProcess;
$newInactiveProcesses[] = $subProcess;
}

$this->updateOverviewForProcess($subProcess);
}
}

/** @var SubprocessInfo $newInactiveProcess */
foreach ($newInactiveProcesses as $newInactiveProcess) {
$this->moveInactiveProcessToTop($newInactiveProcess);
}

if ($this->terminate && $runningProcesses === 0) {
break;
}
Expand All @@ -136,6 +152,37 @@ public function getCommandsToRun(): array
return [];
}

protected function moveInactiveProcessToTop(SubprocessInfo $newInactiveProcess): void
{
$newInactiveProcessIndex = array_search($newInactiveProcess, $this->subProcesses, true);

$subProcess = $this->subProcesses[$this->totalFinishedProcesses];
if ($subProcess === $newInactiveProcess) {
$this->totalFinishedProcesses++;
return;
}

$this->subProcesses[$newInactiveProcessIndex] = $subProcess;
$this->subProcesses[$this->totalFinishedProcesses] = $newInactiveProcess;


$replaceOutput = $subProcess->getOutput();
$replaceIo = $subProcess->getIo();

$newActiveOutput = $newInactiveProcess->getOutput();
$newActiveIo = $newInactiveProcess->getIo();

$subProcess->setOutput($newActiveOutput);
$subProcess->setIo($newActiveIo);
$newInactiveProcess->setOutput($replaceOutput);
$newInactiveProcess->setIo($replaceIo);

$this->updateOverviewForProcess($subProcess);
$this->updateOverviewForProcess($newInactiveProcess);

$this->totalFinishedProcesses++;
}

protected function updateOverviewForProcess(SubprocessInfo $subProcess): void
{
$subProcess->output->clear();
Expand Down
29 changes: 29 additions & 0 deletions src/Struct/SubprocessInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ public static function create(
return new self($process, $io, $output);
}

public function setOutput(ConsoleSectionOutput $output): self
{
$this->output = $output;
return $this;
}

public function getOutput(): ConsoleSectionOutput
{
return $this->output;
}

public function getIo(): SymfonyStyle
{
return $this->io;
}

public function setIo(SymfonyStyle $io): self
{
$this->io = $io;
return $this;
}

public function addOutputRow(string $row): void
{
if (count($this->outputRows) > $this->maxOutputRows) {
Expand Down Expand Up @@ -165,4 +187,11 @@ public function getRuntime(): string
{
return $this->startTime->longAbsoluteDiffForHumans($this->endTime);
}

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


}

0 comments on commit 5389dc0

Please sign in to comment.