Skip to content

Fix/refactor creation of RabbitMQ Connection and QueueAPI #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 165 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ RabbitMQ Queue driver for Laravel
[![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)
[![Build Status](https://github.com/vyuldashev/laravel-queue-rabbitmq/workflows/Tests/badge.svg)](https://github.com/vyuldashev/laravel-queue-rabbitmq/actions)
[![Total Downloads](https://poser.pugx.org/vladimir-yuldashev/laravel-queue-rabbitmq/downloads?format=flat-square)](https://packagist.org/packages/vladimir-yuldashev/laravel-queue-rabbitmq)
[![StyleCI](https://styleci.io/repos/14976752/shield)](https://styleci.io/repos/14976752)
[![License](https://poser.pugx.org/vladimir-yuldashev/laravel-queue-rabbitmq/license?format=flat-square)](https://packagist.org/packages/vladimir-yuldashev/laravel-queue-rabbitmq)

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

The package will automatically register itself.

### Configuration

Add connection to `config/queue.php`:

> This is the minimal config for the rabbitMQ connection/driver to work.

```php
'connections' => [
// ...

'rabbitmq' => [

'driver' => 'rabbitmq',
'queue' => env('RABBITMQ_QUEUE', 'default'),
'connection' => PhpAmqpLib\Connection\AMQPLazyConnection::class,

'hosts' => [
[
'host' => env('RABBITMQ_HOST', '127.0.0.1'),
Expand All @@ -44,33 +44,17 @@ Add connection to `config/queue.php`:
'password' => env('RABBITMQ_PASSWORD', 'guest'),
'vhost' => env('RABBITMQ_VHOST', '/'),
],
// ...
],

'options' => [
'ssl_options' => [
'cafile' => env('RABBITMQ_SSL_CAFILE', null),
'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null),
'local_key' => env('RABBITMQ_SSL_LOCALKEY', null),
'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
],
'queue' => [
'job' => VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob::class,
],
],

/*
* Set to "horizon" if you wish to use Laravel Horizon.
*/
'worker' => env('RABBITMQ_WORKER', 'default'),
'after_commit' => false,

// ...
],

// ...
],
```

### Optional Config
### Optional Queue Config

Optionally add queue options to the config of a connection.
Every queue created for this connection, gets the properties.
Expand Down Expand Up @@ -164,6 +148,30 @@ by adding extra options.
],
```

### Horizon support
Starting with 8.0, this package supports [Laravel Horizon](http://horizon.laravel.com) out of the box. Firstly, install
Horizon and then set `RABBITMQ_WORKER` to `horizon`.

Horizon is depending on events dispatched by the worker.
These events inform Horizon what was done with the message/job.

This Library supports Horizon, but in the config you have to inform Laravel to use the QueueApi compatible with horizon.

```php
'connections' => [
// ...

'rabbitmq' => [
// ...

/* Set to "horizon" if you wish to use Laravel Horizon. */
'worker' => env('RABBITMQ_WORKER', 'default'),
],

// ...
],
```

### Use your own RabbitMQJob class

Sometimes you have to work with messages published by another application.
Expand Down Expand Up @@ -254,17 +262,143 @@ class RabbitMQJob extends BaseJob
}
```

### Use your own Connection

You can extend the built-in `PhpAmqpLib\Connection\AMQPStreamConnection::class`
or `PhpAmqpLib\Connection\AMQPSLLConnection::class` and within the connection config, you can define your own class.
When you specify a `connection` key in the config, with your own class name, every connection will use your own class.

An example for the config:

```php
'connections' => [
// ...

'rabbitmq' => [
// ...

'connection' = > \App\Queue\Connection\MyRabbitMQConnection::class,
],

// ...
],
```

### Default Queue

The connection does use a default queue with value 'default', when no queue is provided by laravel.
It is possible to change te default queue by adding an extra parameter in the connection config.

```php
'connections' => [
// ...

'rabbitmq' => [
// ...

'queue' => env('RABBITMQ_QUEUE', 'default'),
],

// ...
],
```

### Heartbeat

By default, your connection will be created with a heartbeat setting of `0`.
You can alter the heartbeat settings by changing the config.

```php

'connections' => [
// ...

'rabbitmq' => [
// ...

'options' => [
// ...

'heartbeat' => 10,
],
],

// ...
],
```

### SSL Secure

If you need a secure connection to rabbitMQ server(s), you will need to add these extra config options.

```php
'connections' => [
// ...

'rabbitmq' => [
// ...

'secure' = > true,
'options' => [
// ...

'ssl_options' => [
'cafile' => env('RABBITMQ_SSL_CAFILE', null),
'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null),
'local_key' => env('RABBITMQ_SSL_LOCALKEY', null),
'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
],
],
],

// ...
],
```

### Events after Database commits

To instruct Laravel workers to dispatch events after all database commits are completed.

```php
'connections' => [
// ...

'rabbitmq' => [
// ...

'after_commit' => true,
],

// ...
],
```

### Lazy Connection

By default, your connection will be created as a lazy connection.
If for some reason you don't want the connection lazy you can turn it off by setting the following config.

```php
'connections' => [
// ...

'rabbitmq' => [
// ...

'lazy' = > false,
],

// ...
],
```

## Laravel Usage

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

## Laravel Horizon Usage

Starting with 8.0, this package supports [Laravel Horizon](http://horizon.laravel.com) out of the box. Firstly, install
Horizon and then set `RABBITMQ_WORKER` to `horizon`.

## Lumen Usage

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

```bash
docker-compose up -d rabbitmq
docker compose up -d
```

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

If you receive any errors from the style tests, you can automatically fix most,
if not all of the issues with the following command:
if not all the issues with the following command:

```bash
composer fix:style
Expand Down
112 changes: 56 additions & 56 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
{
"name": "vladimir-yuldashev/laravel-queue-rabbitmq",
"description": "RabbitMQ driver for Laravel Queue. Supports Laravel Horizon.",
"license": "MIT",
"authors": [
{
"name": "Vladimir Yuldashev",
"email": "misterio92@gmail.com"
}
],
"require": {
"php": "^8.0",
"ext-json": "*",
"illuminate/queue": "^9.0|^10.0",
"php-amqplib/php-amqplib": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^9.3",
"mockery/mockery": "^1.0",
"laravel/horizon": "^5.0",
"orchestra/testbench": "^7.0|^8.0",
"laravel/pint": "^1.2",
"laravel/framework": "^9.0|^10.0"
},
"autoload": {
"psr-4": {
"VladimirYuldashev\\LaravelQueueRabbitMQ\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"VladimirYuldashev\\LaravelQueueRabbitMQ\\Tests\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "13.0-dev"
},
"laravel": {
"providers": [
"VladimirYuldashev\\LaravelQueueRabbitMQ\\LaravelQueueRabbitMQServiceProvider"
]
}
},
"suggest": {
"ext-pcntl": "Required to use all features of the queue consumer."
},
"scripts": {
"test": [
"@test:style",
"@test:unit"
"name": "vladimir-yuldashev/laravel-queue-rabbitmq",
"description": "RabbitMQ driver for Laravel Queue. Supports Laravel Horizon.",
"license": "MIT",
"authors": [
{
"name": "Vladimir Yuldashev",
"email": "misterio92@gmail.com"
}
],
"test:style": "@php vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --allow-risky=yes --dry-run --diff --verbose",
"test:unit": "@php vendor/bin/phpunit",
"fix:style": "@php vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --allow-risky=yes --diff --verbose"
},
"minimum-stability": "dev",
"prefer-stable": true
"require": {
"php": "^8.0",
"ext-json": "*",
"illuminate/queue": "^9.0|^10.0",
"php-amqplib/php-amqplib": "^v3.2"
},
"require-dev": {
"phpunit/phpunit": "^9.3",
"mockery/mockery": "^1.0",
"laravel/horizon": "^5.0",
"orchestra/testbench": "^7.0|^8.0",
"laravel/pint": "^1.2",
"laravel/framework": "^9.0|^10.0"
},
"autoload": {
"psr-4": {
"VladimirYuldashev\\LaravelQueueRabbitMQ\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"VladimirYuldashev\\LaravelQueueRabbitMQ\\Tests\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "13.0-dev"
},
"laravel": {
"providers": [
"VladimirYuldashev\\LaravelQueueRabbitMQ\\LaravelQueueRabbitMQServiceProvider"
]
}
},
"suggest": {
"ext-pcntl": "Required to use all features of the queue consumer."
},
"scripts": {
"test": [
"@test:style",
"@test:unit"
],
"test:style": "@php vendor/bin/pint --test -v",
"test:unit": "@php vendor/bin/phpunit",
"fix:style": "@php vendor/bin/pint -v"
},
"minimum-stability": "dev",
"prefer-stable": true
}
Loading