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