-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvancedqueue.queue.inc
108 lines (98 loc) · 3.6 KB
/
advancedqueue.queue.inc
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
<?php
/**
* Defintion of AdvancedQueue.
*/
/**
* Extended queue.
*/
class AdvancedQueue implements DrupalReliableQueueInterface {
/**
* The name of the queue this instance is working with.
*
* @var string
*/
protected $name;
public function __construct($name) {
$this->name = $name;
}
public function createItem($data) {
$query = db_insert('advancedqueue')
->fields(array(
'name' => $this->name,
'uid' => is_array($data) && isset($data['uid']) ? $data['uid'] : $GLOBALS['user']->uid,
'title' => is_array($data) && isset($data['title']) ? $data['title'] : t('Unnamed item'),
'data' => serialize($data),
// We cannot rely on REQUEST_TIME because many items might be created
// by a single request which takes longer than 1 second.
'created' => time(),
'status' => ADVANCEDQUEUE_STATUS_QUEUED,
));
return (bool) $query->execute();
}
public function numberOfItems() {
return db_query('SELECT COUNT(item_id) FROM {advancedqueue} WHERE name = :name AND status <= 0', array(':name' => $this->name))->fetchField();
}
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) {
$item = db_query_range('SELECT data, item_id FROM {advancedqueue} q WHERE status = -1 AND expire = 0 AND name = :name ORDER BY created ASC', 0, 1, array(':name' => $this->name))->fetchObject();
if ($item) {
// Try to update the item. Only one thread can succeed in UPDATEing the
// same row. We cannot rely on REQUEST_TIME because items might be
// claimed by a single consumer which runs longer than 1 second. If we
// continue to use REQUEST_TIME instead of the current time(), we steal
// time from the lease, and will tend to reset items before the lease
// should really expire.
$update = db_update('advancedqueue')
->fields(array(
'status' => ADVANCEDQUEUE_STATUS_PROCESSING,
'expire' => time() + $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;
}
}
else {
// No items currently available to claim.
return FALSE;
}
}
}
public function releaseItem($item) {
$update = db_update('advancedqueue')
->fields(array(
'expire' => 0,
'status' => ADVANCEDQUEUE_STATUS_QUEUED,
))
->condition('item_id', $item->item_id);
return $update->execute();
}
public function deleteItem($item) {
db_update('advancedqueue')
->fields(array(
'expire' => 0,
'status' => isset($item->status) ? $item->status : ADVANCEDQUEUE_STATUS_SUCCESS,
'result' => serialize(isset($item->result) ? $item->result : array()),
'processed' => time(),
))
->condition('item_id', $item->item_id)
->execute();
}
public function createQueue() {
// All tasks are stored in a single database table (which is created when
// Drupal is first installed) so there is nothing we need to do to create
// a new queue.
}
public function deleteQueue() {
db_delete('advancedqueue')
->condition('name', $this->name)
->execute();
}
}