3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
{
35 * array(queueItemId => queueItemData)
41 * array(queueItemId => releaseTime), expressed in seconds since epoch.
45 public $nextQueueItemId = 1;
48 * Create a reference to queue. After constructing the queue, one should
49 * usually call createQueue (if it's a new queue) or loadQueue (if it's
50 * known to be an existing queue).
52 * @param array $queueSpec
54 * - type: string, required, e.g. "interactive", "immediate", "stomp",
56 * - name: string, required, e.g. "upgrade-tasks"
57 * - reset: bool, optional; if a queue is found, then it should be
58 * flushed; default to TRUE
59 * - (additional keys depending on the queue provider).
61 public function __construct($queueSpec) {
62 parent
::__construct($queueSpec);
66 * Perform any registation or resource-allocation for a new queue
68 public function createQueue() {
69 $this->items
= array();
70 $this->releaseTimes
= array();
74 * Perform any loading or pre-fetch for an existing queue.
76 public function loadQueue() {
77 // $this->createQueue();
78 throw new Exception('Unsupported: CRM_Queue_Queue_Memory::loadQueue');
82 * Release any resources claimed by the queue (memory, DB rows, etc)
84 public function deleteQueue() {
86 $this->releaseTimes
= NULL;
90 * Check if the queue exists.
94 public function existsQueue() {
95 return is_array($this->items
);
99 * Add a new item to the queue.
102 * Serializable PHP object or array.
103 * @param array $options
104 * Queue-dependent options; for example, if this is a
105 * priority-queue, then $options might specify the item's priority.
107 public function createItem($data, $options = array()) {
108 $id = $this->nextQueueItemId++
;
109 // force copy, no unintendedsharing effects from pointers
110 $this->items
[$id] = serialize($data);
114 * Determine number of items remaining in the queue.
118 public function numberOfItems() {
119 return count($this->items
);
123 * Get and remove the next item.
125 * @param int $leaseTime
129 * Includes key 'data' that matches the inputted data.
131 public function claimItem($leaseTime = 3600) {
132 // foreach hits the items in order -- but we short-circuit after the first
133 foreach ($this->items
as $id => $data) {
134 $nowEpoch = CRM_Utils_Time
::getTimeRaw();
135 if (empty($this->releaseTimes
[$id]) ||
$this->releaseTimes
[$id] < $nowEpoch) {
136 $this->releaseTimes
[$id] = $nowEpoch +
$leaseTime;
138 $item = new stdClass();
140 $item->data
= unserialize($data);
144 // item in queue is reserved
155 * @param int $leaseTime
159 * With key 'data' that matches the inputted data.
161 public function stealItem($leaseTime = 3600) {
162 // foreach hits the items in order -- but we short-circuit after the first
163 foreach ($this->items
as $id => $data) {
164 $nowEpoch = CRM_Utils_Time
::getTimeRaw();
165 $this->releaseTimes
[$id] = $nowEpoch +
$leaseTime;
167 $item = new stdClass();
169 $item->data
= unserialize($data);
177 * Remove an item from the queue.
179 * @param object $item
180 * The item returned by claimItem.
182 public function deleteItem($item) {
183 unset($this->items
[$item->id
]);
184 unset($this->releaseTimes
[$item->id
]);
188 * Return an item that could not be processed.
190 * @param CRM_Core_DAO $item
191 * The item returned by claimItem.
193 public function releaseItem($item) {
194 unset($this->releaseTimes
[$item->id
]);