namespace Drupal\Console\Command\Queue;
-use Symfony\Component\Console\Command\Command;
+use Drupal\Console\Core\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Core\Queue\QueueWorkerManagerInterface;
use Drupal\Core\Queue\QueueFactory;
-use Drupal\Console\Core\Command\Shared\CommandTrait;
-use Drupal\Console\Core\Style\DrupalStyle;
/**
* Class RunCommand
*/
class RunCommand extends Command
{
- use CommandTrait;
-
/**
* @var QueueWorkerManagerInterface
*/
/**
* @var QueueFactory
*/
- protected $queue;
+ protected $queueFactory;
/**
* DebugCommand constructor.
*/
public function __construct(
QueueWorkerManagerInterface $queueWorker,
- QueueFactory $queue
+ QueueFactory $queueFactory
) {
$this->queueWorker = $queueWorker;
- $this->queue = $queue;
+ $this->queueFactory = $queueFactory;
parent::__construct();
}
'name',
InputArgument::OPTIONAL,
$this->trans('commands.queue.run.arguments.name')
- );
+ )->setAliases(['qr']);
}
/**
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
- $io = new DrupalStyle($input, $output);
$name = $input->getArgument('name');
if (!$name) {
- $io->error(
+ $this->getIo()->error(
$this->trans('commands.queue.run.messages.missing-name')
);
try {
$worker = $this->queueWorker->createInstance($name);
+ $queue = $this->queueFactory->get($name);
} catch (\Exception $e) {
- $io->error(
+ $this->getIo()->error(
sprintf(
$this->trans('commands.queue.run.messages.invalid-name'),
$name
}
$start = microtime(true);
- $result = $this->runQueue($worker);
+ $result = $this->runQueue($queue, $worker);
$time = microtime(true) - $start;
if (!empty($result['error'])) {
- $io->error(
+ $this->getIo()->error(
sprintf(
$this->trans('commands.queue.run.messages.failed'),
$name,
return 1;
}
- $io->success(
+ $this->getIo()->success(
sprintf(
$this->trans('commands.queue.run.success'),
$name,
}
/**
- * @param $worker
+ * @param \Drupal\Core\Queue\QueueInterface $queue
+ * @param \Drupal\Core\Queue\QueueWorkerInterface $worker
*
* @return array
*/
- private function runQueue($worker)
+ private function runQueue($queue, $worker)
{
$result['count'] = 0;
- $result['total'] = $this->queue->numberOfItems();
- while ($item = $this->queue->claimItem()) {
+ $result['total'] = $queue->numberOfItems();
+ while ($item = $queue->claimItem()) {
try {
$worker->processItem($item->data);
- $this->queue->deleteItem($item);
+ $queue->deleteItem($item);
$result['count']++;
} catch (SuspendQueueException $e) {
- $this->queue->releaseItem($item);
+ $queue->releaseItem($item);
$result['error'] = $e;
}
}