Skip to content

Commit ee24fb1

Browse files
committed
rework
- removed octane shizzle
1 parent 7690c9c commit ee24fb1

File tree

8 files changed

+103
-135
lines changed

8 files changed

+103
-135
lines changed

README.md

Lines changed: 98 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ by adding extra options.
151151
### Horizon support
152152

153153
Starting with 8.0, this package supports [Laravel Horizon](https://laravel.com/docs/horizon) out of the box. Firstly,
154-
install
155-
Horizon and then set `RABBITMQ_WORKER` to `horizon`.
154+
install Horizon and then set `RABBITMQ_WORKER` to `horizon`.
156155

157156
Horizon is depending on events dispatched by the worker.
158157
These events inform Horizon what was done with the message/job.
@@ -174,30 +173,6 @@ This Library supports Horizon, but in the config you have to inform Laravel to u
174173
],
175174
```
176175

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-
201176
### Use your own RabbitMQJob class
202177

203178
Sometimes you have to work with messages published by another application.
@@ -288,7 +263,9 @@ class RabbitMQJob extends BaseJob
288263
}
289264
```
290265

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:
266+
If you want to handle raw message, not in JSON format or without 'job' key in JSON,
267+
you should add stub for `getName` method:
268+
292269
```php
293270
<?php
294271

@@ -336,6 +313,93 @@ An example for the config:
336313
],
337314
```
338315

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

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

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

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
519+
Once you completed the configuration you can use the Laravel Queue API. If you used other queue drivers you do not
520+
need to change anything else. If you do not know how to use the Queue API, please refer to the official Laravel
452521
documentation: http://laravel.com/docs/queues
453522

454523
## 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)