Skip to content

Commit 738631b

Browse files
committed
feat: allow wp-cli commands to run asynchronously
1 parent 6d3c463 commit 738631b

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

src/Command/AbstractCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,9 @@ protected function retryApi(callable $callable, string $message, ConsoleOutput $
342342
*/
343343
protected function wait(callable $callable, int $timeout = 60, int $sleep = 1)
344344
{
345-
$timeout += time();
345+
if (0 !== $timeout) {
346+
$timeout += time();
347+
}
346348

347349
do {
348350
$result = $callable();

src/Command/WpCliCommand.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,19 @@ protected function configure()
3838
->setName(self::NAME)
3939
->setDescription('Execute a WP-CLI command')
4040
->addArgument('wp-command', InputArgument::IS_ARRAY, 'The WP-CLI command to execute')
41-
->addOption('environment', null, InputOption::VALUE_REQUIRED, 'The environment name', 'staging');
41+
->addOption('environment', null, InputOption::VALUE_REQUIRED, 'The environment name', 'staging')
42+
->addOption('async', null, InputOption::VALUE_NONE, 'Execute WP-CLI command asynchronously');
4243
}
4344

4445
/**
4546
* {@inheritdoc}
4647
*/
4748
protected function perform(InputInterface $input, ConsoleOutput $output)
4849
{
50+
$async = $this->getBooleanOption($input, 'async');
4951
$command = implode(' ', $this->getArrayArgument($input, 'wp-command'));
5052
$environment = (string) $this->getStringOption($input, 'environment');
53+
$exitCode = 0;
5154

5255
if (empty($command) && $input->isInteractive()) {
5356
$command = $output->ask('Please enter the WP-CLI command to run');
@@ -61,15 +64,19 @@ protected function perform(InputInterface $input, ConsoleOutput $output)
6164
throw new RuntimeException(sprintf('The "wp %s" command isn\'t available remotely', $command));
6265
}
6366

64-
$output->info(sprintf('Running "<comment>wp %s</comment>" on "<comment>%s</comment>" environment', $command, $environment));
67+
$output->info(sprintf('Running "<comment>wp %s</comment>" %s "<comment>%s</comment>" environment', $command, $async ? 'asynchronously on' : 'on', $environment));
6568

6669
$result = $this->invokeEnvironmentFunction($environment, [
6770
'php' => sprintf('bin/wp %s', $command),
68-
], Arr::get($this->projectConfiguration->getEnvironment($environment), 'console.timeout', 60));
71+
], $async ? 0 : Arr::get($this->projectConfiguration->getEnvironment($environment), 'console.timeout', 60));
6972

70-
$output->newLine();
71-
$output->write("${result['output']}");
73+
if (!$async) {
74+
$output->newLine();
75+
$output->write("${result['output']}");
7276

73-
return $result['exitCode'];
77+
$exitCode = $result['exitCode'];
78+
}
79+
80+
return $exitCode;
7481
}
7582
}

0 commit comments

Comments
 (0)