Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queue improvements #2422

Merged
merged 3 commits into from Jun 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions config/services/queue.yml
@@ -1,9 +1,9 @@
services:
queue_debug:
class: Drupal\Console\Command\Queue\DebugCommand
tags:
- { name: console.command }
class: Drupal\Console\Command\Queue\DebugCommand
tags:
- { name: console.command }
queue_run:
class: Drupal\Console\Command\Queue\RunCommand
tags:
- { name: console.command }
class: Drupal\Console\Command\Queue\RunCommand
tags:
- { name: console.command }
4 changes: 4 additions & 0 deletions config/translations/en/queue.debug.yml
@@ -1 +1,5 @@
description: 'Display the queues of your application'
messages:
queue: 'Queue'
items: 'Items'
class: 'Class'
12 changes: 9 additions & 3 deletions config/translations/en/queue.run.yml
@@ -1,3 +1,9 @@
description: 'Process the selected queue'
success: 'Processed %s: %s/%s items in %s sec'
error: '%s failed: %s'
description: 'Process the selected queue.'
arguments:
name: 'Queue name.'
messages:
success: 'Processed %s: %s/%s items in %s seconds.'
failed: '%s failed: %s.'
missing-name: 'Provide a valid queue name.'
invalid-name: 'Invalid queue name "%s" provided.'

94 changes: 52 additions & 42 deletions src/Command/Queue/DebugCommand.php
Expand Up @@ -8,63 +8,73 @@
namespace Drupal\Console\Command\Queue;

use Symfony\Component\Console\Command\Command;
use Drupal\Console\Style\DrupalStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Command\Shared\ContainerAwareCommandTrait;
use Drupal\Console\Style\DrupalStyle;

/**
* Class DebugCommand
* @package Drupal\Console\Command\Queue
*/
class DebugCommand extends Command
{
use ContainerAwareCommandTrait;
use ContainerAwareCommandTrait;

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('queue:debug')
->setDescription($this->trans('commands.queue.debug.description'));
}

/**
* @var $queueManager \Drupal\Core\Queue\QueueWorkerManagerInterface
*/
private $queueManager;
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('queue:debug')
->setDescription($this->trans('commands.queue.debug.description'));
}
$tableHeader = [
$this->trans('commands.queue.debug.messages.queue'),
$this->trans('commands.queue.debug.messages.items'),
$this->trans('commands.queue.debug.messages.class')
];

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);
$this->queueManager = $this->getDrupalService('plugin.manager.queue_worker');
$this->listQueues($io);
}
$tableBody = $this->listQueues();

/**
* @param \Drupal\Console\Style\DrupalStyle $io
*/
private function listQueues(DrupalStyle $io) {
$header = ['queue', 'items', 'class'];
foreach ($this->queueManager->getDefinitions() as $name => $info) {
$queues[$name] = $this->formatQueues($name, $header);
$io->table($tableHeader, $tableBody);
}
$io->table($header, $queues);
}

/**
* @param $name
* @param array $header
* @return array
*/
private function formatQueues($name, $header) {
$q = $this->getDrupalService('queue')->get($name);
return array_combine($header, [$name, $q->numberOfItems(), get_class($q)]);
}
/**
* listQueues.
*/
private function listQueues()
{
$queueManager = $this->getDrupalService('plugin.manager.queue_worker');
$queues = [];
foreach ($queueManager->getDefinitions() as $name => $info) {
$queues[$name] = $this->formatQueue($name);
}

return $queues;
}

/**
* @param $name
* @return array
*/
private function formatQueue($name)
{
$q = $this->getDrupalService('queue')->get($name);

return [
$name,
$q->numberOfItems(),
get_class($q)
];
}
}
181 changes: 94 additions & 87 deletions src/Command/Queue/RunCommand.php
Expand Up @@ -7,109 +7,116 @@

namespace Drupal\Console\Command\Queue;

use MongoDB\BSON\Timestamp;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Drupal\Console\Style\DrupalStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Command\Shared\ContainerAwareCommandTrait;
use Drupal\Console\Style\DrupalStyle;

/**
* Class RunCommand
* @package Drupal\Console\Command\Queue
*/
class RunCommand extends Command
{
use ContainerAwareCommandTrait;

/**
* @var $queueManager \Drupal\Core\Queue\QueueWorkerManagerInterface
*/
private $queueManager;

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('queue:run')
->setDescription($this->trans('commands.queue.run.description'))
->addArgument(
'queue-name',
InputArgument::REQUIRED,
$this->trans('commands.queue.run.arguments.queue-name')
);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);
$queue_name = $input->getArgument('queue-name');
if ($queue_name) {
$this->queueManager = $this->getDrupalService('plugin.manager.queue_worker');
$this->runQueue($io, $queue_name);
}
use ContainerAwareCommandTrait;

}

/**
* @param \Drupal\Console\Style\DrupalStyle $io
* @param $queue_name
*/
private function runQueue(DrupalStyle $io, $queue_name) {
$worker = $this->queueManager->createInstance($queue_name);
$q = $this->getDrupalService('queue')->get($queue_name);
$start = microtime(true);
$result = $this->clearQueue($worker, $q);
$time = microtime(true) - $start;
if (empty($result['error'])) {
$io->success(
sprintf(
$this->trans('commands.queue.run.success'),
$queue_name,
$result['count'],
$result['total'],
round($time, 2)
)
);
} else {
$io->error(
sprintf(
$this->trans('commands.queue.run.error'),
$queue_name,
$result['error']
)
);
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('queue:run')
->setDescription($this->trans('commands.queue.run.description'))
->addArgument(
'name',
InputArgument::OPTIONAL,
$this->trans('commands.queue.run.arguments.name')
);
}
}

/**
* @param \Drupal\Core\Queue\QueueWorkerManagerInterface $worker
* @param \Drupal\Core\Queue\Queue $q
* @return array
*/
private function clearQueue($worker, $q) {
$result['count'] = 0;
$result['total'] = $q->numberOfItems();
while ($item = $q->claimItem()) {
try {
$worker->processItem($item->data);
$q->deleteItem($item);
$result['count']++;
}
catch (SuspendQueueException $e) {
$q->releaseItem($item);
$result['error'] = $e;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);
$name = $input->getArgument('name');

if (!$name) {
$io->error(
$this->trans('commands.queue.run.messages.missing-name')
);

return 1;
}

$queueManager = $this->getDrupalService('plugin.manager.queue_worker');

try {
$worker = $queueManager->createInstance($name);
} catch (\Exception $e) {
$io->error(
sprintf(
$this->trans('commands.queue.run.messages.invalid-name'),
$name
)
);

return 1;
}

$start = microtime(true);
$result = $this->runQueue($worker, $name);
$time = microtime(true) - $start;

if (!empty($result['error'])) {
$io->error(
sprintf(
$this->trans('commands.queue.run.messages.failed'),
$name,
$result['error']
)
);

return 1;
}

$io->success(
sprintf(
$this->trans('commands.queue.run.success'),
$name,
$result['count'],
$result['total'],
round($time, 2)
)
);
}

return $result;
}
/**
* @param $worker
* @param $name
*
* @return array
*/
private function runQueue($worker, $name)
{
$q = $this->getDrupalService('queue')->get($name);
$result['count'] = 0;
$result['total'] = $q->numberOfItems();
while ($item = $q->claimItem()) {
try {
$worker->processItem($item->data);
$q->deleteItem($item);
$result['count']++;
} catch (SuspendQueueException $e) {
$q->releaseItem($item);
$result['error'] = $e;
}
}

return $result;
}
}