-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework commands for more consistent reporting.
- Loading branch information
1 parent
b80ec95
commit b070ebf
Showing
4 changed files
with
419 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Drupal\islandora_drush_utils\Drush\Commands; | ||
|
||
use Drupal\Core\DependencyInjection\AutowireTrait; | ||
use Drupal\Core\Queue\QueueFactory; | ||
use Drupal\islandora_drush_utils\Drush\Commands\Traits\WrappedCommandVerbosityTrait; | ||
use Drush\Attributes as CLI; | ||
use Drush\Commands\DrushCommands; | ||
use Drush\Commands\core\QueueCommands; | ||
use Drush\Drush; | ||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
|
||
/** | ||
* Extended queue-running command. | ||
*/ | ||
class QueueDrushCommands extends DrushCommands { | ||
|
||
use AutowireTrait; | ||
use WrappedCommandVerbosityTrait; | ||
|
||
/** | ||
* Construct. | ||
*/ | ||
public function __construct( | ||
#[Autowire(service: 'queue')] | ||
protected QueueFactory $queueFactory, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Command callback; wrap queue running to run to completion. | ||
* | ||
* NOTE: `time-per-iteration` and `items-per-iteration` should be selected | ||
* with awareness of the environments memory constraints. | ||
*/ | ||
#[CLI\Command(name: 'islandora_drush_utils:queue:run')] | ||
#[CLI\Argument(name: 'name', description: 'The name of the queue to run.')] | ||
#[CLI\Option(name: 'time-per-iteration', description: 'The time limit we will provide to the wrapped `queue:run` invocation.')] | ||
#[CLI\Option(name: 'items-per-invocation', description: 'The item limit we will provide to the wrapped `queue:run` invocation.')] | ||
#[CLI\ValidateQueueName(argumentName: 'name')] | ||
public function runQueue( | ||
string $name, | ||
array $options = [ | ||
'time-per-iteration' => 300, | ||
'items-per-iteration' => 100, | ||
], | ||
) : void { | ||
$queue = $this->queueFactory->get($name, TRUE); | ||
|
||
while ($queue->numberOfItems() > 0) { | ||
$process = Drush::drush( | ||
Drush::aliasManager()->getSelf(), | ||
QueueCommands::RUN, | ||
[$name], | ||
[ | ||
'time-limit' => $options['time-per-iteration'], | ||
'items-limit' => $options['items-per-iteration'], | ||
] + $this->getVerbosityOptions(), | ||
); | ||
// We expect sane exit from time * items. | ||
$process->setTimeout(NULL); | ||
$process->run(static::directOutputCallback(...)); | ||
if (!$process->isSuccessful()) { | ||
throw new \Exception('Subprocess failed.'); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/Drush/Commands/Traits/WrappedCommandVerbosityTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Drupal\islandora_drush_utils\Drush\Commands\Traits; | ||
|
||
use Drush\Commands\DrushCommands; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Process\Process; | ||
|
||
/** | ||
* Facilitate forwarding of verbosity options to wrapped commands. | ||
*/ | ||
trait WrappedCommandVerbosityTrait { | ||
|
||
/** | ||
* Helper; build map of verbosity options from the current instance. | ||
* | ||
* @return bool[] | ||
* An associative array mapping options as strings to booleans representing | ||
* the state of the given options. | ||
* | ||
* @see static::mapVerbosityOptions() | ||
*/ | ||
protected function getVerbosityOptions() : array { | ||
$input = match(TRUE) { | ||
$this instanceof DrushCommands => $this->input(), | ||
}; | ||
|
||
return static::mapVerbosityOptions($input); | ||
} | ||
|
||
/** | ||
* Helper; build map of verbosity options from the given input. | ||
* | ||
* @param \Symfony\Component\Console\Input\InputInterface $input | ||
* The input from which to map the options. | ||
* | ||
* @return bool[] | ||
* An associative array mapping options as strings to booleans representing | ||
* the state of the given options. | ||
*/ | ||
protected static function mapVerbosityOptions(InputInterface $input) : array { | ||
return [ | ||
// Adapted from https://github.com/drush-ops/drush/blob/7fe0a492d5126c457c5fb184c4668a132b0aaac6/src/Application.php#L291-L302 | ||
'verbose' => $input->getParameterOption(['--verbose', '-v'], FALSE, TRUE) !== FALSE, | ||
'vv' => $input->getParameterOption(['-vv'], FALSE, TRUE) !== FALSE, | ||
'vvv' => $input->getParameterOption(['--debug', '-d', '-vvv'], FALSE, TRUE) !== FALSE, | ||
]; | ||
} | ||
|
||
/** | ||
* Helper to directly output from wrapped commands. | ||
* | ||
* @param string $type | ||
* The type of output, one of Process::OUT and Process::ERR. | ||
* @param string $output | ||
* The output to output. | ||
* | ||
* @return false|int | ||
* The number of bytes written; otherwise, FALSE. | ||
*/ | ||
protected static function directOutputCallback(string $type, string $output) : false|int { | ||
$fp = match($type) { | ||
Process::OUT => STDOUT, | ||
Process::ERR => STDERR, | ||
}; | ||
return fwrite($fp, $output); | ||
} | ||
|
||
} |
Oops, something went wrong.