1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bernard\Driver\PhpRedis; |
4
|
|
|
|
5
|
|
|
use Redis; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Implements a Driver for use with https://github.com/nicolasff/phpredis. |
9
|
|
|
*/ |
10
|
|
|
class Driver implements \Bernard\Driver |
11
|
|
|
{ |
12
|
|
|
const QUEUE_PREFIX = 'queue:'; |
13
|
|
|
|
14
|
|
|
protected $redis; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param Redis $redis |
18
|
|
|
*/ |
19
|
8 |
|
public function __construct(Redis $redis) |
20
|
|
|
{ |
21
|
8 |
|
$this->redis = $redis; |
22
|
8 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
2 |
|
public function listQueues() |
28
|
|
|
{ |
29
|
2 |
|
return $this->redis->sMembers('queues'); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
2 |
|
public function createQueue($queueName) |
36
|
|
|
{ |
37
|
2 |
|
$this->redis->sAdd('queues', $queueName); |
|
|
|
|
38
|
2 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
2 |
|
public function countMessages($queueName) |
44
|
|
|
{ |
45
|
2 |
|
return $this->redis->lLen(self::QUEUE_PREFIX.$queueName); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
2 |
|
public function pushMessage($queueName, $message) |
52
|
|
|
{ |
53
|
2 |
|
$this->redis->rpush($this->resolveKey($queueName), $message); |
|
|
|
|
54
|
2 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
1 |
|
public function popMessage($queueName, $duration = 5) |
60
|
|
|
{ |
61
|
|
|
// When PhpRedis is set up with an Redis::OPT_PREFIX |
62
|
|
|
// it does set the prefix to the key and to the timeout value something like: |
63
|
|
|
// "BLPOP" "bernard:queue:my-queue" "bernard:5" |
64
|
|
|
// |
65
|
|
|
// To set the resolved key in an array seems fixing this issue. We get: |
66
|
|
|
// "BLPOP" "bernard:queue:my-queue" "5" |
67
|
|
|
// |
68
|
|
|
// see https://github.com/nicolasff/phpredis/issues/158 |
69
|
1 |
|
list(, $message) = $this->redis->blpop([$this->resolveKey($queueName)], $duration) ?: null; |
|
|
|
|
70
|
|
|
|
71
|
1 |
|
return [$message, null]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
2 |
|
public function peekQueue($queueName, $index = 0, $limit = 20) |
78
|
|
|
{ |
79
|
2 |
|
$limit += $index - 1; |
80
|
|
|
|
81
|
2 |
|
return $this->redis->lRange($this->resolveKey($queueName), $index, $limit); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function acknowledgeMessage($queueName, $receipt) |
88
|
|
|
{ |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
2 |
|
public function removeQueue($queueName) |
95
|
|
|
{ |
96
|
2 |
|
$this->redis->sRem('queues', $queueName); |
|
|
|
|
97
|
2 |
|
$this->redis->del($this->resolveKey($queueName)); |
|
|
|
|
98
|
2 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function info() |
104
|
|
|
{ |
105
|
|
|
return $this->redis->info(); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Transform the queueName into a key. |
110
|
|
|
* |
111
|
|
|
* @param string $queueName |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
8 |
|
protected function resolveKey($queueName) |
116
|
|
|
{ |
117
|
8 |
|
return self::QUEUE_PREFIX.$queueName; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: