|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Drupal\mongodb\Queue; |
|
5
|
|
|
|
|
6
|
|
|
use Drupal\Core\Queue\QueueInterface; |
|
7
|
|
|
use Drupal\mongodb\MongoDb; |
|
8
|
|
|
use MongoDB\Database; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Mongodb queue implementation. |
|
12
|
|
|
* |
|
13
|
|
|
* @ingroup queue |
|
14
|
|
|
*/ |
|
15
|
|
|
class MongodbQueue implements QueueInterface { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The queue storage. |
|
19
|
|
|
* |
|
20
|
|
|
* @var \MongoDB\Database |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $database; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The collection name for the queue. |
|
26
|
|
|
* |
|
27
|
|
|
* @var \MongoDB\Collection[] $collection |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $collection; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Constructs a \Drupal\mongodb\Queue\MongodbQueue object. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $name |
|
35
|
|
|
* The name of the queue. |
|
36
|
|
|
* @param array $settings |
|
37
|
|
|
* Array of Mongodb-related settings for this queue. |
|
38
|
|
|
* @param \MongoDB\Database $database |
|
39
|
|
|
* The database object. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($name, $settings, Database $database) { |
|
42
|
|
|
$this->name = $name; |
|
43
|
|
|
$this->database = $database; |
|
44
|
|
|
$this->collection = $this->database |
|
|
|
|
|
|
45
|
|
|
->selectCollection($name); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function createItem($data) { |
|
52
|
|
|
try { |
|
53
|
|
|
$id = $this->doCreateItem($data); |
|
54
|
|
|
} |
|
55
|
|
|
catch (\Exception $e) { |
|
56
|
|
|
throw $e; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $id; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Adds a queue item and store it directly to the queue. |
|
64
|
|
|
* |
|
65
|
|
|
* @param $data |
|
66
|
|
|
* Arbitrary data to be associated with the new task in the queue. |
|
67
|
|
|
* |
|
68
|
|
|
* @return |
|
69
|
|
|
* A unique ID if the item was successfully created and was (best effort) |
|
70
|
|
|
* added to the queue, otherwise FALSE. We don't guarantee the item was |
|
71
|
|
|
* committed to disk etc, but as far as we know, the item is now in the |
|
72
|
|
|
* queue. |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function doCreateItem($data) { |
|
75
|
|
|
|
|
76
|
|
|
$item = [ |
|
77
|
|
|
'name' => $this->name, |
|
78
|
|
|
'data' => $data, |
|
79
|
|
|
'expire' => 0, |
|
80
|
|
|
'created' => time(), |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
$result = $this->collection->insertOne($item); |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
return $result->getInsertedId(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
|
|
public function numberOfItems() { |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
try { |
|
94
|
|
|
return (int) MongoDb::countCollection($this->collection); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
catch (\Exception $e) { |
|
97
|
|
|
throw $e; |
|
98
|
|
|
// If there is no collection there cannot be any items. |
|
99
|
|
|
return 0; |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
*/ |
|
106
|
|
|
public function claimItem($lease_time = 30) { |
|
|
|
|
|
|
107
|
|
|
$newobj = [ |
|
108
|
|
|
'expire' => time() + $lease_time, |
|
109
|
|
|
]; |
|
110
|
|
|
return $this->collection->findOneAndUpdate( |
|
|
|
|
|
|
111
|
|
|
[], |
|
112
|
|
|
[ '$set' => $newobj ], |
|
113
|
|
|
[ 'sort' => ['created' => 1],] |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* {@inheritdoc} |
|
121
|
|
|
*/ |
|
122
|
|
|
public function releaseItem($item) { |
|
|
|
|
|
|
123
|
|
|
return $this->collection |
|
|
|
|
|
|
124
|
|
|
->updateOne( |
|
125
|
|
|
['_id' => $item->_id], |
|
126
|
|
|
['$set' => |
|
127
|
|
|
[ |
|
128
|
|
|
'expire' => 0 |
|
129
|
|
|
], |
|
130
|
|
|
] |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* {@inheritdoc} |
|
136
|
|
|
*/ |
|
137
|
|
|
public function deleteItem($item) { |
|
138
|
|
|
try { |
|
139
|
|
|
$this->collection |
|
|
|
|
|
|
140
|
|
|
->deleteOne( |
|
141
|
|
|
[ |
|
142
|
|
|
'_id' => $item->_id |
|
143
|
|
|
] |
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
catch (\Exception $e) { |
|
147
|
|
|
throw $e; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* {@inheritdoc} |
|
153
|
|
|
*/ |
|
154
|
|
|
public function createQueue() { |
|
155
|
|
|
// Create the index. |
|
156
|
|
|
$this->collection->createIndex([ |
|
|
|
|
|
|
157
|
|
|
'expire' => 1, |
|
158
|
|
|
'created' => 1 |
|
159
|
|
|
]); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* {@inheritdoc} |
|
164
|
|
|
*/ |
|
165
|
|
|
public function deleteQueue() { |
|
166
|
|
|
try { |
|
167
|
|
|
$this->collection->drop(); |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
catch (\Exception $e) { |
|
170
|
|
|
throw $e; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..