Skip to content

Commit 9615f5d

Browse files
authored
Merge pull request #55 from php-enqueue/redis-transport
Redis transport.
2 parents 0daa2f5 + d53e052 commit 9615f5d

36 files changed

+2202
-12
lines changed

Diff for: bin/test

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function waitForService()
2222

2323
waitForService rabbitmq 5672 50
2424
waitForService mysql 3306 50
25+
waitForService redis 6379 50
2526

2627
php pkg/job-queue/Tests/Functional/app/console doctrine:database:create
2728
php pkg/job-queue/Tests/Functional/app/console doctrine:schema:update --force

Diff for: composer.json

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
"enqueue/enqueue": "*@dev",
99
"enqueue/stomp": "*@dev",
1010
"enqueue/amqp-ext": "*@dev",
11+
"enqueue/redis": "*@dev",
1112
"enqueue/fs": "*@dev",
1213
"enqueue/enqueue-bundle": "*@dev",
1314
"enqueue/job-queue": "*@dev",
14-
"enqueue/test": "*@dev"
15-
},
16-
"require-dev": {
15+
"enqueue/test": "*@dev",
16+
1717
"phpunit/phpunit": "^5",
1818
"doctrine/doctrine-bundle": "~1.2",
19+
"predis/predis": "^1.1",
1920
"symfony/monolog-bundle": "^2.8|^3",
2021
"symfony/browser-kit": "^2.8|^3",
2122
"symfony/expression-language": "^2.8|^3",
@@ -46,6 +47,10 @@
4647
"type": "path",
4748
"url": "pkg/amqp-ext"
4849
},
50+
{
51+
"type": "path",
52+
"url": "pkg/redis"
53+
},
4954
{
5055
"type": "path",
5156
"url": "pkg/enqueue-bundle"

Diff for: docker-compose.yml

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ services:
66
depends_on:
77
- rabbitmq
88
- mysql
9+
- redis
910
volumes:
10-
- ./:/mqdev
11+
- './:/mqdev'
1112
environment:
1213
- SYMFONY__RABBITMQ__HOST=rabbitmq
1314
- SYMFONY__RABBITMQ__USER=guest
@@ -21,6 +22,8 @@ services:
2122
- SYMFONY__DB__NAME=mqdev
2223
- SYMFONY__DB__USER=root
2324
- SYMFONY__DB__PASSWORD=rootpass
25+
- SYMFONY__REDIS__HOST=redis
26+
- SYMFONY__REDIS__PORT=6379
2427

2528
rabbitmq:
2629
image: enqueue/rabbitmq:latest
@@ -29,6 +32,11 @@ services:
2932
- RABBITMQ_DEFAULT_USER=guest
3033
- RABBITMQ_DEFAULT_PASS=guest
3134
- RABBITMQ_DEFAULT_VHOST=mqdev
35+
redis:
36+
image: 'redis:3'
37+
ports:
38+
- "6379:6379"
39+
3240
mysql:
3341
image: mariadb:10
3442
volumes:

Diff for: docs/filesystem_transport.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
Use files on local filesystem as queues.
44
It creates a file per queue\topic.
55
A message is a line inside the file.
6-
**Limitations** It works only in auto ack mode. Local by nature therefor messages are not visible on other servers.
6+
**Limitations** It works only in auto ack mode hence If consumer crashes the message is lost. Local by nature therefor messages are not visible on other servers.
77

88
* [Installation](#installation)
99
* [Create context](#create-context)
10-
* [Declare topic](#declare-topic)
11-
* [Declare queue](#decalre-queue)
12-
* [Bind queue to topic](#bind-queue-to-topic)
1310
* [Send message to topic](#send-message-to-topic)
1411
* [Send message to queue](#send-message-to-queue)
1512
* [Consume message](#consume-message)

Diff for: docs/index.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
* [Quick tour](quick_tour.md)
44
* Transports
5-
- [Amqp](amqp_transport.md)
6-
- [Stomp](stomp_transport.md)
5+
- [Amqp (RabbitMQ, ActiveMQ)](amqp_transport.md)
6+
- [Stomp (RabbitMQ, ActiveMQ)](stomp_transport.md)
7+
- [Redis](redis_transport.md)
78
- [Filesystem](filesystem_transport.md)
89
- [Null](null_transport.md)
910
* Consumption

Diff for: docs/redis_transport.md

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Redis transport
2+
3+
The transport uses [Redis](https://redis.io/) as a message broker.
4+
It creates a collection (a queue or topic) there. Pushes messages to the tail of the collection and pops from the head.
5+
The transport works with [phpredis](https://github.com/phpredis/phpredis) php extension or [predis](https://github.com/nrk/predis) library.
6+
Make sure you installed either of them
7+
8+
**Limitations** It works only in auto ack mode hence If consumer crashes the message is lost.
9+
10+
* [Installation](#installation)
11+
* [Create context](#create-context)
12+
* [Send message to topic](#send-message-to-topic)
13+
* [Send message to queue](#send-message-to-queue)
14+
* [Consume message](#consume-message)
15+
* [Delete queue (purge messages)](#delete-queue-purge-messages)
16+
* [Delete topic (purge messages)](#delete-topic-purge-messages)
17+
18+
## Installation
19+
20+
* With php redis extension:
21+
22+
```bash
23+
$ apt-get install php-redis
24+
$ composer require enqueue/redis
25+
```
26+
27+
* With predis library:
28+
29+
```bash
30+
$ composer require enqueue/redis predis/predis:^1
31+
```
32+
33+
## Create context
34+
35+
* With php redis extension:
36+
37+
```php
38+
<?php
39+
use Enqueue\Redis\RedisConnectionFactory;
40+
41+
$connectionFactory = new RedisConnectionFactory([
42+
'host' => 'localhost',
43+
'port' => 6379,
44+
'vendor' => 'phpredis',
45+
]);
46+
47+
$psrContext = $connectionFactory->createContext();
48+
```
49+
50+
* With predis library:
51+
52+
```php
53+
<?php
54+
use Enqueue\Redis\RedisConnectionFactory;
55+
56+
$connectionFactory = new RedisConnectionFactory([
57+
'host' => 'localhost',
58+
'port' => 6379,
59+
'vendor' => 'predis',
60+
]);
61+
62+
$psrContext = $connectionFactory->createContext();
63+
```
64+
65+
## Send message to topic
66+
67+
```php
68+
<?php
69+
/** @var \Enqueue\Redis\RedisContext $psrContext */
70+
71+
$fooTopic = $psrContext->createTopic('aTopic');
72+
$message = $psrContext->createMessage('Hello world!');
73+
74+
$psrContext->createProducer()->send($fooTopic, $message);
75+
```
76+
77+
## Send message to queue
78+
79+
```php
80+
<?php
81+
/** @var \Enqueue\Redis\RedisContext $psrContext */
82+
83+
$fooQueue = $psrContext->createQueue('aQueue');
84+
$message = $psrContext->createMessage('Hello world!');
85+
86+
$psrContext->createProducer()->send($fooQueue, $message);
87+
```
88+
89+
## Consume message:
90+
91+
```php
92+
<?php
93+
/** @var \Enqueue\Redis\RedisContext $psrContext */
94+
95+
$fooQueue = $psrContext->createQueue('aQueue');
96+
$consumer = $psrContext->createConsumer($fooQueue);
97+
98+
$message = $consumer->receive();
99+
100+
// process a message
101+
```
102+
103+
## Delete queue (purge messages):
104+
105+
```php
106+
<?php
107+
/** @var \Enqueue\Redis\RedisContext $psrContext */
108+
109+
$fooQueue = $psrContext->createQueue('aQueue');
110+
111+
$psrContext->deleteQueue($fooQueue);
112+
```
113+
114+
## Delete topic (purge messages):
115+
116+
```php
117+
<?php
118+
/** @var \Enqueue\Redis\RedisContext $psrContext */
119+
120+
$fooTopic = $psrContext->createTopic('aTopic');
121+
122+
$psrContext->deleteTopic($fooTopic);
123+
```
124+
125+
[back to index](index.md)

Diff for: phpunit.xml.dist

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929
<directory>pkg/amqp-ext/Tests</directory>
3030
</testsuite>
3131

32-
<testsuite name="fs">
32+
<testsuite name="fs transport">
3333
<directory>pkg/fs/Tests</directory>
3434
</testsuite>
3535

36+
<testsuite name="redis transport">
37+
<directory>pkg/redis/Tests</directory>
38+
</testsuite>
39+
3640
<testsuite name="enqueue-bundle">
3741
<directory>pkg/enqueue-bundle/Tests</directory>
3842
</testsuite>

Diff for: pkg/amqp-ext/AmqpConnectionFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class AmqpConnectionFactory implements PsrConnectionFactory
2626
* 'read_timeout' => Timeout in for income activity. Note: 0 or greater seconds. May be fractional.
2727
* 'write_timeout' => Timeout in for outcome activity. Note: 0 or greater seconds. May be fractional.
2828
* 'connect_timeout' => Connection timeout. Note: 0 or greater seconds. May be fractional.
29-
* 'persisted' => bool
29+
* 'persisted' => bool, Whether it use single persisted connection or open a new one for every context
30+
* 'lazy' => the connection will be performed as later as possible, if the option set to true
3031
* ].
3132
*
3233
* @param $config

Diff for: pkg/enqueue/Rpc/Promise.php

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function getMessage()
5050

5151
return $message;
5252
}
53+
5354
$this->consumer->reject($message, true);
5455
}
5556
}

Diff for: pkg/fs/FsConsumer.php

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\Fs;
44

5+
use Enqueue\Psr\InvalidMessageException;
56
use Enqueue\Psr\PsrConsumer;
67
use Enqueue\Psr\PsrMessage;
78

@@ -118,6 +119,8 @@ public function acknowledge(PsrMessage $message)
118119
*/
119120
public function reject(PsrMessage $message, $requeue = false)
120121
{
122+
InvalidMessageException::assertMessageInstanceOf($message, FsMessage::class);
123+
121124
// do nothing on reject. fs transport always works in auto ack mode
122125

123126
if ($requeue) {

Diff for: pkg/redis/.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*~
2+
/composer.lock
3+
/composer.phar
4+
/phpunit.xml
5+
/vendor/
6+
/.idea/

Diff for: pkg/redis/.travis.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
sudo: false
2+
3+
git:
4+
depth: 1
5+
6+
language: php
7+
8+
php:
9+
- '5.6'
10+
- '7.0'
11+
12+
cache:
13+
directories:
14+
- $HOME/.composer/cache
15+
16+
install:
17+
- composer self-update
18+
- composer install --prefer-source
19+
20+
script:
21+
- vendor/bin/phpunit --exclude-group=functional

Diff for: pkg/redis/LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2017 Forma-Pro
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is furnished
9+
to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.

0 commit comments

Comments
 (0)