Commit 7b9ff71f authored by Oleh Vehera's avatar Oleh Vehera
Browse files

Issue #3222385 by voleger: Update Drush support

parent 1ecc9574
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -40,5 +40,12 @@
    ],
    "support": {
        "issues": "https://www.drupal.org/project/issues/queue_ui"
    },
    "extra": {
        "drush": {
            "services": {
                "drush.services.yml": "^9 || ^10 || ^11"
            }
        }
    }
}

drush.services.yml

0 → 100644
+8 −0
Original line number Diff line number Diff line
services:
  queue_ui.commands:
    class: \Drupal\queue_ui\Commands\QueueUiCommands
    arguments:
      - '@plugin.manager.queue_worker'
      - '@plugin.manager.queue_ui'
    tags:
      - { name: drush.command }

queue_ui.drush.inc

deleted100644 → 0
+0 −160
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Provide Drush command for queue_ui module.
 */

/**
 * Implements hook_drush_command().
 */
function queue_ui_drush_command() {
  $items = [];

  $items['queue-process'] = [
    'description' => 'Process queue',
    'arguments' => [
      'id' => "The QueueWorker ID",
    ],
    'aliases' => ['qp'],
  ];
  $items['queue-process-all'] = [
    'description' => 'Process all queues',
    'aliases' => ['qpa'],
  ];
  $items['queue-release'] = [
    'description' => 'Remove lease from queue',
    'arguments' => [
      'id' => "The QueueWorker ID",
    ],
    'aliases' => ['qr'],
  ];
  $items['queue-release-all'] = [
    'description' => 'Remove lease from all queue',
    'aliases' => ['qra'],
  ];

  return $items;
}

/**
 * Process queue.
 *
 * @param string $queueName
 *   The name of the queue being inspected.
 */
function drush_queue_ui_queue_process($queueName = NULL) {
  // Require the choice name to be filled.
  if ($queueName = _queue_ui_queue_choice($queueName)) {
    // Add operations and start to batch process.
    _queue_ui_start_batch([
      ['\Drupal\queue_ui\QueueUIBatch::step', [$queueName]],
    ]);
  }
}

/**
 * Process all queues.
 */
function drush_queue_ui_queue_process_all() {
  $operations = [];

  $queues = \Drupal::service('plugin.manager.queue_worker')->getDefinitions();
  // Add operations for each queue.
  foreach ($queues as $queueName => $queue_definition) {
    $operations[] = ['\Drupal\queue_ui\QueueUIBatch::step', [$queueName]];
  }

  // Start batch process.
  _queue_ui_start_batch($operations);
}

/**
 * Remove lease from queue.
 *
 * @param string $queueName
 *   The name of the queue being inspected.
 */
function drush_queue_ui_queue_release($queueName = NULL) {
  // Require the choice name to be filled.
  if ($queueName = _queue_ui_queue_choice($queueName)) {
    _queue_ui_release_queue($queueName);
  }
}

/**
 * Remove lease from all queues.
 */
function drush_queue_ui_queue_release_all() {
  $queues = \Drupal::service('plugin.manager.queue_worker')->getDefinitions();
  // Release each queue.
  foreach ($queues as $queueName => $queue_definition) {
    _queue_ui_release_queue($queueName);
  }
}

/**
 * Remove leases from all items in a queue.
 *
 * @param string $queueName
 *   The name of the queue being inspected.
 */
function _queue_ui_release_queue($queueName) {
  /** @var \Drupal\queue_ui\QueueUIInterface $queue_ui */
  $queue_ui = \Drupal::service('plugin.manager.queue_ui')->fromQueueName($queueName);

  // Remove leases.
  $num_updated = $queue_ui->releaseItems($queueName);

  drush_log(t('@count lease reset in queue @name', [
    '@count' => $num_updated,
    '@name' => $queueName,
  ]), 'ok');
}

/**
 * Give the user a choice prompt.
 *
 * @param string $queueName
 *   The name of the queue being inspected.
 *
 * @return mixed
 *   A mixed value of queue name
 */
function _queue_ui_queue_choice($queueName) {
  // Queue name is not provided.
  if (empty($queueName)) {
    // Get all defined queue names.
    $defined_queues = \Drupal::service('plugin.manager.queue_worker')->getDefinitions();

    $queueNames = array_map(function ($queue) {
      // Render queue title.
      return $queue['title']->render();
    }, $defined_queues);

    // Show a list of all defined queues.
    $queueName = drush_choice($queueNames, t('Which queue do you want to process?'));
  }

  return $queueName;
}

/**
 * Helper function to start a batch process.
 *
 * @param array $operations
 *   Contains the operations that needs to be processed.
 */
function _queue_ui_start_batch(array $operations) {
  $batch = [
    'operations' => $operations,
  ];

  // Set and configure the batch.
  batch_set($batch);
  $batch =& batch_get();
  $batch['progressive'] = FALSE;

  // Process the batch.
  drush_backend_batch_process();
}
+204 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\queue_ui\Commands;

use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Queue\QueueWorkerManagerInterface;
use Drupal\queue_ui\QueueUIBatch;
use Drupal\queue_ui\QueueUIManager;
use Drush\Commands\DrushCommands;

