|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bernard\Driver\MongoDB; |
|
4
|
|
|
|
|
5
|
|
|
use MongoCollection; |
|
6
|
|
|
use MongoDate; |
|
7
|
|
|
use MongoId; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Driver supporting MongoDB |
|
11
|
|
|
* |
|
12
|
|
|
* @package Bernard |
|
13
|
|
|
*/ |
|
14
|
|
|
final class Driver implements \Bernard\Driver |
|
15
|
|
|
{ |
|
16
|
|
|
private $messages; |
|
17
|
|
|
private $queues; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param MongoCollection $queues Collection where queues will be stored |
|
21
|
|
|
* @param MongoCollection $messages Collection where messages will be stored |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(MongoCollection $queues, MongoCollection $messages) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->queues = $queues; |
|
26
|
|
|
$this->messages = $messages; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
public function listQueues() |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->queues->distinct('_id'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function createQueue($queueName) |
|
41
|
|
|
{ |
|
42
|
|
|
$data = ['_id' => (string) $queueName]; |
|
43
|
|
|
|
|
44
|
|
|
$this->queues->update($data, $data, ['upsert' => true]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function countMessages($queueName) |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->messages->count([ |
|
53
|
|
|
'queue' => (string) $queueName, |
|
54
|
|
|
'visible' => true, |
|
55
|
|
|
]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function pushMessage($queueName, $message) |
|
62
|
|
|
{ |
|
63
|
|
|
$data = [ |
|
64
|
|
|
'queue' => (string) $queueName, |
|
65
|
|
|
'message' => (string) $message, |
|
66
|
|
|
'sentAt' => new MongoDate(), |
|
67
|
|
|
'visible' => true, |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
$this->messages->insert($data); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function popMessage($queueName, $duration = 5) |
|
77
|
|
|
{ |
|
78
|
|
|
$runtime = microtime(true) + $duration; |
|
79
|
|
|
|
|
80
|
|
|
while (microtime(true) < $runtime) { |
|
81
|
|
|
$result = $this->messages->findAndModify( |
|
82
|
|
|
['queue' => (string) $queueName, 'visible' => true], |
|
83
|
|
|
['$set' => ['visible' => false]], |
|
84
|
|
|
['message' => 1], |
|
85
|
|
|
['sort' => ['sentAt' => 1]] |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
if ($result) { |
|
|
|
|
|
|
89
|
|
|
return [(string) $result['message'], (string) $result['_id']]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
usleep(10000); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return [null, null]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
|
|
public function acknowledgeMessage($queueName, $receipt) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->messages->remove([ |
|
104
|
|
|
'_id' => new MongoId((string) $receipt), |
|
105
|
|
|
'queue' => (string) $queueName, |
|
106
|
|
|
]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
|
|
public function peekQueue($queueName, $index = 0, $limit = 20) |
|
113
|
|
|
{ |
|
114
|
|
|
$query = ['queue' => (string) $queueName, 'visible' => true]; |
|
115
|
|
|
$fields = ['_id' => 0, 'message' => 1]; |
|
116
|
|
|
|
|
117
|
|
|
$cursor = $this->messages |
|
118
|
|
|
->find($query, $fields) |
|
119
|
|
|
->sort(['sentAt' => 1]) |
|
120
|
|
|
->limit($limit) |
|
121
|
|
|
->skip($index) |
|
122
|
|
|
; |
|
123
|
|
|
|
|
124
|
|
|
$mapper = function ($result) { |
|
125
|
|
|
return (string) $result['message']; |
|
126
|
|
|
}; |
|
127
|
|
|
|
|
128
|
|
|
return array_map($mapper, iterator_to_array($cursor, false)); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* {@inheritdoc} |
|
133
|
|
|
*/ |
|
134
|
|
|
public function removeQueue($queueName) |
|
135
|
|
|
{ |
|
136
|
|
|
$this->queues->remove(['_id' => $queueName]); |
|
137
|
|
|
$this->messages->remove(['queue' => (string) $queueName]); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* {@inheritdoc} |
|
142
|
|
|
*/ |
|
143
|
|
|
public function info() |
|
144
|
|
|
{ |
|
145
|
|
|
return [ |
|
146
|
|
|
'messages' => (string) $this->messages, |
|
147
|
|
|
'queues' => (string) $this->queues, |
|
148
|
|
|
]; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.