Skip to content

Commit 0d34ba7

Browse files
committed
rework
1 parent 7690c9c commit 0d34ba7

File tree

8 files changed

+102
-133
lines changed

8 files changed

+102
-133
lines changed

README.md

Lines changed: 97 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -174,30 +174,6 @@ This Library supports Horizon, but in the config you have to inform Laravel to u
174174
],
175175
```
176176

177-
### Octane support
178-
179-
Starting with 13.3.0, this package supports [Laravel Octane](https://laravel.com/docs/octane) out of the box. Firstly,
180-
install
181-
Octane and then set `RABBITMQ_WORKER` to `octane`.
182-
183-
This Library supports Octane, but in the config you have to inform Laravel to use the QueueApi compatible with octane.
184-
185-
> Note: don't forget to warm 'rabbitmq' connection in the octane config.
186-
```php
187-
'connections' => [
188-
// ...
189-
190-
'rabbitmq' => [
191-
// ...
192-
193-
/* Set to "octane" if you wish to use Laravel Octane. */
194-
'worker' => env('RABBITMQ_WORKER', 'default'),
195-
],
196-
197-
// ...
198-
],
199-
```
200-
201177
### Use your own RabbitMQJob class
202178

203179
Sometimes you have to work with messages published by another application.
@@ -288,7 +264,9 @@ class RabbitMQJob extends BaseJob
288264
}
289265
```
290266

291-
If you want to handle raw message, not in JSON format or without 'job' key in JSON, you should add stub for `getName` method:
267+
If you want to handle raw message, not in JSON format or without 'job' key in JSON, you should add stub for `getName`
268+
method:
269+
292270
```php
293271
<?php
294272

@@ -336,6 +314,93 @@ An example for the config:
336314
],
337315
```
338316

317+
### Use your own Worker class
318+
319+
If you want to use your own `RabbitMQQueue::class` this is possible by
320+
extending `VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue`.
321+
and inform laravel to use your class by setting `RABBITMQ_WORKER` to `\App\Queue\RabbitMQQueue::class`.
322+
323+
> Note: Worker classes **must** extend ``VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue``
324+
325+
```php
326+
'connections' => [
327+
// ...
328+
329+
'rabbitmq' => [
330+
// ...
331+
332+
/* Set to a class if you wish to use your own. */
333+
'worker' => \App\Queue\RabbitMQQueue::class,
334+
],
335+
336+
// ...
337+
],
338+
```
339+
340+
```php
341+
<?php
342+
343+
namespace App\Queue;
344+
345+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue as BaseRabbitMQQueue;
346+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\ReconnectTrait;
347+
348+
class RabbitMQQueue extends BaseRabbitMQQueue
349+
{
350+
// ...
351+
}
352+
```
353+
354+
**For Example: A reconnect implementation.**
355+
356+
If you want to reconnect to RabbitMQ, if the connection is dead.
357+
You can override the publishing and the createChannel methods.
358+
359+
> Note: this is not best practice, it is an example.
360+
361+
```php
362+
<?php
363+
364+
namespace App\Queue;
365+
366+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue as BaseRabbitMQQueue;
367+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\ReconnectTrait;
368+
369+
class RabbitMQQueue extends BaseRabbitMQQueue
370+
{
371+
372+
protected function publishBasic($msg, $exchange = '', $destination = '', $mandatory = false, $immediate = false, $ticket = null): void
373+
{
374+
try {
375+
parent::publishBasic($msg, $exchange, $destination, $mandatory, $immediate, $ticket);
376+
} catch (AMQPConnectionClosedException|AMQPChannelClosedException) {
377+
$this->reconnect();
378+
parent::publishBasic($msg, $exchange, $destination, $mandatory, $immediate, $ticket);
379+
}
380+
}
381+
382+
protected function publishBatch($jobs, $data = '', $queue = null): void
383+
{
384+
try {
385+
parent::publishBatch($jobs, $data, $queue);
386+
} catch (AMQPConnectionClosedException|AMQPChannelClosedException) {
387+
$this->reconnect();
388+
parent::publishBatch($jobs, $data, $queue);
389+
}
390+
}
391+
392+
protected function createChannel(): AMQPChannel
393+
{
394+
try {
395+
return parent::createChannel();
396+
} catch (AMQPConnectionClosedException) {
397+
$this->reconnect();
398+
return parent::createChannel();
399+
}
400+
}
401+
}
402+
```
403+
339404
### Default Queue
340405

