3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * A queue implementation which stores items in the CiviCRM SQL database
31 class CRM_Queue_Queue_Sql
extends CRM_Queue_Queue
{
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).
38 * @param array $queueSpec
40 * - type: string, required, e.g. "interactive", "immediate", "stomp",
42 * - name: string, required, e.g. "upgrade-tasks"
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).
47 public function __construct($queueSpec) {
48 parent
::__construct($queueSpec);
52 * Perform any registation or resource-allocation for a new queue
54 public function createQueue() {
55 // nothing to do -- just start CRUDing items in the appropriate table
59 * Perform any loading or pre-fetch for an existing queue.
61 public function loadQueue() {
62 // nothing to do -- just start CRUDing items in the appropriate table
66 * Release any resources claimed by the queue (memory, DB rows, etc)
68 public function deleteQueue() {
69 return CRM_Core_DAO
::singleValueQuery("
70 DELETE FROM civicrm_queue_item
73 1 => array($this->getName(), 'String'),
78 * Check if the queue exists.
82 public function existsQueue() {
83 return ($this->numberOfItems() > 0);
87 * Add a new item to the queue.
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.
95 public function createItem($data, $options = array()) {
96 $dao = new CRM_Queue_DAO_QueueItem();
97 $dao->queue_name
= $this->getName();
98 $dao->submit_time
= CRM_Utils_Time
::getTime('YmdHis');
99 $dao->data
= serialize($data);
100 $dao->weight
= CRM_Utils_Array
::value('weight', $options, 0);
105 * Determine number of items remaining in the queue.
109 public function numberOfItems() {
110 return CRM_Core_DAO
::singleValueQuery("
112 FROM civicrm_queue_item
113 WHERE queue_name = %1
115 1 => array($this->getName(), 'String'),
122 * @param int $lease_time
126 * With key 'data' that matches the inputted data.
128 public function claimItem($lease_time = 3600) {
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
137 1 => array($this->getName(), 'String'),
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();
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(
149 '1' => array(date('YmdHis', $nowEpoch +
$lease_time), 'String'),
150 '2' => array($dao->id
, 'Integer'),
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);
156 $dao->data
= unserialize($dao->data
);
163 * Get the next item, even if there's an active lease
165 * @param int $lease_time
169 * With key 'data' that matches the inputted data.
171 public function stealItem($lease_time = 3600) {
173 SELECT id, queue_name, submit_time, release_time, data
174 FROM civicrm_queue_item
175 WHERE queue_name = %1
176 ORDER BY weight ASC, id ASC
180 1 => array($this->getName(), 'String'),
182 $dao = CRM_Core_DAO
::executeQuery($sql, $params, TRUE, 'CRM_Queue_DAO_QueueItem');
184 $nowEpoch = CRM_Utils_Time
::getTimeRaw();
185 CRM_Core_DAO
::executeQuery("UPDATE civicrm_queue_item SET release_time = %1 WHERE id = %2", array(
186 '1' => array(date('YmdHis', $nowEpoch +
$lease_time), 'String'),
187 '2' => array($dao->id
, 'Integer'),
189 $dao->data
= unserialize($dao->data
);
195 * Remove an item from the queue.
197 * @param CRM_Core_DAO $dao
198 * The item returned by claimItem.
200 public function deleteItem($dao) {
206 * Return an item that could not be processed.
208 * @param CRM_Core_DAO $dao
209 * The item returned by claimItem.
211 public function releaseItem($dao) {
212 $sql = "UPDATE civicrm_queue_item SET release_time = NULL WHERE id = %1";
214 1 => array($dao->id
, 'Integer'),
216 CRM_Core_DAO
::executeQuery($sql, $params);