1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
<?php
namespace Drupal\Core\Queue;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\DatabaseException;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
/**
* Default queue implementation.
*
* @ingroup queue
*/
class DatabaseQueue implements ReliableQueueInterface, QueueGarbageCollectionInterface, DelayableQueueInterface {
use DependencySerializationTrait;
/**
* The database table name.
*/
const TABLE_NAME = 'queue';
/**
* The name of the queue this instance is working with.
*
* @var string
*/
protected $name;
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* Constructs a \Drupal\Core\Queue\DatabaseQueue object.
*
* @param string $name
* The name of the queue.
* @param \Drupal\Core\Database\Connection $connection
* The Connection object containing the key-value tables.
*/
public function __construct($name, Connection $connection) {
$this->name = $name;
$this->connection = $connection;
}
/**
* {@inheritdoc}
*/
public function createItem($data) {
$try_again = FALSE;
try {
$id = $this->doCreateItem($data);
}
catch (\Exception $e) {
// If there was an exception, try to create the table.
if (!$try_again = $this->ensureTableExists()) {
// If the exception happened for other reason than the missing table,
// propagate the exception.
throw $e;
}
}
// Now that the table has been created, try again if necessary.
if ($try_again) {
$id = $this->doCreateItem($data);
}
return $id;
}
/**
* Adds a queue item and store it directly to the queue.
*
* @param mixed $data
* Arbitrary data to be associated with the new task in the queue.
*
* @return int|string
* A unique ID if the item was successfully created and was (best effort)
* added to the queue, otherwise FALSE. We don't guarantee the item was
* committed to disk etc, but as far as we know, the item is now in the
* queue.
*/
protected function doCreateItem($data) {
$query = $this->connection->insert(static::TABLE_NAME)
->fields([
'name' => $this->name,
'data' => serialize($data),
// We cannot rely on \Drupal::time()->getRequestTime() because many
// items might be created by a single request which takes longer than
// 1 second.
'created' => \Drupal::time()->getCurrentTime(),
]);
// Return the new serial ID, or FALSE on failure.
return $query->execute();
}
/**
* {@inheritdoc}
*/
public function numberOfItems() {
try {
return (int) $this->connection->query('SELECT COUNT([item_id]) FROM {' . static::TABLE_NAME . '} WHERE [name] = :name', [':name' => $this->name])
->fetchField();
}
catch (\Exception $e) {
$this->catchException($e);
// If there is no table there cannot be any items.
return 0;
}
}
/**
* {@inheritdoc}
*/
public function claimItem($lease_time = 30) {
// Claim an item by updating its expire fields. If claim is not successful
// another thread may have claimed the item in the meantime. Therefore loop
// until an item is successfully claimed or we are reasonably sure there
// are no unclaimed items left.
while (TRUE) {
try {
$item = $this->connection->queryRange('SELECT [data], [created], [item_id] FROM {' . static::TABLE_NAME . '} q WHERE [expire] = 0 AND [name] = :name ORDER BY [created], [item_id] ASC', 0, 1, [':name' => $this->name])->fetchObject();
}
catch (\Exception $e) {
$this->catchException($e);
}
// If the table does not exist there are no items currently available to
// claim.
if (empty($item)) {
return FALSE;
}
// Try to update the item. Only one thread can succeed in UPDATEing the
// same row. We cannot rely on \Drupal::time()->getRequestTime() because
// items might be claimed by a single consumer which runs longer than 1
// second. If we continue to use ::getRequestTime() instead of
// ::getCurrentTime(), we steal time from the lease, and will tend to
// reset items before the lease should really expire.
$update = $this->connection->update(static::TABLE_NAME)
->fields([
'expire' => \Drupal::time()->getCurrentTime() + $lease_time,
])
->condition('item_id', $item->item_id)
->condition('expire', 0);
// If there are affected rows, this update succeeded.
if ($update->execute()) {
$item->data = unserialize($item->data);
return $item;
}
}
}
/**
* {@inheritdoc}
*/
public function releaseItem($item) {
try {
$update = $this->connection->update(static::TABLE_NAME)
->fields([
'expire' => 0,
])
->condition('item_id', $item->item_id);
return (bool) $update->execute();
}
catch (\Exception $e) {
$this->catchException($e);
// If the table doesn't exist we should consider the item released.
return TRUE;
}
}
/**
* {@inheritdoc}
*/
public function delayItem($item, int $delay) {
// Only allow a positive delay interval.
if ($delay < 0) {
throw new \InvalidArgumentException('$delay must be non-negative');
}
try {
// Add the delay relative to the current time.
$expire = \Drupal::time()->getCurrentTime() + $delay;
// Update the expiry time of this item.
$update = $this->connection->update(static::TABLE_NAME)
->fields([
'expire' => $expire,
])
->condition('item_id', $item->item_id);
return (bool) $update->execute();
}
catch (\Exception $e) {
$this->catchException($e);
// If the table doesn't exist we should consider the item nonexistent.
return TRUE;
}
}
/**
* {@inheritdoc}
*/
public function deleteItem($item) {
try {
$this->connection->delete(static::TABLE_NAME)
->condition('item_id', $item->item_id)
->execute();
}
catch (\Exception $e) {
$this->catchException($e);
}
}
/**
* {@inheritdoc}
*/
public function createQueue() {
// All tasks are stored in a single database table (which is created on
// demand) so there is nothing we need to do to create a new queue.
}
/**
* {@inheritdoc}
*/
public function deleteQueue() {
try {
$this->connection->delete(static::TABLE_NAME)
->condition('name', $this->name)
->execute();
}
catch (\Exception $e) {
$this->catchException($e);
}
}
/**
* {@inheritdoc}
*/
public function garbageCollection() {
try {
// Clean up the queue for failed batches.
$this->connection->delete(static::TABLE_NAME)
->condition('created', \Drupal::time()->getRequestTime() - 864000, '<')
->condition('name', 'drupal_batch:%', 'LIKE')
->execute();
// Reset expired items in the default queue implementation table. If
// that's not used, this will simply be a no-op.
$this->connection->update(static::TABLE_NAME)
->fields([
'expire' => 0,
])
->condition('expire', 0, '<>')
->condition('expire', \Drupal::time()->getRequestTime(), '<')
->execute();
}
catch (\Exception $e) {
$this->catchException($e);
}
}
/**
* Check if the table exists and create it if not.
*/
protected function ensureTableExists() {
try {
$database_schema = $this->connection->schema();
$schema_definition = $this->schemaDefinition();
$database_schema->createTable(static::TABLE_NAME, $schema_definition);
}
// If another process has already created the queue table, attempting to
// recreate it will throw an exception. In this case just catch the
// exception and do nothing.
catch (DatabaseException) {
}
catch (\Exception) {
return FALSE;
}
return TRUE;
}
/**
* Act on an exception when queue might be stale.
*
* If the table does not yet exist, that's fine, but if the table exists and
* yet the query failed, then the queue is stale and the exception needs to
* propagate.
*
* @param \Exception $e
* The exception.
*
* @throws \Exception
* If the table exists the exception passed in is rethrown.
*/
protected function catchException(\Exception $e) {
if ($this->connection->schema()->tableExists(static::TABLE_NAME)) {
throw $e;
}
}
/**
* Defines the schema for the queue table.
*
* @internal
*/
public function schemaDefinition() {
return [
'description' => 'Stores items in queues.',
'fields' => [
'item_id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Unique item ID.',
],
'name' => [
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'The queue name.',
],
'data' => [
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'description' => 'The arbitrary data for the item.',
],
'expire' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Timestamp when the claim lease expires on the item.',
'size' => 'big',
],
'created' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Timestamp when the item was created.',
'size' => 'big',
],
],
'primary key' => ['item_id'],
'indexes' => [
'name_created' => ['name', 'created'],
'expire' => ['expire'],
],
];
}
}
|