Skip to content

Commit 9c0d172

Browse files
authored
Merge pull request #528 from vyuldashev/fix/refactor-creation-of-connections-and-queues
Fix/refactor creation of RabbitMQ Connection and QueueAPI
2 parents a83eff9 + b43ac84 commit 9c0d172

16 files changed

+1259
-398
lines changed

README.md

Lines changed: 165 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ RabbitMQ Queue driver for Laravel
33
[![Latest Stable Version](https://poser.pugx.org/vladimir-yuldashev/laravel-queue-rabbitmq/v/stable?format=flat-square)](https://packagist.org/packages/vladimir-yuldashev/laravel-queue-rabbitmq)
44
[![Build Status](https://github.com/vyuldashev/laravel-queue-rabbitmq/workflows/Tests/badge.svg)](https://github.com/vyuldashev/laravel-queue-rabbitmq/actions)
55
[![Total Downloads](https://poser.pugx.org/vladimir-yuldashev/laravel-queue-rabbitmq/downloads?format=flat-square)](https://packagist.org/packages/vladimir-yuldashev/laravel-queue-rabbitmq)
6-
[![StyleCI](https://styleci.io/repos/14976752/shield)](https://styleci.io/repos/14976752)
76
[![License](https://poser.pugx.org/vladimir-yuldashev/laravel-queue-rabbitmq/license?format=flat-square)](https://packagist.org/packages/vladimir-yuldashev/laravel-queue-rabbitmq)
87

98
## Support Policy
@@ -24,18 +23,19 @@ composer require vladimir-yuldashev/laravel-queue-rabbitmq
2423

2524
The package will automatically register itself.
2625

26+
### Configuration
27+
2728
Add connection to `config/queue.php`:
2829

30+
> This is the minimal config for the rabbitMQ connection/driver to work.
31+
2932
```php
3033
'connections' => [
3134
// ...
3235

3336
'rabbitmq' => [
3437

3538
'driver' => 'rabbitmq',
36-
'queue' => env('RABBITMQ_QUEUE', 'default'),
37-
'connection' => PhpAmqpLib\Connection\AMQPLazyConnection::class,
38-
3939
'hosts' => [
4040
[
4141
'host' => env('RABBITMQ_HOST', '127.0.0.1'),
@@ -44,33 +44,17 @@ Add connection to `config/queue.php`:
4444
'password' => env('RABBITMQ_PASSWORD', 'guest'),
4545
'vhost' => env('RABBITMQ_VHOST', '/'),
4646
],
47+
// ...
4748
],
48-
49-
'options' => [
50-
'ssl_options' => [
51-
'cafile' => env('RABBITMQ_SSL_CAFILE', null),
52-
'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null),
53-
'local_key' => env('RABBITMQ_SSL_LOCALKEY', null),
54-
'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
55-
'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
56-
],
57-
'queue' => [
58-
'job' => VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob::class,
59-
],
60-
],
61-
62-
/*
63-
* Set to "horizon" if you wish to use Laravel Horizon.
64-
*/
65-
'worker' => env('RABBITMQ_WORKER', 'default'),
66-
'after_commit' => false,
49+
50+
// ...
6751
],
6852

6953
// ...
7054
],
7155
```
7256

73-
### Optional Config
57+
### Optional Queue Config
7458

7559
Optionally add queue options to the config of a connection.
7660
Every queue created for this connection, gets the properties.
@@ -164,6 +148,30 @@ by adding extra options.
164148
],
165149
```
166150

151+
### Horizon support
152+
Starting with 8.0, this package supports [Laravel Horizon](http://horizon.laravel.com) out of the box. Firstly, install
153+
Horizon and then set `RABBITMQ_WORKER` to `horizon`.
154+
155+
Horizon is depending on events dispatched by the worker.
156+
These events inform Horizon what was done with the message/job.
157+
158+
This Library supports Horizon, but in the config you have to inform Laravel to use the QueueApi compatible with horizon.
159+
160+
```php
161+
'connections' => [
162+
// ...
163+
164+
'rabbitmq' => [
165+
// ...
166+
167+
/* Set to "horizon" if you wish to use Laravel Horizon. */
168+
'worker' => env('RABBITMQ_WORKER', 'default'),
169+
],
170+
171+
// ...
172+
],
173+
```
174+
167175
### Use your own RabbitMQJob class
168176

169177
Sometimes you have to work with messages published by another application.
@@ -254,17 +262,143 @@ class RabbitMQJob extends BaseJob
254262
}
255263
```
256264

265+
### Use your own Connection
266+
267+
You can extend the built-in `PhpAmqpLib\Connection\AMQPStreamConnection::class`
268+
or `PhpAmqpLib\Connection\AMQPSLLConnection::class` and within the connection config, you can define your own class.
269+
When you specify a `connection` key in the config, with your own class name, every connection will use your own class.
270+
271+
An example for the config:
272+
273+
```php
274+
'connections' => [
275+
// ...
276+
277+
'rabbitmq' => [
278+
// ...
279+
280+
'connection' = > \App\Queue\Connection\MyRabbitMQConnection::class,
281+
],
282+
283+
// ...
284+
],
285+
```
286+
287+
### Default Queue
288+
289+
The connection does use a default queue with value 'default', when no queue is provided by laravel.
290+
It is possible to change te default queue by adding an extra parameter in the connection config.
291+
292+
```php
293+
'connections' => [
294+
// ...
295+
296+
'rabbitmq' => [
297+
// ...
298+
299+
'queue' => env('RABBITMQ_QUEUE', 'default'),
300+
],
301+
302+
// ...
303+
],
304+
```
305+
306+
### Heartbeat
307+
308+
By default, your connection will be created with a heartbeat setting of `0`.
309+
You can alter the heartbeat settings by changing the config.
310+
311+
```php
312+
313+
'connections' => [
314+
// ...
315+
316+
'rabbitmq' => [
317+
// ...
318+
319+
'options' => [
320+
// ...
321+
322+
'heartbeat' => 10,
323+
],
324+
],
325+
326+
// ...
327+
],
328+
```
329+
330+
### SSL Secure
331+
332+
If you need a secure connection to rabbitMQ server(s), you will need to add these extra config options.
333+
334+
```php
335+
'connections' => [
336+
// ...
337+
338+
'rabbitmq' => [
339+
// ...
340+
341+
'secure' = > true,
342+
'options' => [
343+
// ...
344+
345+
'ssl_options' => [
346+
'cafile' => env('RABBITMQ_SSL_CAFILE', null),
347+
'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null),
348+
'local_key' => env('RABBITMQ_SSL_LOCALKEY', null),
349+
'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
350+
'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
351+
],
352+
],
353+
],
354+
355+
// ...
356+
],
357+
```
358+
359+
### Events after Database commits
360+
361+
To instruct Laravel workers to dispatch events after all database commits are completed.
362+
363+
```php
364+
'connections' => [
365+
// ...
366+
367+
'rabbitmq' => [
368+
// ...
369+
370+
'after_commit' => true,
371+
],
372+
373+
// ...
374+
],
375+
```
376+
377+
### Lazy Connection
378+
379+
By default, your connection will be created as a lazy connection.
380+
If for some reason you don't want the connection lazy you can turn it off by setting the following config.
381+
382+
```php
383+
'connections' => [
384+
// ...
385+
386+
'rabbitmq' => [
387+
// ...
388+
389+
'lazy' = > false,
390+
],
391+
392+
// ...
393+
],
394+
```
395+
257396
## Laravel Usage
258397

259398
Once you completed the configuration you can use the Laravel Queue API. If you used other queue drivers you do not need to
260399
change anything else. If you do not know how to use the Queue API, please refer to the official Laravel
261400
documentation: http://laravel.com/docs/queues
262401

263-
## Laravel Horizon Usage
264-
265-
Starting with 8.0, this package supports [Laravel Horizon](http://horizon.laravel.com) out of the box. Firstly, install
266-
Horizon and then set `RABBITMQ_WORKER` to `horizon`.
267-
268402
## Lumen Usage
269403

270404
For Lumen usage the service provider should be registered manually as follow in `bootstrap/app.php`:
@@ -287,7 +421,7 @@ There are two ways of consuming messages.
287421
Setup RabbitMQ using `docker-compose`:
288422

289423
```bash
290-
docker-compose up -d rabbitmq
424+
docker compose up -d
291425
```
292426

293427
To run the test suite you can use the following commands:
@@ -304,7 +438,7 @@ composer test:unit
304438
```
305439

306440
If you receive any errors from the style tests, you can automatically fix most,
307-
if not all of the issues with the following command:
441+
if not all the issues with the following command:
308442

309443
```bash
310444
composer fix:style

composer.json

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
11
{
2-
"name": "vladimir-yuldashev/laravel-queue-rabbitmq",
3-
"description": "RabbitMQ driver for Laravel Queue. Supports Laravel Horizon.",
4-
"license": "MIT",
5-
"authors": [
6-
{
7-
"name": "Vladimir Yuldashev",
8-
"email": "[email protected]"
9-
}
10-
],
11-
"require": {
12-
"php": "^8.0",
13-
"ext-json": "*",
14-
"illuminate/queue": "^9.0|^10.0",
15-
"php-amqplib/php-amqplib": "^3.0"
16-
},
17-
"require-dev": {
18-
"phpunit/phpunit": "^9.3",
19-
"mockery/mockery": "^1.0",
20-
"laravel/horizon": "^5.0",
21-
"orchestra/testbench": "^7.0|^8.0",
22-
"laravel/pint": "^1.2",
23-
"laravel/framework": "^9.0|^10.0"
24-
},
25-
"autoload": {
26-
"psr-4": {
27-
"VladimirYuldashev\\LaravelQueueRabbitMQ\\": "src/"
28-
}
29-
},
30-
"autoload-dev": {
31-
"psr-4": {
32-
"VladimirYuldashev\\LaravelQueueRabbitMQ\\Tests\\": "tests/"
33-
}
34-
},
35-
"extra": {
36-
"branch-alias": {
37-
"dev-master": "13.0-dev"
38-
},
39-
"laravel": {
40-
"providers": [
41-
"VladimirYuldashev\\LaravelQueueRabbitMQ\\LaravelQueueRabbitMQServiceProvider"
42-
]
43-
}
44-
},
45-
"suggest": {
46-
"ext-pcntl": "Required to use all features of the queue consumer."
47-
},
48-
"scripts": {
49-
"test": [
50-
"@test:style",
51-
"@test:unit"
2+
"name": "vladimir-yuldashev/laravel-queue-rabbitmq",
3+
"description": "RabbitMQ driver for Laravel Queue. Supports Laravel Horizon.",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Vladimir Yuldashev",
8+
"email": "[email protected]"
9+
}
5210
],
53-
"test:style": "@php vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --allow-risky=yes --dry-run --diff --verbose",
54-
"test:unit": "@php vendor/bin/phpunit",
55-
"fix:style": "@php vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --allow-risky=yes --diff --verbose"
56-
},
57-
"minimum-stability": "dev",
58-
"prefer-stable": true
11+
"require": {
12+
"php": "^8.0",
13+
"ext-json": "*",
14+
"illuminate/queue": "^9.0|^10.0",
15+
"php-amqplib/php-amqplib": "^v3.2"
16+
},
17+
"require-dev": {
18+
"phpunit/phpunit": "^9.3",
19+
"mockery/mockery": "^1.0",
20+
"laravel/horizon": "^5.0",
21+
"orchestra/testbench": "^7.0|^8.0",
22+
"laravel/pint": "^1.2",
23+
"laravel/framework": "^9.0|^10.0"
24+
},
25+
"autoload": {
26+
"psr-4": {
27+
"VladimirYuldashev\\LaravelQueueRabbitMQ\\": "src/"
28+
}
29+
},
30+
"autoload-dev": {
31+
"psr-4": {
32+
"VladimirYuldashev\\LaravelQueueRabbitMQ\\Tests\\": "tests/"
33+
}
34+
},
35+
"extra": {
36+
"branch-alias": {
37+
"dev-master": "13.0-dev"
38+
},
39+
"laravel": {
40+
"providers": [
41+
"VladimirYuldashev\\LaravelQueueRabbitMQ\\LaravelQueueRabbitMQServiceProvider"
42+
]
43+
}
44+
},
45+
"suggest": {
46+
"ext-pcntl": "Required to use all features of the queue consumer."
47+
},
48+
"scripts": {
49+
"test": [
50+
"@test:style",
51+
"@test:unit"
52+
],
53+
"test:style": "@php vendor/bin/pint --test -v",
54+
"test:unit": "@php vendor/bin/phpunit",
55+
"fix:style": "@php vendor/bin/pint -v"
56+
},
57+
"minimum-stability": "dev",
58+
"prefer-stable": true
5959
}

0 commit comments

Comments
 (0)