Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * A queue implementation which stores items in the CiviCRM SQL database | |
14 | */ | |
15 | class CRM_Queue_Queue_Sql extends CRM_Queue_Queue { | |
16 | ||
17 | /** | |
18 | * Create a reference to queue. After constructing the queue, one should | |
19 | * usually call createQueue (if it's a new queue) or loadQueue (if it's | |
20 | * known to be an existing queue). | |
21 | * | |
4523a2f5 TO |
22 | * @param array $queueSpec |
23 | * Array with keys: | |
24 | * - type: string, required, e.g. "interactive", "immediate", "stomp", | |
25 | * "beanstalk" | |
6a488035 | 26 | * - name: string, required, e.g. "upgrade-tasks" |
4523a2f5 TO |
27 | * - reset: bool, optional; if a queue is found, then it should be |
28 | * flushed; default to TRUE | |
29 | * - (additional keys depending on the queue provider). | |
6a488035 | 30 | */ |
4523a2f5 | 31 | public function __construct($queueSpec) { |
6a488035 TO |
32 | parent::__construct($queueSpec); |
33 | } | |
34 | ||
35 | /** | |
36 | * Perform any registation or resource-allocation for a new queue | |
37 | */ | |
4523a2f5 | 38 | public function createQueue() { |
6a488035 TO |
39 | // nothing to do -- just start CRUDing items in the appropriate table |
40 | } | |
41 | ||
42 | /** | |
43 | * Perform any loading or pre-fetch for an existing queue. | |
44 | */ | |
4523a2f5 | 45 | public function loadQueue() { |
6a488035 TO |
46 | // nothing to do -- just start CRUDing items in the appropriate table |
47 | } | |
48 | ||
49 | /** | |
50 | * Release any resources claimed by the queue (memory, DB rows, etc) | |
51 | */ | |
4523a2f5 | 52 | public function deleteQueue() { |
6a488035 TO |
53 | return CRM_Core_DAO::singleValueQuery(" |
54 | DELETE FROM civicrm_queue_item | |
55 | WHERE queue_name = %1 | |
be2fb01f CW |
56 | ", [ |
57 | 1 => [$this->getName(), 'String'], | |
58 | ]); | |
6a488035 TO |
59 | } |
60 | ||
61 | /** | |
fe482240 | 62 | * Check if the queue exists. |
6a488035 TO |
63 | * |
64 | * @return bool | |
65 | */ | |
4523a2f5 | 66 | public function existsQueue() { |
6a488035 TO |
67 | return ($this->numberOfItems() > 0); |
68 | } | |
69 | ||
70 | /** | |
fe482240 | 71 | * Add a new item to the queue. |
6a488035 | 72 | * |
4523a2f5 TO |
73 | * @param mixed $data |
74 | * Serializable PHP object or array. | |
75 | * @param array $options | |
76 | * Queue-dependent options; for example, if this is a | |
77 | * priority-queue, then $options might specify the item's priority. | |
6a488035 | 78 | */ |
be2fb01f | 79 | public function createItem($data, $options = []) { |
4523a2f5 TO |
80 | $dao = new CRM_Queue_DAO_QueueItem(); |
81 | $dao->queue_name = $this->getName(); | |
6a488035 | 82 | $dao->submit_time = CRM_Utils_Time::getTime('YmdHis'); |
4523a2f5 TO |
83 | $dao->data = serialize($data); |
84 | $dao->weight = CRM_Utils_Array::value('weight', $options, 0); | |
6a488035 TO |
85 | $dao->save(); |
86 | } | |
87 | ||
88 | /** | |
fe482240 | 89 | * Determine number of items remaining in the queue. |
6a488035 TO |
90 | * |
91 | * @return int | |
92 | */ | |
4523a2f5 | 93 | public function numberOfItems() { |
6a488035 TO |
94 | return CRM_Core_DAO::singleValueQuery(" |
95 | SELECT count(*) | |
96 | FROM civicrm_queue_item | |
97 | WHERE queue_name = %1 | |
be2fb01f CW |
98 | ", [ |
99 | 1 => [$this->getName(), 'String'], | |
100 | ]); | |
6a488035 TO |
101 | } |
102 | ||
103 | /** | |
fe482240 | 104 | * Get the next item. |
6a488035 | 105 | * |
4523a2f5 TO |
106 | * @param int $lease_time |
107 | * Seconds. | |
6a488035 | 108 | * |
4523a2f5 TO |
109 | * @return object |
110 | * With key 'data' that matches the inputted data. | |
6a488035 | 111 | */ |
4523a2f5 | 112 | public function claimItem($lease_time = 3600) { |
e38f694b RLAR |
113 | |
114 | $result = NULL; | |
115 | $dao = CRM_Core_DAO::executeQuery('LOCK TABLES civicrm_queue_item WRITE;'); | |
6a488035 | 116 | $sql = " |
e38f694b RLAR |
117 | SELECT first_in_queue.* FROM ( |
118 | SELECT id, queue_name, submit_time, release_time, data | |
119 | FROM civicrm_queue_item | |
120 | WHERE queue_name = %1 | |
121 | ORDER BY weight ASC, id ASC | |
122 | LIMIT 1 | |
123 | ) first_in_queue | |
f0733867 | 124 | WHERE release_time IS NULL OR release_time < %2 |
e38f694b | 125 | "; |
be2fb01f CW |
126 | $params = [ |
127 | 1 => [$this->getName(), 'String'], | |
f0733867 | 128 | 2 => [CRM_Utils_Time::getTime(), 'Timestamp'], |
be2fb01f | 129 | ]; |
6a488035 TO |
130 | $dao = CRM_Core_DAO::executeQuery($sql, $params, TRUE, 'CRM_Queue_DAO_QueueItem'); |
131 | if (is_a($dao, 'DB_Error')) { | |
132 | // FIXME - Adding code to allow tests to pass | |
133 | CRM_Core_Error::fatal(); | |
134 | } | |
135 | ||
136 | if ($dao->fetch()) { | |
137 | $nowEpoch = CRM_Utils_Time::getTimeRaw(); | |
e38f694b RLAR |
138 | CRM_Core_DAO::executeQuery("UPDATE civicrm_queue_item SET release_time = %1 WHERE id = %2", [ |
139 | '1' => [date('YmdHis', $nowEpoch + $lease_time), 'String'], | |
140 | '2' => [$dao->id, 'Integer'], | |
141 | ]); | |
142 | // (Comment by artfulrobot Sep 2019: Not sure what the below comment means, should be removed/clarified?) | |
143 | // work-around: inconsistent date-formatting causes unintentional breakage | |
144 | # $dao->submit_time = date('YmdHis', strtotime($dao->submit_time)); | |
145 | # $dao->release_time = date('YmdHis', $nowEpoch + $lease_time); | |
146 | # $dao->save(); | |
147 | $dao->data = unserialize($dao->data); | |
148 | $result = $dao; | |
6a488035 | 149 | } |
e38f694b RLAR |
150 | |
151 | $dao = CRM_Core_DAO::executeQuery('UNLOCK TABLES;'); | |
152 | ||
153 | return $result; | |
6a488035 TO |
154 | } |
155 | ||
156 | /** | |
157 | * Get the next item, even if there's an active lease | |
158 | * | |
4523a2f5 TO |
159 | * @param int $lease_time |
160 | * Seconds. | |
6a488035 | 161 | * |
4523a2f5 TO |
162 | * @return object |
163 | * With key 'data' that matches the inputted data. | |
6a488035 | 164 | */ |
4523a2f5 | 165 | public function stealItem($lease_time = 3600) { |
6a488035 TO |
166 | $sql = " |
167 | SELECT id, queue_name, submit_time, release_time, data | |
168 | FROM civicrm_queue_item | |
169 | WHERE queue_name = %1 | |
170 | ORDER BY weight ASC, id ASC | |
171 | LIMIT 1 | |
172 | "; | |
be2fb01f CW |
173 | $params = [ |
174 | 1 => [$this->getName(), 'String'], | |
175 | ]; | |
6a488035 TO |
176 | $dao = CRM_Core_DAO::executeQuery($sql, $params, TRUE, 'CRM_Queue_DAO_QueueItem'); |
177 | if ($dao->fetch()) { | |
178 | $nowEpoch = CRM_Utils_Time::getTimeRaw(); | |
be2fb01f CW |
179 | CRM_Core_DAO::executeQuery("UPDATE civicrm_queue_item SET release_time = %1 WHERE id = %2", [ |
180 | '1' => [date('YmdHis', $nowEpoch + $lease_time), 'String'], | |
181 | '2' => [$dao->id, 'Integer'], | |
182 | ]); | |
6a488035 TO |
183 | $dao->data = unserialize($dao->data); |
184 | return $dao; | |
185 | } | |
6a488035 TO |
186 | } |
187 | ||
188 | /** | |
fe482240 | 189 | * Remove an item from the queue. |
6a488035 | 190 | * |
4523a2f5 TO |
191 | * @param CRM_Core_DAO $dao |
192 | * The item returned by claimItem. | |
6a488035 | 193 | */ |
4523a2f5 | 194 | public function deleteItem($dao) { |
6a488035 | 195 | $dao->delete(); |
6a488035 TO |
196 | } |
197 | ||
198 | /** | |
fe482240 | 199 | * Return an item that could not be processed. |
6a488035 | 200 | * |
4523a2f5 TO |
201 | * @param CRM_Core_DAO $dao |
202 | * The item returned by claimItem. | |
6a488035 | 203 | */ |
4523a2f5 | 204 | public function releaseItem($dao) { |
6a488035 | 205 | $sql = "UPDATE civicrm_queue_item SET release_time = NULL WHERE id = %1"; |
be2fb01f CW |
206 | $params = [ |
207 | 1 => [$dao->id, 'Integer'], | |
208 | ]; | |
6a488035 | 209 | CRM_Core_DAO::executeQuery($sql, $params); |
6a488035 | 210 | } |
96025800 | 211 | |
6a488035 | 212 | } |