Skip to content

Commit fd3624b

Browse files
committed
3961: Commands cleanup
1 parent f062830 commit fd3624b

File tree

3 files changed

+141
-28
lines changed

3 files changed

+141
-28
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ composer require os2web/os2web_audit
2626
drush pm:enable os2web_audit
2727
```
2828

29-
### Drush
29+
## Drush
30+
31+
### Test audit log
3032

3133
The module provides a Drush command named audit:log. This command enables you
3234
to log a test message to the configured logger. The audit:log command accepts a
@@ -39,6 +41,15 @@ and once as an error message.
3941
drush audit:log 'This is a test message'
4042
```
4143

44+
### Retry jobs
45+
46+
The module also comes with method for retrying failed jobs. This will retry,
47+
all failed jobs in the `os2web_audit` queue.
48+
49+
```shell
50+
drush audit:retry-jobs
51+
```
52+
4253
## Usage
4354

4455
The module exposes a simple `Logger` service which can log an `info` and `error`

src/Drush/Commands/Commands.php renamed to src/Drush/Commands/LogMessageCommand.php

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Drupal\os2web_audit\Drush\Commands;
44

5-
use Drupal\Core\Database\Connection;
6-
use Drupal\advancedqueue\Job;
75
use Drupal\os2web_audit\Service\Logger;
86
use Drush\Attributes\Argument;
97
use Drush\Attributes\Command;
@@ -15,20 +13,17 @@
1513
/**
1614
* Simple command to send log message into audit log.
1715
*/
18-
class Commands extends DrushCommands {
16+
class LogMessageCommand extends DrushCommands {
1917

2018
/**
2119
* Commands constructor.
2220
*
2321
* @param \Drupal\os2web_audit\Service\Logger $auditLogger
2422
* Audit logger service.
25-
* @param \Drupal\Core\Database\Connection $connection
26-
* The database connection.
2723
*/
2824
public function __construct(
2925
#[Autowire(service: 'os2web_audit.logger')]
3026
protected readonly Logger $auditLogger,
31-
protected Connection $connection,
3227
) {
3328
parent::__construct();
3429
}
@@ -39,7 +34,6 @@ public function __construct(
3934
public static function create(ContainerInterface $container): self {
4035
return new static(
4136
$container->get('os2web_audit.logger'),
42-
$container->get('database'),
4337
);
4438
}
4539

@@ -60,24 +54,4 @@ public function logMessage(string $log_message): void {
6054
$this->auditLogger->error('test', $log_message, TRUE, ['from' => 'drush']);
6155
}
6256

63-
/**
64-
* Retries all failed jobs in the os2web_audit queue.
65-
*/
66-
#[Command(name: 'audit:retry-jobs')]
67-
public function retryJobs(): void {
68-
69-
try {
70-
$this->connection->update('advancedqueue')
71-
->fields(['state' => Job::STATE_QUEUED])
72-
->condition('queue_id', 'os2web_audit')
73-
->condition('state', Job::STATE_FAILURE)
74-
->execute();
75-
76-
$this->output()->writeln('Successfully retried all failed jobs.');
77-
}
78-
catch (\Exception $e) {
79-
$this->output()->writeln($e->getMessage());
80-
}
81-
}
82-
8357
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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

Comments
 (0)