|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2web_audit\Drush\Commands; |
| 4 | + |
| 5 | +use Drupal\Core\Database\Connection; |
| 6 | +use Drupal\advancedqueue\Job; |
| 7 | +use Drush\Attributes\Argument; |
| 8 | +use Drush\Attributes\Command; |
| 9 | +use Drush\Attributes\Option; |
| 10 | +use Drush\Commands\DrushCommands; |
| 11 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 12 | + |
| 13 | +/** |
| 14 | + * Simple command to send log message into audit log. |
| 15 | + */ |
| 16 | +class RetryFailedQueueCommand extends DrushCommands { |
| 17 | + |
| 18 | + private const OS2WEB_AUDIT_QUEUE_ID = 'os2web_audit'; |
| 19 | + private const ADVANCEDQUEUE_TABLE = 'advancedqueue'; |
| 20 | + |
| 21 | + /** |
| 22 | + * Commands constructor. |
| 23 | + * |
| 24 | + * @param \Drupal\Core\Database\Connection $connection |
| 25 | + * The database connection. |
| 26 | + */ |
| 27 | + public function __construct( |
| 28 | + protected Connection $connection, |
| 29 | + ) { |
| 30 | + parent::__construct(); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * {@inheritdoc} |
| 35 | + */ |
| 36 | + public static function create(ContainerInterface $container): self { |
| 37 | + return new static( |
| 38 | + $container->get('database'), |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Retries failed jobs in the os2web_audit queue. |
| 44 | + */ |
| 45 | + #[Command(name: 'audit:retry-failed-jobs')] |
| 46 | + #[Argument(name: 'limit', description: "The number of jobs to retry. Minimum: 1, Maximum: 5000")] |
| 47 | + public function retryJobs(int $limit = 1000): void { |
| 48 | + |
| 49 | + if ($limit < 1 || $limit > 5000) { |
| 50 | + $this->output()->writeln('Limit should be an integer between 1 and 5000.'); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + $ids = $this->connection->select(self::ADVANCEDQUEUE_TABLE, 'a') |
| 55 | + ->fields('a', ['job_id']) |
| 56 | + ->condition('queue_id', self::OS2WEB_AUDIT_QUEUE_ID) |
| 57 | + ->condition('state', Job::STATE_FAILURE) |
| 58 | + ->range(0, $limit) |
| 59 | + ->execute() |
| 60 | + ->fetchCol(); |
| 61 | + |
| 62 | + try { |
| 63 | + $this->connection->update(self::ADVANCEDQUEUE_TABLE) |
| 64 | + ->fields(['state' => Job::STATE_QUEUED]) |
| 65 | + ->condition('queue_id', self::OS2WEB_AUDIT_QUEUE_ID) |
| 66 | + ->condition('state', Job::STATE_FAILURE) |
| 67 | + ->condition('job_id', $ids, 'IN') |
| 68 | + ->execute(); |
| 69 | + |
| 70 | + $this->output()->writeln('Successfully retried failed jobs.'); |
| 71 | + } |
| 72 | + catch (\Exception $e) { |
| 73 | + $this->output()->writeln($e->getMessage()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Retries failed job. |
| 79 | + */ |
| 80 | + #[Command(name: 'audit:retry-job')] |
| 81 | + #[Argument(name: 'id', description: "The job ID to retry.")] |
| 82 | + #[Option(name: 'ignore-state', description: 'Retries job regardless of state.')] |
| 83 | + public function retryJob(int $id, bool $ignoreState): void { |
| 84 | + |
| 85 | + try { |
| 86 | + // Check that job exists by fetching its state. |
| 87 | + $query = $this->connection->select(self::ADVANCEDQUEUE_TABLE, 'a') |
| 88 | + ->fields('a', ['state']) |
| 89 | + ->condition('job_id', $id); |
| 90 | + |
| 91 | + $result = $query->execute()->fetchAssoc(); |
| 92 | + |
| 93 | + if (!$result) { |
| 94 | + $this->output()->writeln('Job not found.'); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + // State check. |
| 99 | + if (!$ignoreState && $result['state'] !== Job::STATE_FAILURE) { |
| 100 | + $this->output()->writeln('Job is not in a failed state.'); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + $query = $this->connection->update(self::ADVANCEDQUEUE_TABLE) |
| 105 | + ->fields(['state' => Job::STATE_QUEUED]) |
| 106 | + ->condition('queue_id', self::OS2WEB_AUDIT_QUEUE_ID) |
| 107 | + ->condition('job_id', $id); |
| 108 | + |
| 109 | + if (!$ignoreState) { |
| 110 | + $query->condition('state', Job::STATE_FAILURE); |
| 111 | + } |
| 112 | + |
| 113 | + $result = $query->execute(); |
| 114 | + |
| 115 | + if ($result) { |
| 116 | + $this->output()->writeln('Successfully retried job.'); |
| 117 | + } |
| 118 | + else { |
| 119 | + $this->output()->writeln('Failed retrying job.'); |
| 120 | + } |
| 121 | + |
| 122 | + } |
| 123 | + catch (\Exception $e) { |
| 124 | + $this->output()->writeln($e->getMessage()); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments