Skip to content

Commit e0cd279

Browse files
authored
feat: add RabbitMQHandler (#67)
* feat: RabbitMQ handler * better cleanup with tests * use AMQPConnectionConfig and AMQPConnectionFactory for connection handling
1 parent 0f95012 commit e0cd279

File tree

12 files changed

+1113
-3
lines changed

12 files changed

+1113
-3
lines changed

.github/workflows/phpunit.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ jobs:
117117
--health-timeout=5s
118118
--health-retries=3
119119
120+
rabbitmq:
121+
image: rabbitmq
122+
env:
123+
RABBITMQ_DEFAULT_USER: guest
124+
RABBITMQ_DEFAULT_PASS: guest
125+
ports:
126+
- 5672
127+
options: >-
128+
--health-cmd="rabbitmq-diagnostics -q ping"
129+
--health-interval=10s
130+
--health-timeout=5s
131+
--health-retries=5
132+
120133
steps:
121134
- name: Free Disk Space (Ubuntu)
122135
uses: jlumbroso/free-disk-space@main

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"codeigniter4/devkit": "^1.0",
2020
"codeigniter4/framework": "^4.3",
2121
"predis/predis": "^2.0",
22-
"phpstan/phpstan-strict-rules": "^1.5"
22+
"phpstan/phpstan-strict-rules": "^1.5",
23+
"php-amqplib/php-amqplib": "^3.7"
2324
},
2425
"minimum-stability": "dev",
2526
"prefer-stable": true,
@@ -38,7 +39,8 @@
3839
},
3940
"suggest": {
4041
"ext-redis": "If you want to use RedisHandler",
41-
"predis/predis": "If you want to use PredisHandler"
42+
"predis/predis": "If you want to use PredisHandler",
43+
"php-amqplib/php-amqplib": "If you want to use RabbitMQHandler"
4244
},
4345
"config": {
4446
"allow-plugins": {

docs/configuration.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Available options:
1717
- [$database](#database)
1818
- [$redis](#redis)
1919
- [$predis](#predis)
20+
- [$rabbitmq](#rabbitmq)
2021
- [$keepDoneJobs](#keepdonejobs)
2122
- [$keepFailedJobs](#keepfailedjobs)
2223
- [$queueDefaultPriority](#queuedefaultpriority)
@@ -29,7 +30,7 @@ The default handler used by the library. Default value: `database`.
2930

3031
### $handlers
3132

32-
An array of available handlers. By now only `database`, `redis` and `predis` handlers are implemented.
33+
An array of available handlers. Available handlers: `database`, `redis`, `predis`, and `rabbitmq`.
3334

3435
### $database
3536

@@ -66,6 +67,16 @@ The configuration settings for `predis` handler. You need to have [Predis](https
6667
* `database` - The database number. Default value: `0`.
6768
* `prefix` - The default key prefix. Default value: `''` (not set).
6869

70+
### $rabbitmq
71+
72+
The configuration settings for `rabbitmq` handler. You need to have [php-amqplib](https://github.com/php-amqplib/php-amqplib) installed to use it.
73+
74+
* `host` - The RabbitMQ server host. Default value: `127.0.0.1`.
75+
* `port` - The port number. Default value: `5672`.
76+
* `username` - The username for authentication. Default value: `guest`.
77+
* `password` - The password for authentication. Default value: `guest`.
78+
* `vhost` - The virtual host to use. Default value: `/`.
79+
6980
### $keepDoneJobs
7081

7182
If the job is done, should we keep it in the table? Default value: `false`.

docs/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ If you use `Redis` (you still need a relational database to store failed jobs):
3535
- PHPRedis
3636
- Predis
3737

38+
If you use `RabbitMQ` (you still need a relational database to store failed jobs):
39+
40+
- php-amqplib
41+
3842
### Table of Contents
3943

4044
* [Installation](installation.md)

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ parameters:
1515
paths:
1616
- src/Handlers/RedisHandler.php
1717
- src/Handlers/PredisHandler.php
18+
- src/Handlers/RabbitMQHandler.php
1819
-
1920
message: '#Call to an undefined method CodeIgniter\\Queue\\Models\\QueueJobFailedModel::affectedRows\(\).#'
2021
paths:

src/Config/Queue.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Queue\Exceptions\QueueException;
1818
use CodeIgniter\Queue\Handlers\DatabaseHandler;
1919
use CodeIgniter\Queue\Handlers\PredisHandler;
20+
use CodeIgniter\Queue\Handlers\RabbitMQHandler;
2021
use CodeIgniter\Queue\Handlers\RedisHandler;
2122
use CodeIgniter\Queue\Interfaces\JobInterface;
2223
use CodeIgniter\Queue\Interfaces\QueueInterface;
@@ -37,6 +38,7 @@ class Queue extends BaseConfig
3738
'database' => DatabaseHandler::class,
3839
'redis' => RedisHandler::class,
3940
'predis' => PredisHandler::class,
41+
'rabbitmq' => RabbitMQHandler::class,
4042
];
4143

4244
/**
@@ -75,6 +77,17 @@ class Queue extends BaseConfig
7577
'prefix' => '',
7678
];
7779

80+
/**
81+
* RabbitMQ handler config.
82+
*/
83+
public array $rabbitmq = [
84+
'host' => '127.0.0.1',
85+
'port' => 5672,
86+
'user' => 'guest',
87+
'password' => 'guest',
88+
'vhost' => '/',
89+
];
90+
7891
/**
7992
* Whether to keep the DONE jobs in the queue.
8093
*/

src/Exceptions/QueueException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ public static function forIncorrectDelayValue(): static
5656
{
5757
return new self(lang('Queue.incorrectDelayValue'));
5858
}
59+
60+
public static function forFailedJsonEncode(string $error): static
61+
{
62+
return new self(lang('Queue.failedToJsonEncode', [$error]));
63+
}
5964
}

0 commit comments

Comments
 (0)