|
1 <?php |
|
2 |
|
3 /** |
|
4 * @file |
|
5 * Queue handlers used by the Batch API. |
|
6 * |
|
7 * These implementations: |
|
8 * - Ensure FIFO ordering. |
|
9 * - Allow an item to be repeatedly claimed until it is actually deleted (no |
|
10 * notion of lease time or 'expire' date), to allow multipass operations. |
|
11 */ |
|
12 |
|
13 /** |
|
14 * Defines a batch queue. |
|
15 * |
|
16 * Stale items from failed batches are cleaned from the {queue} table on cron |
|
17 * using the 'created' date. |
|
18 */ |
|
19 class BatchQueue extends SystemQueue { |
|
20 |
|
21 /** |
|
22 * Overrides SystemQueue::claimItem(). |
|
23 * |
|
24 * Unlike SystemQueue::claimItem(), this method provides a default lease |
|
25 * time of 0 (no expiration) instead of 30. This allows the item to be |
|
26 * claimed repeatedly until it is deleted. |
|
27 */ |
|
28 public function claimItem($lease_time = 0) { |
|
29 $item = db_query_range('SELECT data, item_id FROM {queue} q WHERE name = :name ORDER BY item_id ASC', 0, 1, array(':name' => $this->name))->fetchObject(); |
|
30 if ($item) { |
|
31 $item->data = unserialize($item->data); |
|
32 return $item; |
|
33 } |
|
34 return FALSE; |
|
35 } |
|
36 |
|
37 /** |
|
38 * Retrieves all remaining items in the queue. |
|
39 * |
|
40 * This is specific to Batch API and is not part of the DrupalQueueInterface. |
|
41 */ |
|
42 public function getAllItems() { |
|
43 $result = array(); |
|
44 $items = db_query('SELECT data FROM {queue} q WHERE name = :name ORDER BY item_id ASC', array(':name' => $this->name))->fetchAll(); |
|
45 foreach ($items as $item) { |
|
46 $result[] = unserialize($item->data); |
|
47 } |
|
48 return $result; |
|
49 } |
|
50 } |
|
51 |
|
52 /** |
|
53 * Defines a batch queue for non-progressive batches. |
|
54 */ |
|
55 class BatchMemoryQueue extends MemoryQueue { |
|
56 |
|
57 /** |
|
58 * Overrides MemoryQueue::claimItem(). |
|
59 * |
|
60 * Unlike MemoryQueue::claimItem(), this method provides a default lease |
|
61 * time of 0 (no expiration) instead of 30. This allows the item to be |
|
62 * claimed repeatedly until it is deleted. |
|
63 */ |
|
64 public function claimItem($lease_time = 0) { |
|
65 if (!empty($this->queue)) { |
|
66 reset($this->queue); |
|
67 return current($this->queue); |
|
68 } |
|
69 return FALSE; |
|
70 } |
|
71 |
|
72 /** |
|
73 * Retrieves all remaining items in the queue. |
|
74 * |
|
75 * This is specific to Batch API and is not part of the DrupalQueueInterface. |
|
76 */ |
|
77 public function getAllItems() { |
|
78 $result = array(); |
|
79 foreach ($this->queue as $item) { |
|
80 $result[] = $item->data; |
|
81 } |
|
82 return $result; |
|
83 } |
|
84 } |