8495bfa731fdb5cbcaa2e4bdef238a056ff9278d
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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_Memory
extends CRM_Queue_Queue
{
34 * @var array(queueItemId => queueItemData)
40 queueItemId => releaseTime), expressed in seconds since epoch
44 var $nextQueueItemId = 1;
47 * Create a reference to queue. After constructing the queue, one should
48 * usually call createQueue (if it's a new queue) or loadQueue (if it's
49 * known to be an existing queue).
51 * @param $queueSpec, array with keys:
52 * - type: string, required, e.g. "interactive", "immediate", "stomp", "beanstalk"
53 * - name: string, required, e.g. "upgrade-tasks"
54 * - reset: bool, optional; if a queue is found, then it should be flushed; default to TRUE
55 * - (additional keys depending on the queue provider)
57 function __construct($queueSpec) {
58 parent
::__construct($queueSpec);
62 * Perform any registation or resource-allocation for a new queue
64 function createQueue() {
65 $this->items
= array();
66 $this->releaseTimes
= array();
70 * Perform any loading or pre-fetch for an existing queue.
72 function loadQueue() {
73 // $this->createQueue();
74 throw new Exception('Unsupported: CRM_Queue_Queue_Memory::loadQueue');
78 * Release any resources claimed by the queue (memory, DB rows, etc)
80 function deleteQueue() {
82 $this->releaseTimes
= NULL;
86 * Check if the queue exists
90 function existsQueue() {
91 return is_array($this->items
);
95 * Add a new item to the queue
97 * @param $data serializable PHP object or array
98 * @param array|\queue $options queue-dependent options; for example, if this is a
99 * priority-queue, then $options might specify the item's priority
101 * @return bool, TRUE on success
103 function createItem($data, $options = array()) {
104 $id = $this->nextQueueItemId++
;
105 // force copy, no unintendedsharing effects from pointers
106 $this->items
[$id] = serialize($data);
110 * Determine number of items remaining in the queue
114 function numberOfItems() {
115 return count($this->items
);
119 * Get and remove the next item
121 * @param int|\seconds $leaseTime seconds
123 * @return object with key 'data' that matches the inputted data
125 function claimItem($leaseTime = 3600) {
126 // foreach hits the items in order -- but we short-circuit after the first
127 foreach ($this->items
as $id => $data) {
128 $nowEpoch = CRM_Utils_Time
::getTimeRaw();
129 if (empty($this->releaseTimes
[$id]) ||
$this->releaseTimes
[$id] < $nowEpoch) {
130 $this->releaseTimes
[$id] = $nowEpoch +
$leaseTime;
132 $item = new stdClass();
134 $item->data
= unserialize($data);
138 // item in queue is reserved
149 * @param int|\seconds $leaseTime seconds
151 * @return object with key 'data' that matches the inputted data
153 function stealItem($leaseTime = 3600) {
154 // foreach hits the items in order -- but we short-circuit after the first
155 foreach ($this->items
as $id => $data) {
156 $nowEpoch = CRM_Utils_Time
::getTimeRaw();
157 $this->releaseTimes
[$id] = $nowEpoch +
$leaseTime;
159 $item = new stdClass();
161 $item->data
= unserialize($data);
169 * Remove an item from the queue
171 * @param $item object The item returned by claimItem
173 function deleteItem($item) {
174 unset($this->items
[$item->id
]);
175 unset($this->releaseTimes
[$item->id
]);
179 * Return an item that could not be processed
181 * @param $dao object The item returned by claimItem
185 function releaseItem($item) {
186 unset($this->releaseTimes
[$item->id
]);