341406
The connection does use a default queue with value 'default', when no queue is provided by laravel.
@@ -445,10 +510,15 @@ If for some reason you don't want the connection lazy you can turn it off by set
445510
],
446511
```
447512

513+
### Octane support
514+
515+
Starting with 13.3.0, this package supports [Laravel Octane](https://laravel.com/docs/octane) out of the box.
516+
Firstly, install Octane and don't forget to warm 'rabbitmq' connection in the octane config.
517+
448518
## Laravel Usage
449519

450-
Once you completed the configuration you can use the Laravel Queue API. If you used other queue drivers you do not need to
451-
change anything else. If you do not know how to use the Queue API, please refer to the official Laravel
520+
Once you completed the configuration you can use the Laravel Queue API. If you used other queue drivers you do not
521+
need to change anything else. If you do not know how to use the Queue API, please refer to the official Laravel
452522
documentation: http://laravel.com/docs/queues
453523

454524
## Lumen Usage

src/Octane/RabbitMQQueue.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/Queue/QueueFactory.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Support\Arr;
66
use VladimirYuldashev\LaravelQueueRabbitMQ\Horizon\RabbitMQQueue as HorizonRabbitMQQueue;
7-
use VladimirYuldashev\LaravelQueueRabbitMQ\Octane\RabbitMQQueue as OctaneRabbitMQQueue;
87

98
class QueueFactory
109
{
@@ -21,10 +20,6 @@ public static function make(array $config = []): RabbitMQQueue
2120
return new HorizonRabbitMQQueue($queueConfig);
2221
}
2322

24-
if (strtolower($worker) == 'octane') {
25-
return new OctaneRabbitMQQueue($queueConfig);
26-
}
27-
2823
return new $worker($queueConfig);
2924
}
3025
}

src/Queue/ReconnectTrait.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

tests/Feature/QueueTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ public function setUp(): void
1414
{
1515
parent::setUp();
1616

17-
$this->withoutExceptionHandling([AMQPChannelClosedException::class, AMQPConnectionClosedException::class, AMQPProtocolChannelException::class]);
17+
$this->withoutExceptionHandling([
18+
AMQPChannelClosedException::class, AMQPConnectionClosedException::class, AMQPProtocolChannelException::class,
19+
]);
1820
}
1921

2022
public function testConnection(): void
@@ -40,6 +42,8 @@ public function testWithoutReconnect(): void
4042

4143
public function testReconnect(): void
4244
{
45+
$this->markTestSkipped();
46+
4347
$queue = $this->connection('octane');
4448

4549
$queue->push(new TestJob());

tests/Functional/RabbitMQQueueTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Support\Str;
66
use PhpAmqpLib\Exchange\AMQPExchangeType;
7-
use VladimirYuldashev\LaravelQueueRabbitMQ\Octane\RabbitMQQueue as OctaneRabbitMQQueue;
87
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue;
98
use VladimirYuldashev\LaravelQueueRabbitMQ\Tests\Functional\TestCase as BaseTestCase;
109

@@ -20,10 +19,6 @@ public function testConnection(): void
2019

2120
$queue = $this->connection('rabbitmq-with-options-empty');
2221
$this->assertInstanceOf(RabbitMQQueue::class, $queue);
23-
24-
$queue = $this->connection('rabbitmq-for-octane');
25-
$this->assertInstanceOf(RabbitMQQueue::class, $queue);
26-
$this->assertInstanceOf(OctaneRabbitMQQueue::class, $queue);
2722
}
2823

2924
public function testConfigRerouteFailed(): void

tests/Functional/TestCase.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,19 +197,6 @@ protected function getEnvironmentSetUp($app): void
197197
'worker' => 'default',
198198

199199
]);
200-
$app['config']->set('queue.connections.rabbitmq-for-octane', [
201-
'driver' => 'rabbitmq',
202-
'hosts' => [
203-
[
204-
'host' => getenv('HOST'),
205-
'port' => getenv('PORT'),
206-
'vhost' => '/',
207-
'user' => 'guest',
208-
'password' => 'guest',
209-
],
210-
],
211-
'worker' => 'octane',
212-
]);
213200
}
214201

215202
/**

tests/TestCase.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,6 @@ protected function getEnvironmentSetUp($app): void
4848
'worker' => 'default',
4949

5050
]);
51-
$app['config']->set('queue.connections.octane', [
52-
'driver' => 'rabbitmq',
53-
'queue' => 'default',
54-
'connection' => AMQPLazyConnection::class,
55-
56-
'hosts' => [
57-
[
58-
'host' => getenv('HOST'),
59-
'port' => getenv('PORT'),
60-
'vhost' => '/',
61-
'user' => 'guest',
62-
'password' => 'guest',
63-
],
64-
],
65-
66-
'options' => [],
67-
68-
'worker' => 'octane',
69-
70-
]);
7151
}
7252

7353
protected function connection(string $name = null): RabbitMQQueue

0 commit comments

Comments
 (0)