Skip to content

Commit

Permalink
Merge pull request #3 from robjuz/main
Browse files Browse the repository at this point in the history
allow to set execution timeout
  • Loading branch information
shyim authored May 2, 2024
2 parents 6117bac + 1515bcc commit 6c07eb6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ protected function configure(): void
{
$this->addOption('skip-theme-compile', null, InputOption::VALUE_OPTIONAL, 'Skip theme compile (should be used when the Theme has been compiled before in the CI/CD)', false);
$this->addOption('skip-asset-install', null, InputOption::VALUE_OPTIONAL, 'Skip asset install (should be used when the Assets has been copied before in the CI/CD)', false);
$this->addOption('timeout', null, InputOption::VALUE_OPTIONAL, 'Set script execution timeout (is seconds). Set to null to disable timeout', 300);
}


protected function execute(InputInterface $input, OutputInterface $output): int
{
$timeout = $input->getOption('timeout');

$config = new RunConfiguration(
skipThemeCompile: (bool) $input->getOption('skip-theme-compile'),
skipAssetInstall: (bool) $input->getOption('skip-asset-install'),
timeout: $timeout ? (float) $timeout : null,
);

$installed = $this->state->isInstalled();
Expand Down
35 changes: 35 additions & 0 deletions src/Helper/ProcessHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Shopware\Deployment\Helper;

use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Process\Exception\InvalidArgumentException;
use Symfony\Component\Process\PhpSubprocess;
use Symfony\Component\Process\Process;

Expand All @@ -14,6 +15,7 @@ class ProcessHelper
public function __construct(
#[Autowire('%kernel.project_dir%')]
private readonly string $projectDir,
private ?float $timeout = 60
) {}

/**
Expand All @@ -24,6 +26,7 @@ public function run(array $args): void
$completeCmd = ['php', ...$args];

$process = new PhpSubprocess($args, $this->projectDir);
$process->setTimeout($this->timeout);

$startTime = $this->printPreStart($completeCmd);

Expand Down Expand Up @@ -59,6 +62,7 @@ public function runAndTail(string $code): void
$start = $this->printPreStart([$code]);

$process = new Process(['sh', '-c', $code], $this->projectDir);
$process->setTimeout($this->timeout);

if (function_exists('stream_isatty') && stream_isatty(\STDOUT)) {
$process->setTty(true);
Expand All @@ -83,6 +87,21 @@ public function getPluginList(): string
{
return (new PhpSubprocess(['bin/console', 'plugin:list', '--json'], $this->projectDir))->mustRun()->getOutput();
}
/**
* Sets the process timeout (max. runtime) in seconds.
*
* To disable the timeout, set this value to null.
*
* @return $this
*
* @throws InvalidArgumentException if the timeout is negative
*/
public function setTimeout(?float $timeout): static
{
$this->timeout = $this->validateTimeout($timeout);

return $this;
}

/**
* @param array<string> $cmd
Expand Down Expand Up @@ -117,6 +136,22 @@ private function printPostStart(array $cmd, float $startTime): void
fwrite(\STDOUT, PHP_EOL);
}

/**
* Validates and returns the filtered timeout.
*
* @throws InvalidArgumentException if the given timeout is a negative number
*/
private function validateTimeout(?float $timeout): ?float
{
$timeout = (float) $timeout;

if (0.0 === $timeout) {
$timeout = null;
} elseif ($timeout < 0) {
throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
}

return $timeout;
}

}
2 changes: 2 additions & 0 deletions src/Services/InstallationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public function __construct(

public function run(RunConfiguration $configuration, OutputInterface $output): void
{
$this->processHelper->setTimeout($configuration->timeout);

$output->writeln('Shopware is not installed, starting installation');

$this->hookExecutor->execute(HookExecutor::PRE_INSTALL);
Expand Down
2 changes: 2 additions & 0 deletions src/Services/UpgradeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function __construct(

public function run(RunConfiguration $configuration, OutputInterface $output): void
{
$this->processHelper->setTimeout($configuration->timeout);

$this->hookExecutor->execute(HookExecutor::PRE_UPDATE);

$output->writeln('Shopware is installed, running update tools');
Expand Down
2 changes: 1 addition & 1 deletion src/Struct/RunConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

class RunConfiguration
{
public function __construct(public readonly bool $skipThemeCompile = false, public readonly bool $skipAssetInstall = false) {}
public function __construct(public readonly bool $skipThemeCompile = false, public readonly bool $skipAssetInstall = false, public readonly ?float $timeout = 60) {}
}

0 comments on commit 6c07eb6

Please sign in to comment.