3 * StatusNet, the distributed open-source microblogging tool
5 * Simple-minded queue manager for storing items in the database
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * @category QueueManager
24 * @author Evan Prodromou <evan@status.net>
25 * @copyright 2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 class DBQueueManager extends QueueManager
34 function enqueue($object, $queue)
38 $qi = new Queue_item();
40 $qi->notice_id = $notice->id;
41 $qi->transport = $queue;
42 $qi->created = $notice->created;
43 $result = $qi->insert();
46 common_log_db_error($qi, 'INSERT', __FILE__);
47 throw new ServerException('DB error inserting queue item');
53 function service($queue, $handler)
56 $this->_log(LOG_DEBUG, 'Checking for notices...');
57 $timeout = $handler->timeout();
58 $notice = $this->_nextItem($queue, $timeout);
60 $this->_log(LOG_DEBUG, 'No notices waiting; idling.');
61 // Nothing in the queue. Do you
62 // have other tasks, like servicing your
63 // XMPP connection, to do?
64 $handler->idle(QUEUE_HANDLER_MISS_IDLE);
66 $this->_log(LOG_INFO, 'Got notice '. $notice->id);
68 if ($handler->handle_notice($notice)) {
69 $this->_log(LOG_INFO, 'Successfully handled notice '. $notice->id);
70 $this->_done($notice, $queue);
72 $this->_log(LOG_INFO, 'Failed to handle notice '. $notice->id);
73 $this->_fail($notice, $queue);
75 // Chance to e.g. service your XMPP connection
76 $this->_log(LOG_DEBUG, 'Idling after success.');
77 $handler->idle(QUEUE_HANDLER_HIT_IDLE);
79 // XXX: when do we give up?
83 function _nextItem($queue, $timeout=null)
91 $qi = Queue_item::top($queue);
93 $this->_log(LOG_DEBUG, "No new queue items, sleeping $sleeptime seconds.");
97 $notice = Notice::staticGet('id', $qi->notice_id);
98 if (!empty($notice)) {
101 $this->_log(LOG_INFO, 'dequeued non-existent notice ' . $notice->id);
108 } while (empty($result) && (is_null($timeout) || (time() - $start) < $timeout));
113 function _done($object, $queue)
115 // XXX: right now, we only handle notices
119 $qi = Queue_item::pkeyGet(array('notice_id' => $notice->id,
120 'transport' => $queue));
123 $this->_log(LOG_INFO, 'Cannot find queue item for notice '.$notice->id.', queue '.$queue);
125 if (empty($qi->claimed)) {
126 $this->_log(LOG_WARNING, 'Reluctantly releasing unclaimed queue item '.
127 'for '.$notice->id.', queue '.$queue);
134 $this->_log(LOG_INFO, 'done with notice ID = ' . $notice->id);
140 function _fail($object, $queue)
142 // XXX: right now, we only handle notices
146 $qi = Queue_item::pkeyGet(array('notice_id' => $notice->id,
147 'transport' => $queue));
150 $this->_log(LOG_INFO, 'Cannot find queue item for notice '.$notice->id.', queue '.$queue);
152 if (empty($qi->claimed)) {
153 $this->_log(LOG_WARNING, 'Ignoring failure for unclaimed queue item '.
154 'for '.$notice->id.', queue '.$queue);
163 $this->_log(LOG_INFO, 'done with notice ID = ' . $notice->id);
169 function _log($level, $msg)
171 common_log($level, 'DBQueueManager: '.$msg);