Merge pull request #21550 from demeritcowboy/template-missed
[civicrm-core.git] / CRM / Queue / Queue / SqlParallel.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * A queue implementation which stores items in the CiviCRM SQL database
14 */
15 class CRM_Queue_Queue_SqlParallel 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 *
22 * @param array $queueSpec
23 * Array with keys:
24 * - type: string, required, e.g. "interactive", "immediate", "stomp",
25 * "beanstalk"
26 * - name: string, required, e.g. "upgrade-tasks"
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).
30 */
31 public function __construct($queueSpec) {
32 parent::__construct($queueSpec);
33 }
34
35 /**
36 * Perform any registation or resource-allocation for a new queue
37 */
38 public function createQueue() {
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 */
45 public function loadQueue() {
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 */
52 public function deleteQueue() {
53 return CRM_Core_DAO::singleValueQuery("
54 DELETE FROM civicrm_queue_item
55 WHERE queue_name = %1
56 ", [
57 1 => [$this->getName(), 'String'],
58 ]);
59 }
60
61 /**
62 * Check if the queue exists.
63 *
64 * @return bool
65 */
66 public function existsQueue() {
67 return ($this->numberOfItems() > 0);
68 }
69
70 /**
71 * Add a new item to the queue.
72 *
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.
78 */
79 public function createItem($data, $options = []) {
80 $dao = new CRM_Queue_DAO_QueueItem();
81 $dao->queue_name = $this->getName();
82 $dao->submit_time = CRM_Utils_Time::getTime('YmdHis');
83 $dao->data = serialize($data);
84 $dao->weight = CRM_Utils_Array::value('weight', $options, 0);
85 $dao->save();
86 }
87
88 /**
89 * Determine number of items remaining in the queue.
90 *
91 * @return int
92 */
93 public function numberOfItems() {
94 return CRM_Core_DAO::singleValueQuery("
95 SELECT count(*)
96 FROM civicrm_queue_item
97 WHERE queue_name = %1
98 ", [
99 1 => [$this->getName(), 'String'],
100 ]);
101 }
102
103 /**
104 * Get the next item.
105 *
106 * @param int $lease_time
107 * Seconds.
108 *
109 * @return object
110 * With key 'data' that matches the inputted data.
111 */
112 public function claimItem($lease_time = 3600) {
113
114 $result = NULL;
115 $dao = CRM_Core_DAO::executeQuery('LOCK TABLES civicrm_queue_item WRITE;');
116 $sql = "SELECT id, queue_name, submit_time, release_time, data
117 FROM civicrm_queue_item
118 WHERE queue_name = %1
119 AND release_time IS NULL
120 ORDER BY weight ASC, id ASC
121 LIMIT 1
122 ";
123 $params = [
124 1 => [$this->getName(), 'String'],
125 ];
126 $dao = CRM_Core_DAO::executeQuery($sql, $params, TRUE, 'CRM_Queue_DAO_QueueItem');
127 if (is_a($dao, 'DB_Error')) {
128 // FIXME - Adding code to allow tests to pass
129 CRM_Core_Error::fatal();
130 }
131
132 if ($dao->fetch()) {
133 $nowEpoch = CRM_Utils_Time::getTimeRaw();
134 CRM_Core_DAO::executeQuery("UPDATE civicrm_queue_item SET release_time = %1 WHERE id = %2", [
135 '1' => [date('YmdHis', $nowEpoch + $lease_time), 'String'],
136 '2' => [$dao->id, 'Integer'],
137 ]);
138 // (Comment by artfulrobot Sep 2019: Not sure what the below comment means, should be removed/clarified?)
139 // work-around: inconsistent date-formatting causes unintentional breakage
140 # $dao->submit_time = date('YmdHis', strtotime($dao->submit_time));
141 # $dao->release_time = date('YmdHis', $nowEpoch + $lease_time);
142 # $dao->save();
143 $dao->data = unserialize($dao->data);
144 $result = $dao;
145 }
146
147 $dao = CRM_Core_DAO::executeQuery('UNLOCK TABLES;');
148
149 return $result;
150 }
151
152 /**
153 * Get the next item, even if there's an active lease
154 *
155 * @param int $lease_time
156 * Seconds.
157 *
158 * @return object
159 * With key 'data' that matches the inputted data.
160 */
161 public function stealItem($lease_time = 3600) {
162 $sql = "
163 SELECT id, queue_name, submit_time, release_time, data
164 FROM civicrm_queue_item
165 WHERE queue_name = %1
166 ORDER BY weight ASC, id ASC
167 LIMIT 1
168 ";
169 $params = [
170 1 => [$this->getName(), 'String'],
171 ];
172 $dao = CRM_Core_DAO::executeQuery($sql, $params, TRUE, 'CRM_Queue_DAO_QueueItem');
173 if ($dao->fetch()) {
174 $nowEpoch = CRM_Utils_Time::getTimeRaw();
175 CRM_Core_DAO::executeQuery("UPDATE civicrm_queue_item SET release_time = %1 WHERE id = %2", [
176 '1' => [date('YmdHis', $nowEpoch + $lease_time), 'String'],
177 '2' => [$dao->id, 'Integer'],
178 ]);
179 $dao->data = unserialize($dao->data);
180 return $dao;
181 }
182 }
183
184 /**
185 * Remove an item from the queue.
186 *
187 * @param CRM_Core_DAO $dao
188 * The item returned by claimItem.
189 */
190 public function deleteItem($dao) {
191 $dao->delete();
192 $dao->free();
193 }
194
195 /**
196 * Return an item that could not be processed.
197 *
198 * @param CRM_Core_DAO $dao
199 * The item returned by claimItem.
200 */
201 public function releaseItem($dao) {
202 $sql = "UPDATE civicrm_queue_item SET release_time = NULL WHERE id = %1";
203 $params = [
204 1 => [$dao->id, 'Integer'],
205 ];
206 CRM_Core_DAO::executeQuery($sql, $params);
207 $dao->free();
208 }
209
210 }