/**
 * A Drush commandfile.
 *
 * Provide Drush command for queue_ui module.
 *
 * See these files for an example of injecting Drupal services:
 *   - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php
 *   - http://cgit.drupalcode.org/devel/tree/drush.services.yml
 */
class QueueUiCommands extends DrushCommands {

  /**
   * Queue worker manager.
   *
   * @var \Drupal\Core\Queue\QueueWorkerManagerInterface
   */
  protected $queueWorkerManager;

  /**
   * Queue UI manager.
   *
   * @var \Drupal\queue_ui\QueueUIManager
   */
  protected $queueUiManager;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Queue\QueueWorkerManagerInterface $queue_worker_manager
   *   Queue worker manager.
   * @param \Drupal\queue_ui\QueueUIManager $queue_ui_manager
   *   Queue UI manager.
   */
  public function __construct(QueueWorkerManagerInterface $queue_worker_manager, QueueUIManager $queue_ui_manager) {
    parent::__construct();
    $this->queueWorkerManager = $queue_worker_manager;
    $this->queueUiManager = $queue_ui_manager;
  }

  /**
   * Get queue UI manager.
   *
   * @return \Drupal\queue_ui\QueueUIManager
   *   Queue UI manager.
   */
  public function queueUiManager(): QueueUIManager {
    return $this->queueUiManager;
  }

  /**
   * Get queue worker manager.
   *
   * @return \Drupal\Core\Queue\QueueWorkerManagerInterface
   *   Queue worker manager.
   */
  protected function queueWorkerManager(): QueueWorkerManagerInterface {
    return $this->queueWorkerManager;
  }

  /**
   * Process queue
   *
   * @param $queueName
   *   The name of the queue being inspected.
   *
   * @command queue:process
   * @aliases qp,queue-process
   */
  public function process($queueName = NULL) {
    // Require the choice name to be filled.
    if ($queueName = $this->queueChoice($queueName)) {
      // Add operations and start to batch process.
      $this->startBatch(
        (new BatchBuilder())
          ->addOperation([QueueUIBatch::class, 'step',], [$queueName])
      );
    }
  }

  /**
   * Process all queues
   *
   *
   * @command queue:process-all
   * @aliases qpa,queue-process-all
   */
  public function processAll() {
    $batch = new BatchBuilder();

    $queues = $this->queueWorkerManager()->getDefinitions();
    // Add operations for each queue.
    foreach ($queues as $queueName => $queue_definition) {
      $batch->addOperation([QueueUIBatch::class, 'step'], [$queueName]);
    }

    // Start batch process.
    $this->startBatch($batch);
  }

  /**
   * Remove lease from queue
   *
   * @param string|null $queueName
   *   The name of the queue being inspected.
   *
   * @command queue:release
   * @aliases qr,queue-release
   */
  public function release(string $queueName = NULL): void {
    // Require the choice name to be filled.
    if ($queueName = $this->queueChoice($queueName)) {
      $this->releaseQueue($queueName);
    }
  }

  /**
   * Remove lease from all queue.
   *
   * @command queue:release-all
   * @aliases qra,queue-release-all
   */
  public function releaseAll() {
    $queues = $this->queueWorkerManager()->getDefinitions();
    // Release each queue.
    foreach ($queues as $queueName => $queue_definition) {
      $this->releaseQueue($queueName);
    }
  }

  /**
   * Remove leases from all items in a queue.
   *
   * @param string $queueName
   *   The name of the queue being inspected.
   */
  protected function releaseQueue(string $queueName): void {
    /** @var \Drupal\queue_ui\QueueUIInterface $queue_ui */
    $queue_ui = $this->queueUiManager()
      ->fromQueueName($queueName);

    // Remove leases.
    $num_updated = $queue_ui->releaseItems($queueName);

    $this->logger()->info(t('@count lease reset in queue @name', [
      '@count' => $num_updated,
      '@name' => $queueName,
    ]));
  }

  /**
   * Give the user a choice prompt.
   *
   * @param string|null $queueName
   *   The name of the queue being inspected.
   *
   * @return string
   *   The queue name.
   */
  protected function queueChoice(string $queueName = NULL): string {
    // Queue name is not provided.
    if (empty($queueName)) {
      // Get all defined queue names.
      $defined_queues = $this->queueWorkerManager()->getDefinitions();

      foreach ($defined_queues as $queue) {
        // Render queue title.
        $queueNames[$queue['id']] = $queue['title']->render();
      }

      // Show a list of all defined queues.
      $queueName = $this->io()
        ->choice(t('Which queue do you want to process?'), $queueNames);
    }

    return $queueName;
  }

  /**
   * Helper function to start a batch process.
   *
   * @param \Drupal\Core\Batch\BatchBuilder $batch
   *   The batch that needs to be processed.
   */
  protected function startBatch(BatchBuilder $batch): void {
    // Set and configure the batch.
    batch_set($batch->toArray());
    $batch =& batch_get();
    $batch['progressive'] = FALSE;

    // Process the batch.
    drush_backend_batch_process();
  }

}