Skip to content

Commit 546d69b

Browse files
authored
code clean-up (vyuldashev#317)
* Better place to tell rabbitMQ Job has failed. * cleanup * cleanup style (not showing-up in local style test)
1 parent 2bd1cab commit 546d69b

File tree

6 files changed

+392
-77
lines changed

6 files changed

+392
-77
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace VladimirYuldashev\LaravelQueueRabbitMQ\Console;
4+
5+
use Exception;
6+
use Illuminate\Console\Command;
7+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Connectors\RabbitMQConnector;
8+
9+
class ExchangeDeleteCommand extends Command
10+
{
11+
protected $signature = 'rabbitmq:exchange-delete
12+
{name : The name of the exchange to delete}
13+
{connection=rabbitmq : The name of the queue connection to use}
14+
{--unused=0 : Check if exchange is unused}';
15+
16+
protected $description = 'Delete exchange';
17+
18+
/**
19+
* @param RabbitMQConnector $connector
20+
* @throws Exception
21+
*/
22+
public function handle(RabbitMQConnector $connector): void
23+
{
24+
$config = $this->laravel['config']->get('queue.connections.'.$this->argument('connection'));
25+
26+
$queue = $connector->connect($config);
27+
28+
if (! $queue->isExchangeExists($this->argument('name'))) {
29+
$this->warn('Exchange does not exist.');
30+
31+
return;
32+
}
33+
34+
$queue->deleteExchange(
35+
$this->argument('name'),
36+
(bool) $this->option('unused')
37+
);
38+
39+
$this->info('Exchange deleted successfully.');
40+
}
41+
}

src/Console/QueueDeleteCommand.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace VladimirYuldashev\LaravelQueueRabbitMQ\Console;
4+
5+
use Exception;
6+
use Illuminate\Console\Command;
7+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Connectors\RabbitMQConnector;
8+
9+
class QueueDeleteCommand extends Command
10+
{
11+
protected $signature = 'rabbitmq:queue-delete
12+
{name : The name of the queue to delete}
13+
{connection=rabbitmq : The name of the queue connection to use}
14+
{--unused=0 : Check if queue has no consumers}
15+
{--empty=0 : Check if queue is empty}';
16+
17+
protected $description = 'Declare queue';
18+
19+
/**
20+
* @param RabbitMQConnector $connector
21+
* @throws Exception
22+
*/
23+
public function handle(RabbitMQConnector $connector): void
24+
{
25+
$config = $this->laravel['config']->get('queue.connections.'.$this->argument('connection'));
26+
27+
$queue = $connector->connect($config);
28+
29+
if (! $queue->isQueueExists($this->argument('name'))) {
30+
$this->warn('Queue does not exist.');
31+
32+
return;
33+
}
34+
35+
$queue->deleteQueue(
36+
$this->argument('name'),
37+
(bool) $this->option('unused'),
38+
(bool) $this->option('empty')
39+
);
40+
41+
$this->info('Queue deleted successfully.');
42+
}
43+
}

src/Queue/Jobs/RabbitMQJob.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Contracts\Queue\Job as JobContract;
88
use Illuminate\Queue\Jobs\Job;
99
use Illuminate\Support\Arr;
10+
use PhpAmqpLib\Exception\AMQPProtocolChannelException;
1011
use PhpAmqpLib\Message\AMQPMessage;
1112
use PhpAmqpLib\Wire\AMQPTable;
1213
use VladimirYuldashev\LaravelQueueRabbitMQ\Horizon\RabbitMQQueue as HorizonRabbitMQQueue;
@@ -55,7 +56,7 @@ public function __construct(
5556
*/
5657
public function getJobId()
5758
{
58-
return json_decode($this->message->getBody(), true)['id'] ?? null;
59+
return $this->decoded['id'] ?? null;
5960
}
6061

6162
/**
@@ -71,31 +72,26 @@ public function getRawBody(): string
7172
*/
7273
public function attempts(): int
7374
{
74-
/** @var AMQPTable|null $headers */
75-
$headers = Arr::get($this->message->get_properties(), 'application_headers');
76-
77-
if (! $headers) {
75+
if (! $data = $this->getRabbitMQMessageHeaders()) {
7876
return 1;
7977
}
8078

81-
$data = $headers->getNativeData();
82-
8379
$laravelAttempts = (int) Arr::get($data, 'laravel.attempts', 0);
8480

85-
return ($laravelAttempts) + 1;
81+
return $laravelAttempts + 1;
8682
}
8783

8884
/**
89-
* @param null $e
85+
* {@inheritdoc}
9086
*/
91-
public function fail($e = null): void
87+
public function markAsFailed(): void
9288
{
89+
parent::markAsFailed();
90+
9391
// We must tel rabbitMQ this Job is failed
9492
// The message must be rejected when the Job marked as failed, in case rabbitMQ wants to do some extra magic.
9593
// like: Death lettering the message to an other exchange/routing-key.
9694
$this->rabbitmq->reject($this);
97-
98-
parent::fail($e);
9995
}
10096

10197
/**
@@ -120,18 +116,21 @@ public function delete(): void
120116
}
121117

122118
/**
123-
* {@inheritdoc}
119+
* Release the job back into the queue.
120+
*
121+
* @param int $delay
122+
* @throws AMQPProtocolChannelException
124123
*/
125124
public function release($delay = 0): void
126125
{
127126
parent::release();
128127

129128
// Always create a new message when this Job is released
130-
$this->rabbitmq->laterRaw($delay, $this->message->body, $this->queue, $this->attempts());
129+
$this->rabbitmq->laterRaw($delay, $this->message->getBody(), $this->queue, $this->attempts());
131130

132131
// Releasing a Job means the message was failed to process.
133-
// Because this Job is always recreated and pushed as new message, this Job is correctly handled.
134-
// We must tell rabbitMQ this fact.
132+
// Because this Job message is always recreated and pushed as new message, this Job message is correctly handled.
133+
// We must tell rabbitMQ this job message can be removed by acknowledging the message.
135134
$this->rabbitmq->ack($this);
136135
}
137136

@@ -154,4 +153,19 @@ public function getRabbitMQMessage(): AMQPMessage
154153
{
155154
return $this->message;
156155
}
156+
157+
/**
158+
* Get the headers from the rabbitMQ message.
159+
*
160+
* @return array|null
161+
*/
162+
protected function getRabbitMQMessageHeaders(): ?array
163+
{
164+
/** @var AMQPTable|null $headers */
165+
if (! $headers = Arr::get($this->message->get_properties(), 'application_headers')) {
166+
return null;
167+
}
168+
169+
return $headers->getNativeData();
170+
}
157171
}

0 commit comments

Comments
 (0)