Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | */ | |
31 | class CRM_Queue_Queue_Memory extends CRM_Queue_Queue { | |
32 | ||
33 | /** | |
34 | * @var array(queueItemId => queueItemData) | |
35 | */ | |
36 | var $items; | |
37 | ||
38 | /** | |
39 | * @var array( | |
40 | queueItemId => releaseTime), expressed in seconds since epoch | |
41 | */ | |
42 | var $releaseTimes; | |
43 | ||
44 | var $nextQueueItemId = 1; | |
45 | ||
46 | /** | |
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). | |
50 | * | |
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) | |
56 | */ | |
57 | function __construct($queueSpec) { | |
58 | parent::__construct($queueSpec); | |
59 | } | |
60 | ||
61 | /** | |
62 | * Perform any registation or resource-allocation for a new queue | |
63 | */ | |
64 | function createQueue() { | |
65 | $this->items = array(); | |
66 | $this->releaseTimes = array(); | |
67 | } | |
68 | ||
69 | /** | |
70 | * Perform any loading or pre-fetch for an existing queue. | |
71 | */ | |
72 | function loadQueue() { | |
73 | // $this->createQueue(); | |
74 | throw new Exception('Unsupported: CRM_Queue_Queue_Memory::loadQueue'); | |
75 | } | |
76 | ||
77 | /** | |
78 | * Release any resources claimed by the queue (memory, DB rows, etc) | |
79 | */ | |
80 | function deleteQueue() { | |
81 | $this->items = NULL; | |
82 | $this->releaseTimes = NULL; | |
83 | } | |
84 | ||
85 | /** | |
86 | * Check if the queue exists | |
87 | * | |
88 | * @return bool | |
89 | */ | |
90 | function existsQueue() { | |
91 | return is_array($this->items); | |
92 | } | |
93 | ||
94 | /** | |
95 | * Add a new item to the queue | |
96 | * | |
97 | * @param $data serializable PHP object or array | |
77b97be7 | 98 | * @param array|\queue $options queue-dependent options; for example, if this is a |
6a488035 TO |
99 | * priority-queue, then $options might specify the item's priority |
100 | * | |
101 | * @return bool, TRUE on success | |
102 | */ | |
103 | function createItem($data, $options = array()) { | |
104 | $id = $this->nextQueueItemId++; | |
105 | // force copy, no unintendedsharing effects from pointers | |
106 | $this->items[$id] = serialize($data); | |
107 | } | |
108 | ||
109 | /** | |
110 | * Determine number of items remaining in the queue | |
111 | * | |
112 | * @return int | |
113 | */ | |
114 | function numberOfItems() { | |
115 | return count($this->items); | |
116 | } | |
117 | ||
118 | /** | |
119 | * Get and remove the next item | |
120 | * | |
6c8f6e67 | 121 | * @param int|\seconds $leaseTime seconds |
6a488035 TO |
122 | * |
123 | * @return object with key 'data' that matches the inputted data | |
124 | */ | |
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; | |
131 | ||
132 | $item = new stdClass(); | |
133 | $item->id = $id; | |
134 | $item->data = unserialize($data); | |
135 | return $item; | |
136 | } | |
137 | else { | |
138 | // item in queue is reserved | |
139 | return FALSE; | |
140 | } | |
141 | } | |
142 | // nothing in queue | |
143 | return FALSE; | |
144 | } | |
145 | ||
146 | /** | |
147 | * Get the next item | |
148 | * | |
dd244018 | 149 | * @param int|\seconds $leaseTime seconds |
6a488035 TO |
150 | * |
151 | * @return object with key 'data' that matches the inputted data | |
152 | */ | |
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; | |
158 | ||
159 | $item = new stdClass(); | |
160 | $item->id = $id; | |
161 | $item->data = unserialize($data); | |
162 | return $item; | |
163 | } | |
164 | // nothing in queue | |
165 | return FALSE; | |
166 | } | |
167 | ||
168 | /** | |
169 | * Remove an item from the queue | |
170 | * | |
171 | * @param $item object The item returned by claimItem | |
172 | */ | |
173 | function deleteItem($item) { | |
174 | unset($this->items[$item->id]); | |
175 | unset($this->releaseTimes[$item->id]); | |
176 | } | |
177 | ||
178 | /** | |
179 | * Return an item that could not be processed | |
180 | * | |
da6b46f4 EM |
181 | * @param The $item |
182 | * | |
183 | * @internal param object $dao The item returned by claimItem | |
6a488035 TO |
184 | * |
185 | * @return bool | |
186 | */ | |
187 | function releaseItem($item) { | |
188 | unset($this->releaseTimes[$item->id]); | |
189 | } | |
190 | } | |
191 |