CRM/Queue - Code style
[civicrm-core.git] / CRM / Queue / Queue / Sql.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 * A queue implementation which stores items in the CiviCRM SQL database
30 */
31class CRM_Queue_Queue_Sql extends CRM_Queue_Queue {
32
33 /**
34 * Create a reference to queue. After constructing the queue, one should
35 * usually call createQueue (if it's a new queue) or loadQueue (if it's
36 * known to be an existing queue).
37 *
4523a2f5
TO
38 * @param array $queueSpec
39 * Array with keys:
40 * - type: string, required, e.g. "interactive", "immediate", "stomp",
41 * "beanstalk"
6a488035 42 * - name: string, required, e.g. "upgrade-tasks"
4523a2f5
TO
43 * - reset: bool, optional; if a queue is found, then it should be
44 * flushed; default to TRUE
45 * - (additional keys depending on the queue provider).
6a488035 46 */
4523a2f5 47 public function __construct($queueSpec) {
6a488035
TO
48 parent::__construct($queueSpec);
49 }
50
51 /**
52 * Perform any registation or resource-allocation for a new queue
53 */
4523a2f5 54 public function createQueue() {
6a488035
TO
55 // nothing to do -- just start CRUDing items in the appropriate table
56 }
57
58 /**
59 * Perform any loading or pre-fetch for an existing queue.
60 */
4523a2f5 61 public function loadQueue() {
6a488035
TO
62 // nothing to do -- just start CRUDing items in the appropriate table
63 }
64
65 /**
66 * Release any resources claimed by the queue (memory, DB rows, etc)
67 */
4523a2f5 68 public function deleteQueue() {
6a488035
TO
69 return CRM_Core_DAO::singleValueQuery("
70 DELETE FROM civicrm_queue_item
71 WHERE queue_name = %1
72 ", array(
4523a2f5
TO
73 1 => array($this->getName(), 'String'),
74 ));
6a488035
TO
75 }
76
77 /**
78 * Check if the queue exists
79 *
80 * @return bool
81 */
4523a2f5 82 public function existsQueue() {
6a488035
TO
83 return ($this->numberOfItems() > 0);
84 }
85
86 /**
87 * Add a new item to the queue
88 *
4523a2f5
TO
89 * @param mixed $data
90 * Serializable PHP object or array.
91 * @param array $options
92 * Queue-dependent options; for example, if this is a
93 * priority-queue, then $options might specify the item's priority.
6a488035 94 */
4523a2f5
TO
95 public function createItem($data, $options = array()) {
96 $dao = new CRM_Queue_DAO_QueueItem();
97 $dao->queue_name = $this->getName();
6a488035 98 $dao->submit_time = CRM_Utils_Time::getTime('YmdHis');
4523a2f5
TO
99 $dao->data = serialize($data);
100 $dao->weight = CRM_Utils_Array::value('weight', $options, 0);
6a488035
TO
101 $dao->save();
102 }
103
104 /**
105 * Determine number of items remaining in the queue
106 *
107 * @return int
108 */
4523a2f5 109 public function numberOfItems() {
6a488035
TO
110 return CRM_Core_DAO::singleValueQuery("
111 SELECT count(*)
112 FROM civicrm_queue_item
113 WHERE queue_name = %1
114 ", array(
4523a2f5
TO
115 1 => array($this->getName(), 'String'),
116 ));
6a488035
TO
117 }
118
119 /**
120 * Get the next item
121 *
4523a2f5
TO
122 * @param int $lease_time
123 * Seconds.
6a488035 124 *
4523a2f5
TO
125 * @return object
126 * With key 'data' that matches the inputted data.
6a488035 127 */
4523a2f5 128 public function claimItem($lease_time = 3600) {
6a488035
TO
129 $sql = "
130 SELECT id, queue_name, submit_time, release_time, data
131 FROM civicrm_queue_item
132 WHERE queue_name = %1
133 ORDER BY weight ASC, id ASC
134 LIMIT 1
135 ";
136 $params = array(
137 1 => array($this->getName(), 'String'),
138 );
139 $dao = CRM_Core_DAO::executeQuery($sql, $params, TRUE, 'CRM_Queue_DAO_QueueItem');
140 if (is_a($dao, 'DB_Error')) {
141 // FIXME - Adding code to allow tests to pass
142 CRM_Core_Error::fatal();
143 }
144
145 if ($dao->fetch()) {
146 $nowEpoch = CRM_Utils_Time::getTimeRaw();
147 if ($dao->release_time === NULL || strtotime($dao->release_time) < $nowEpoch) {
148 CRM_Core_DAO::executeQuery("UPDATE civicrm_queue_item SET release_time = %1 WHERE id = %2", array(
4523a2f5
TO
149 '1' => array(date('YmdHis', $nowEpoch + $lease_time), 'String'),
150 '2' => array($dao->id, 'Integer'),
151 ));
6a488035
TO
152 // work-around: inconsistent date-formatting causes unintentional breakage
153 # $dao->submit_time = date('YmdHis', strtotime($dao->submit_time));
154 # $dao->release_time = date('YmdHis', $nowEpoch + $lease_time);
155 # $dao->save();
156 $dao->data = unserialize($dao->data);
157 return $dao;
158 }
159 else {
160 CRM_Core_Error::debug_var('not ready for release', $dao);
161 return FALSE;
162 }
163 }
164 else {
165 CRM_Core_Error::debug_var('no items found');
166 return FALSE;
167 }
168 }
169
170 /**
171 * Get the next item, even if there's an active lease
172 *
4523a2f5
TO
173 * @param int $lease_time
174 * Seconds.
6a488035 175 *
4523a2f5
TO
176 * @return object
177 * With key 'data' that matches the inputted data.
6a488035 178 */
4523a2f5 179 public function stealItem($lease_time = 3600) {
6a488035
TO
180 $sql = "
181 SELECT id, queue_name, submit_time, release_time, data
182 FROM civicrm_queue_item
183 WHERE queue_name = %1
184 ORDER BY weight ASC, id ASC
185 LIMIT 1
186 ";
187 $params = array(
188 1 => array($this->getName(), 'String'),
189 );
190 $dao = CRM_Core_DAO::executeQuery($sql, $params, TRUE, 'CRM_Queue_DAO_QueueItem');
191 if ($dao->fetch()) {
192 $nowEpoch = CRM_Utils_Time::getTimeRaw();
193 CRM_Core_DAO::executeQuery("UPDATE civicrm_queue_item SET release_time = %1 WHERE id = %2", array(
4523a2f5
TO
194 '1' => array(date('YmdHis', $nowEpoch + $lease_time), 'String'),
195 '2' => array($dao->id, 'Integer'),
196 ));
6a488035
TO
197 $dao->data = unserialize($dao->data);
198 return $dao;
199 }
200 else {
201 CRM_Core_Error::debug_var('no items found');
202 return FALSE;
203 }
204 }
205
206 /**
207 * Remove an item from the queue
208 *
4523a2f5
TO
209 * @param CRM_Core_DAO $dao
210 * The item returned by claimItem.
6a488035 211 */
4523a2f5 212 public function deleteItem($dao) {
6a488035
TO
213 $dao->delete();
214 $dao->free();
215 }
216
217 /**
218 * Return an item that could not be processed
219 *
4523a2f5
TO
220 * @param CRM_Core_DAO $dao
221 * The item returned by claimItem.
6a488035 222 */
4523a2f5 223 public function releaseItem($dao) {
6a488035
TO
224 $sql = "UPDATE civicrm_queue_item SET release_time = NULL WHERE id = %1";
225 $params = array(
226 1 => array($dao->id, 'Integer'),
227 );
228 CRM_Core_DAO::executeQuery($sql, $params);
229 $dao->free();
230 }
231}