Queue8.php

  1. 8.0.x lib/Drush/Queue/Queue8.php
  2. 7.x lib/Drush/Queue/Queue8.php
  3. master lib/Drush/Queue/Queue8.php

Namespace

Drush\Queue

Classes

Namesort descending Description
Queue8

File

lib/Drush/Queue/Queue8.php
View source
  1. <?php
  2. namespace Drush\Queue;
  3. use Drush\Log\LogLevel;
  4. use Drupal\Core\Queue\QueueWorkerManager;
  5. use Drupal\Core\Queue\SuspendQueueException;
  6. class Queue8 extends QueueBase {
  7. /**
  8. * @var \Drupal\Core\Queue\QueueWorkerManager
  9. */
  10. protected $workerManager;
  11. /**
  12. * Set the queue worker manager.
  13. */
  14. public function __construct(QueueWorkerManager $manager = NULL) {
  15. $this->workerManager = $manager ?: \Drupal::service('plugin.manager.queue_worker');
  16. }
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function getQueues() {
  21. if (!isset(static::$queues)) {
  22. static::$queues = array();
  23. foreach ($this->workerManager->getDefinitions() as $name => $info) {
  24. static::$queues[$name] = $info;
  25. }
  26. }
  27. return static::$queues;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. *
  32. * @return \Drupal\Core\Queue\QueueInterface
  33. */
  34. public function getQueue($name) {
  35. return \Drupal::queue($name);
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function run($name, $time_limit = 0) {
  41. $worker = $this->workerManager->createInstance($name);
  42. $end = time() + $time_limit;
  43. $queue = $this->getQueue($name);
  44. $count = 0;
  45. while ((!$time_limit || time() < $end) && ($item = $queue->claimItem())) {
  46. try {
  47. drush_log(dt('Processing item @id from @name queue.', array('@name' => $name, 'id' => $item->item_id)), LogLevel::INFO);
  48. $worker->processItem($item->data);
  49. $queue->deleteItem($item);
  50. $count++;
  51. }
  52. catch (SuspendQueueException $e) {
  53. // If the worker indicates there is a problem with the whole queue,
  54. // release the item and skip to the next queue.
  55. $queue->releaseItem($item);
  56. drush_set_error('DRUSH_SUSPEND_QUEUE_EXCEPTION', $e->getMessage());
  57. }
  58. catch (\Exception $e) {
  59. // In case of any other kind of exception, log it and leave the item
  60. // in the queue to be processed again later.
  61. drush_set_error('DRUSH_QUEUE_EXCEPTION', $e->getMessage());
  62. }
  63. }
  64. return $count;
  65. }
  66. }