Skip to content

Commit 66a1774

Browse files
authored
Merge pull request #585 from php-enqueue/redis
Redis New Implementation
2 parents 8e364ca + 5615d56 commit 66a1774

25 files changed

Lines changed: 880 additions & 176 deletions

‎docs/transport/redis.md‎

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ It creates a collection (a queue or topic) there. Pushes messages to the tail of
55
The transport works with [phpredis](https://github.com/phpredis/phpredis) php extension or [predis](https://github.com/nrk/predis) library.
66
Make sure you installed either of them
77

8-
**Limitations** It works only in auto ack mode hence If consumer crashes the message is lost.
9-
108
* [Installation](#installation)
119
* [Create context](#create-context)
1210
* [Send message to topic](#send-message-to-topic)
1311
* [Send message to queue](#send-message-to-queue)
12+
* [Send expiration message](#send-expiration-message)
13+
* [Send delayed message](#send-delayed-message)
1414
* [Consume message](#consume-message)
1515
* [Delete queue (purge messages)](#delete-queue-purge-messages)
1616
* [Delete topic (purge messages)](#delete-topic-purge-messages)
@@ -56,7 +56,7 @@ $factory = new RedisConnectionFactory([
5656
]);
5757

5858
// same as above but given as DSN string
59-
$factory = new RedisConnectionFactory('redis://example.com:1000?vendor=phpredis');
59+
$factory = new RedisConnectionFactory('redis+phpredis://example.com:1000');
6060

6161
$psrContext = $factory->createContext();
6262

@@ -68,7 +68,7 @@ $redis = new \Enqueue\Redis\PhpRedis([ /** redis connection options */ ]);
6868
$redis->connect();
6969

7070
// Secure\TLS connection. Works only with predis library. Note second "S" in scheme.
71-
$factory = new RedisConnectionFactory('rediss://user:pass@host/0?vendor=predis');
71+
$factory = new RedisConnectionFactory('rediss+predis://user:pass@host/0');
7272

7373
$factory = new RedisConnectionFactory($redis);
7474
```
@@ -82,7 +82,7 @@ use Enqueue\Redis\RedisConnectionFactory;
8282
$connectionFactory = new RedisConnectionFactory([
8383
'host' => 'localhost',
8484
'port' => 6379,
85-
'vendor' => 'predis',
85+
'scheme_extensions' => 'predis',
8686
]);
8787

8888
$psrContext = $connectionFactory->createContext();
@@ -102,7 +102,7 @@ $options = [];
102102

103103
$redis = new PRedis(new \PRedis\Client($config, $options));
104104

105-
$factory = new RedisConnectionFactory(['vendor' => 'custom', 'redis' => $redis]);
105+
$factory = new RedisConnectionFactory($redis);
106106
```
107107

108108
## Send message to topic
@@ -129,6 +129,38 @@ $message = $psrContext->createMessage('Hello world!');
129129
$psrContext->createProducer()->send($fooQueue, $message);
130130
```
131131

132+
## Send expiration message
133+
134+
```php
135+
<?php
136+
/** @var \Enqueue\Redis\RedisContext $psrContext */
137+
/** @var \Enqueue\Redis\RedisDestination $fooQueue */
138+
139+
$message = $psrContext->createMessage('Hello world!');
140+
141+
$psrContext->createProducer()
142+
->setTimeToLive(60000) // 60 sec
143+
//
144+
->send($fooQueue, $message)
145+
;
146+
```
147+
148+
## Send delayed message
149+
150+
```php
151+
<?php
152+
/** @var \Enqueue\Redis\RedisContext $psrContext */
153+
/** @var \Enqueue\Redis\RedisDestination $fooQueue */
154+
155+
$message = $psrContext->createMessage('Hello world!');
156+
157+
$psrContext->createProducer()
158+
->setDeliveryDelay(5000) // 5 sec
159+
160+
->send($fooQueue, $message)
161+
;
162+
````
163+
132164
## Consume message:
133165

134166
```php
@@ -141,6 +173,9 @@ $consumer = $psrContext->createConsumer($fooQueue);
141173
$message = $consumer->receive();
142174

143175
// process a message
176+
177+
$consumer->acknowledge($message);
178+
//$consumer->reject($message);
144179
```
145180

146181
## Delete queue (purge messages):

‎pkg/redis/JsonSerializer.php‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Enqueue\Redis;
6+
7+
class JsonSerializer implements Serializer
8+
{
9+
public function toString(RedisMessage $message): string
10+
{
11+
$json = json_encode([
12+
'body' => $message->getBody(),
13+
'properties' => $message->getProperties(),
14+
'headers' => $message->getHeaders(),
15+
]);
16+
17+
if (JSON_ERROR_NONE !== json_last_error()) {
18+
throw new \InvalidArgumentException(sprintf(
19+
'The malformed json given. Error %s and message %s',
20+
json_last_error(),
21+
json_last_error_msg()
22+
));
23+
}
24+
25+
return $json;
26+
}
27+
28+
public function toMessage(string $string): RedisMessage
29+
{
30+
$data = json_decode($string, true);
31+
if (JSON_ERROR_NONE !== json_last_error()) {
32+
throw new \InvalidArgumentException(sprintf(
33+
'The malformed json given. Error %s and message %s',
34+
json_last_error(),
35+
json_last_error_msg()
36+
));
37+
}
38+
39+
return new RedisMessage($data['body'], $data['properties'], $data['headers']);
40+
}
41+
}

‎pkg/redis/LuaScripts.php‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Enqueue\Redis;
6+
7+
class LuaScripts
8+
{
9+
/**
10+
* Get the Lua script to migrate expired messages back onto the queue.
11+
*
12+
* KEYS[1] - The queue we are removing messages from, for example: queues:foo:reserved
13+
* KEYS[2] - The queue we are moving messages to, for example: queues:foo
14+
* ARGV[1] - The current UNIX timestamp
15+
*
16+
* @return string
17+
*/
18+
public static function migrateExpired()
19+
{
20+
return <<<'LUA'
21+
-- Get all of the messages with an expired "score"...
22+
local val = redis.call('zrangebyscore', KEYS[1], '-inf', ARGV[1])
23+
24+
-- If we have values in the array, we will remove them from the first queue
25+
-- and add them onto the destination queue in chunks of 100, which moves
26+
-- all of the appropriate messages onto the destination queue very safely.
27+
if(next(val) ~= nil) then
28+
redis.call('zremrangebyrank', KEYS[1], 0, #val - 1)
29+
30+
for i = 1, #val, 100 do
31+
redis.call('lpush', KEYS[2], unpack(val, i, math.min(i+99, #val)))
32+
end
33+
end
34+
35+
return val
36+
LUA;
37+
}
38+
}

‎pkg/redis/PRedis.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,34 @@ public function __construct(array $config)
5353
}
5454
}
5555

56+
public function eval(string $script, array $keys = [], array $args = [])
57+
{
58+
try {
59+
// mixed eval($script, $numkeys, $keyOrArg1 = null, $keyOrArgN = null)
60+
return call_user_func_array([$this->redis, 'eval'], array_merge([$script, count($keys)], $keys, $args));
61+
} catch (PRedisServerException $e) {
62+
throw new ServerException('eval command has failed', null, $e);
63+
}
64+
}
65+
66+
public function zadd(string $key, string $value, float $score): int
67+
{
68+
try {
69+
return $this->redis->zadd($key, [$value => $score]);
70+
} catch (PRedisServerException $e) {
71+
throw new ServerException('zadd command has failed', null, $e);
72+
}
73+
}
74+
75+
public function zrem(string $key, string $value): int
76+
{
77+
try {
78+
return $this->redis->zrem($key, [$value]);
79+
} catch (PRedisServerException $e) {
80+
throw new ServerException('zrem command has failed', null, $e);
81+
}
82+
}
83+
5684
public function lpush(string $key, string $value): int
5785
{
5886
try {

‎pkg/redis/PhpRedis.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,33 @@ public function __construct(array $config)
2626
$this->config = $config;
2727
}
2828

29+
public function eval(string $script, array $keys = [], array $args = [])
30+
{
31+
try {
32+
return $this->redis->eval($script, array_merge($keys, $args), count($keys));
33+
} catch (\RedisException $e) {
34+
throw new ServerException('eval command has failed', null, $e);
35+
}
36+
}
37+
38+
public function zadd(string $key, string $value, float $score): int
39+
{
40+
try {
41+
return $this->redis->zAdd($key, $score, $value);
42+
} catch (\RedisException $e) {
43+
throw new ServerException('zadd command has failed', null, $e);
44+
}
45+
}
46+
47+
public function zrem(string $key, string $value): int
48+
{
49+
try {
50+
return $this->redis->zRem($key, $value);
51+
} catch (\RedisException $e) {
52+
throw new ServerException('zrem command has failed', null, $e);
53+
}
54+
}
55+
2956
public function lpush(string $key, string $value): int
3057
{
3158
try {

‎pkg/redis/Redis.php‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,38 @@
66

77
interface Redis
88
{
9+
/**
10+
* @param string $script
11+
* @param array $keys
12+
* @param array $args
13+
*
14+
* @throws ServerException
15+
*
16+
* @return mixed
17+
*/
18+
public function eval(string $script, array $keys = [], array $args = []);
19+
20+
/**
21+
* @param string $key
22+
* @param string $value
23+
* @param float $score
24+
*
25+
* @throws ServerException
26+
*
27+
* @return int
28+
*/
29+
public function zadd(string $key, string $value, float $score): int;
30+
31+
/**
32+
* @param string $key
33+
* @param string $value
34+
*
35+
* @throws ServerException
36+
*
37+
* @return int
38+
*/
39+
public function zrem(string $key, string $value): int;
40+
941
/**
1042
* @param string $key
1143
* @param string $value

‎pkg/redis/RedisConnectionFactory.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class RedisConnectionFactory implements ConnectionFactory
3838
* 'read_write_timeout' => Timeout (expressed in seconds) used when performing read or write operations on the underlying network resource after which an exception is thrown.
3939
* 'predis_options' => An array of predis specific options.
4040
* 'ssl' => could be any of http://fi2.php.net/manual/en/context.ssl.php#refsect1-context.ssl-options
41+
* 'redelivery_delay' => Default 300 sec. Returns back message into the queue if message was not acknowledged or rejected after this delay.
42+
* It could happen if consumer has failed with fatal error or even if message processing is slow and takes more than this time.
4143
* ].
4244
*
4345
* or
@@ -85,10 +87,10 @@ public function createContext(): Context
8587
if ($this->config['lazy']) {
8688
return new RedisContext(function () {
8789
return $this->createRedis();
88-
});
90+
}, $this->config['redelivery_delay']);
8991
}
9092

91-
return new RedisContext($this->createRedis());
93+
return new RedisContext($this->createRedis(), $this->config['redelivery_delay']);
9294
}
9395

9496
private function createRedis(): Redis
@@ -158,6 +160,7 @@ private function defaultConfig(): array
158160
'read_write_timeout' => null,
159161
'predis_options' => null,
160162
'ssl' => null,
163+
'redelivery_delay' => 300,
161164
];
162165
}
163166
}

0 commit comments

Comments
 (0)