Skip to content

Commit 90def1a

Browse files
SWR-15367
1 parent 765ce4d commit 90def1a

File tree

7 files changed

+170
-11
lines changed

7 files changed

+170
-11
lines changed

README.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ RabbitMQ Queue driver for Laravel
99

1010
Only the latest version will get new features. Bug fixes will be provided using the following scheme:
1111

12-
| Package Version | Laravel Version | Bug Fixes Until | |
13-
|-----------------|-----------------|-------------------|---------------------------------------------------------------------------------------------|
14-
| 1 | 06 | December 24th, 2024 | [Documentation](https://github.com/vyuldashev/laravel-queue-rabbitmq/blob/master/README.md) |
12+
| Package Version | Laravel Version | Bug Fixes Until | |
13+
|-----------------|-----------------|---------------------|---------------------------------------------------------------------------------------------|
14+
| 1 | 07 | December 26th, 2024 | [Documentation](https://github.com/vyuldashev/laravel-queue-rabbitmq/blob/master/README.md) |
1515

1616
## Installation
1717

1818
You can install this package via composer using this command:
1919

2020
```
21-
composer require salesmessage/php-lib-rabbitmq:^1.06 --ignore-platform-reqs
21+
composer require salesmessage/php-lib-rabbitmq:^1.07 --ignore-platform-reqs
2222
```
2323

2424
The package will automatically register itself.
@@ -462,6 +462,26 @@ It is possible to change te default queue by adding an extra parameter in the co
462462
],
463463
```
464464

465+
### Immediate Indexation
466+
467+
By default, your connection will be created with a immediate indexation setting of `false`.
468+
469+
470+
```php
471+
472+
'connections' => [
473+
// ...
474+
475+
'rabbitmq_vhosts' => [
476+
// ...
477+
478+
'immediate_indexation' => env('RABBITMQ_IMMEDIATE_INDEXATION', false),
479+
],
480+
481+
// ...
482+
],
483+
```
484+
465485
### Heartbeat
466486

467487
By default, your connection will be created with a heartbeat setting of `0`.
@@ -574,7 +594,7 @@ Available protocols : `tcp`, `ssl`, `tls`
574594

575595
### Octane support
576596

577-
Starting with 13.3.0, this package supports [Laravel Octane](https://laravel.com/docs/octane) out of the box.
597+
Starting with 1.04, this package supports [Laravel Octane](https://laravel.com/docs/octane) out of the box.
578598
Firstly, install Octane and don't forget to warm 'rabbitmq' connection in the octane config.
579599
> See: https://github.com/vyuldashev/laravel-queue-rabbitmq/issues/460#issuecomment-1469851667
580600

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
"extra": {
3636
"branch-alias": {
37-
"dev-master": "1.06-dev"
37+
"dev-master": "1.07-dev"
3838
},
3939
"laravel": {
4040
"providers": [

config/rabbitmq.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'driver' => 'rabbitmq_vhosts',
1111
'queue' => env('RABBITMQ_QUEUE', 'default'),
1212
'connection' => 'default',
13+
'immediate_indexation' => env('RABBITMQ_IMMEDIATE_INDEXATION', false),
1314

1415
'hosts' => [
1516
[

src/Dto/QueueApiDto.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ public function getMessages(): int
5555
return $this->messages;
5656
}
5757

58+
/**
59+
* @param int $messages
60+
* @return $this
61+
*/
62+
public function setMessages(int $messages): self
63+
{
64+
$this->messages = $messages;
65+
return $this;
66+
}
67+
5868
/**
5969
* @return int
6070
*/
@@ -63,6 +73,16 @@ public function getMessagesReady(): int
6373
return $this->messagesReady;
6474
}
6575

76+
/**
77+
* @param int $messagesReady
78+
* @return $this
79+
*/
80+
public function setMessagesReady(int $messagesReady): self
81+
{
82+
$this->messagesReady = $messagesReady;
83+
return $this;
84+
}
85+
6686
/**
6787
* @return int
6888
*/

src/Dto/VhostApiDto.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ public function getMessages(): int
5252
return $this->messages;
5353
}
5454

55+
/**
56+
* @param int $messages
57+
* @return $this
58+
*/
59+
public function setMessages(int $messages): self
60+
{
61+
$this->messages = $messages;
62+
return $this;
63+
}
64+
5565
/**
5666
* @return int
5767
*/
@@ -60,6 +70,16 @@ public function getMessagesReady(): int
6070
return $this->messagesReady;
6171
}
6272

73+
/**
74+
* @param int $messagesReady
75+
* @return $this
76+
*/
77+
public function setMessagesReady(int $messagesReady): self
78+
{
79+
$this->messagesReady = $messagesReady;
80+
return $this;
81+
}
82+
6383
/**
6484
* @return int
6585
*/

src/Queue/RabbitMQQueueBatchable.php

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,44 @@
44

55
use PhpAmqpLib\Connection\AbstractConnection;
66
use Salesmessage\LibRabbitMQ\Dto\ConnectionNameDto;
7+
use Salesmessage\LibRabbitMQ\Dto\QueueApiDto;
8+
use Salesmessage\LibRabbitMQ\Dto\VhostApiDto;
79
use Salesmessage\LibRabbitMQ\Queue\RabbitMQQueue as BaseRabbitMQQueue;
810
use PhpAmqpLib\Exception\AMQPChannelClosedException;
911
use PhpAmqpLib\Exception\AMQPConnectionClosedException;
1012
use PhpAmqpLib\Channel\AMQPChannel;
13+
use Salesmessage\LibRabbitMQ\Services\GroupsService;
14+
use Salesmessage\LibRabbitMQ\Services\InternalStorageManager;
1115
use Salesmessage\LibRabbitMQ\Services\VhostsService;
1216

1317
class RabbitMQQueueBatchable extends BaseRabbitMQQueue
1418
{
15-
protected function publishBasic($msg, $exchange = '', $destination = '', $mandatory = false, $immediate = false, $ticket = null): void
19+
private InternalStorageManager $internalStorageManager;
20+
21+
private GroupsService $groupsService;
22+
23+
private VhostsService $vhostsService;
24+
25+
/**
26+
* @param QueueConfig $config
27+
*/
28+
public function __construct(QueueConfig $config)
29+
{
30+
$this->internalStorageManager = app(InternalStorageManager::class);
31+
$this->groupsService = app(GroupsService::class);
32+
$this->vhostsService = app(VhostsService::class);
33+
34+
parent::__construct($config);
35+
}
36+
37+
protected function publishBasic(
38+
$msg,
39+
$exchange = '',
40+
$destination = '',
41+
$mandatory = false,
42+
$immediate = false,
43+
$ticket = null
44+
): void
1645
{
1746
try {
1847
parent::publishBasic($msg, $exchange, $destination, $mandatory, $immediate, $ticket);
@@ -61,6 +90,10 @@ public function push($job, $data = '', $queue = null)
6190
$result = parent::push($job, $data, $queue);
6291
}
6392

93+
if (config('queue.connections.rabbitmq_vhosts.immediate_indexation')) {
94+
$this->addQueueToIndex((string) $queue);
95+
}
96+
6497
return $result;
6598
}
6699

@@ -81,10 +114,39 @@ private function createNotExistsVhost(): bool
81114
return false;
82115
}
83116

84-
/** @var VhostsService $vhostsService */
85-
$vhostsService = app(VhostsService::class);
86-
87-
return $vhostsService->createVhost($dto->getVhostName(), 'Automatically created vhost');
117+
return $this->vhostsService->createVhost($dto->getVhostName(), 'Automatically created vhost');
88118
}
89119

120+
/**
121+
* @param string $queue
122+
* @return bool
123+
*/
124+
private function addQueueToIndex(string $queue): bool
125+
{
126+
if ('' === $queue) {
127+
return false;
128+
}
129+
130+
$dto = new ConnectionNameDto($this->getConnectionName());
131+
$vhostName = $dto->getVhostName();
132+
if (null === $vhostName) {
133+
return false;
134+
}
135+
136+
$groups = $this->groupsService->getAllGroupsNames();
137+
138+
$queueApiDto = new QueueApiDto([
139+
'name' => $queue,
140+
'vhost' => $vhostName,
141+
]);
142+
$isQueueActivated = $this->internalStorageManager->activateQueue($queueApiDto, $groups);
143+
144+
$vhostDto = new VhostApiDto([
145+
'name' => $vhostName,
146+
]);
147+
$isVhostActivated = $this->internalStorageManager->activateVhost($vhostDto, $groups);
148+
149+
return $isQueueActivated && $isVhostActivated;
150+
}
90151
}
152+

src/Services/InternalStorageManager.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,24 @@ public function removeVhost(VhostApiDto $vhostDto): bool
121121
return true;
122122
}
123123

124+
/**
125+
* @param VhostApiDto $vhostDto
126+
* @param array $groups
127+
* @return bool
128+
*/
129+
public function activateVhost(VhostApiDto $vhostDto, array $groups): bool
130+
{
131+
$storageKey = $this->getVhostStorageKey($vhostDto);
132+
133+
$messages = (int) $this->redis->hget($storageKey, 'messages') + 1;
134+
$vhostDto->setMessages($messages);
135+
136+
$messagesReady = (int) $this->redis->hget($storageKey, 'messages_ready') + 1;
137+
$vhostDto->setMessagesReady($messagesReady);
138+
139+
return $this->addVhost($vhostDto, $groups);
140+
}
141+
124142
/**
125143
* @param VhostApiDto $vhostDto
126144
* @return bool
@@ -215,6 +233,24 @@ public function removeQueue(QueueApiDto $queueDto): bool
215233
return true;
216234
}
217235

236+
/**
237+
* @param QueueApiDto $queueDto
238+
* @param array $groups
239+
* @return bool
240+
*/
241+
public function activateQueue(QueueApiDto $queueDto, array $groups): bool
242+
{
243+
$storageKey = $this->getQueueStorageKey($queueDto);
244+
245+
$messages = (int) $this->redis->hget($storageKey, 'messages') + 1;
246+
$queueDto->setMessages($messages);
247+
248+
$messagesReady = (int) $this->redis->hget($storageKey, 'messages_ready') + 1;
249+
$queueDto->setMessagesReady($messagesReady);
250+
251+
return $this->addQueue($queueDto, $groups);
252+
}
253+
218254
/**
219255
* @param VhostApiDto $vhostDto
220256
* @return bool

0 commit comments

Comments
 (0)