Skip to content
Snippets Groups Projects
Commit 0488faec authored by Bohdan Mykhalchuk's avatar Bohdan Mykhalchuk
Browse files

Issue #3323520: Create a way to empty a queue

parent 30449cf1
No related branches found
No related tags found
1 merge request!20Issue #3323520: Create a way to empty a queue
services: services:
advancedqueue.commands: advancedqueue.commands:
class: \Drupal\advancedqueue\Commands\AdvancedQueueCommands class: \Drupal\advancedqueue\Commands\AdvancedQueueCommands
arguments: ['@entity_type.manager', '@advancedqueue.processor'] arguments: ['@entity_type.manager', '@advancedqueue.processor', '@database']
tags: tags:
- { name: drush.command } - { name: drush.command }
...@@ -5,6 +5,7 @@ namespace Drupal\advancedqueue\Commands; ...@@ -5,6 +5,7 @@ namespace Drupal\advancedqueue\Commands;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields; use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drupal\advancedqueue\Job; use Drupal\advancedqueue\Job;
use Drupal\advancedqueue\ProcessorInterface; use Drupal\advancedqueue\ProcessorInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drush\Commands\DrushCommands; use Drush\Commands\DrushCommands;
...@@ -28,6 +29,13 @@ class AdvancedQueueCommands extends DrushCommands { ...@@ -28,6 +29,13 @@ class AdvancedQueueCommands extends DrushCommands {
*/ */
protected $processor; protected $processor;
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/** /**
* Constructs a new AdvancedQueueCommands object. * Constructs a new AdvancedQueueCommands object.
* *
...@@ -36,11 +44,12 @@ class AdvancedQueueCommands extends DrushCommands { ...@@ -36,11 +44,12 @@ class AdvancedQueueCommands extends DrushCommands {
* @param \Drupal\advancedqueue\ProcessorInterface $processor * @param \Drupal\advancedqueue\ProcessorInterface $processor
* The queue processor. * The queue processor.
*/ */
public function __construct(EntityTypeManagerInterface $entity_type_manager, ProcessorInterface $processor) { public function __construct(EntityTypeManagerInterface $entity_type_manager, ProcessorInterface $processor, Connection $connection) {
parent::__construct(); parent::__construct();
$this->entityTypeManager = $entity_type_manager; $this->entityTypeManager = $entity_type_manager;
$this->processor = $processor; $this->processor = $processor;
$this->connection = $connection;
} }
/** /**
...@@ -135,4 +144,33 @@ class AdvancedQueueCommands extends DrushCommands { ...@@ -135,4 +144,33 @@ class AdvancedQueueCommands extends DrushCommands {
return new RowsOfFields($rows); return new RowsOfFields($rows);
} }
/**
* Clear a queue.
*
* @param string $queue_id
* The queue ID.
*
* @throws \Exception
*
* @command advancedqueue:queue:clear
* @usage advancedqueue:queue:clear commerce_recurring
*/
public function clear($queue_id) {
if ($this->io()->confirm(dt("Are you sure you want to remove queue items for the the @queue_id queue?", ['@queue_id' => $queue_id]))) {
$queue_storage = $this->entityTypeManager->getStorage('advancedqueue_queue');
/** @var \Drupal\advancedqueue\Entity\QueueInterface $queue */
$queue = $queue_storage->load($queue_id);
if (!$queue) {
throw new \Exception(dt('Could not find queue "@queue_id".', ['@queue_id' => $queue_id]));
}
$this->connection->delete('advancedqueue')
->condition('queue_id', $queue_id)
->execute();
$this->io()->success(dt('The queue items for the the @queue queue have been deleted.', [
'@queue' => $queue->label(),
]));
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment