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 * @author Brion Vibber <brion@status.net>
26 * @copyright 2009-2010 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://status.net/
31 class DBQueueManager extends QueueManager
34 * Saves a notice object reference into the queue item table.
35 * @return boolean true on success
36 * @throws ServerException on failure
38 public function enqueue($object, $queue)
42 $qi = new Queue_item();
44 $qi->notice_id = $notice->id;
45 $qi->transport = $queue;
46 $qi->created = $notice->created;
47 $result = $qi->insert();
50 common_log_db_error($qi, 'INSERT', __FILE__);
51 throw new ServerException('DB error inserting queue item');
54 $this->stats('enqueued', $queue);
60 * Poll every minute for new events during idle periods.
61 * We'll look in more often when there's data available.
65 public function pollInterval()
71 * Run a polling cycle during idle processing in the input loop.
72 * @return boolean true if we had a hit
74 public function poll()
76 $this->_log(LOG_DEBUG, 'Checking for notices...');
77 $item = $this->_nextItem();
78 if ($item === false) {
79 $this->_log(LOG_DEBUG, 'No notices waiting; idling.');
83 // We dequeued an entry for a deleted or invalid notice.
84 // Consider it a hit for poll rate purposes.
88 list($queue, $notice) = $item;
89 $this->_log(LOG_INFO, 'Got notice '. $notice->id . ' for transport ' . $queue);
92 $handler = $this->getHandler($queue);
94 if ($handler->handle_notice($notice)) {
95 $this->_log(LOG_INFO, "[$queue:notice $notice->id] Successfully handled notice");
96 $this->_done($notice, $queue);
98 $this->_log(LOG_INFO, "[$queue:notice $notice->id] Failed to handle notice");
99 $this->_fail($notice, $queue);
102 $this->_log(LOG_INFO, "[$queue:notice $notice->id] No handler for queue $queue; discarding.");
103 $this->_done($notice, $queue);
109 * Pop the oldest unclaimed item off the queue set and claim it.
111 * @return mixed false if no items; true if bogus hit; otherwise array(string, Notice)
112 * giving the queue transport name.
114 protected function _nextItem()
119 $qi = Queue_item::top();
124 $queue = $qi->transport;
125 $notice = Notice::staticGet('id', $qi->notice_id);
126 if (empty($notice)) {
127 $this->_log(LOG_INFO, "[$queue:notice $notice->id] dequeued non-existent notice");
133 return array($queue, $notice);
137 * Delete our claimed item from the queue after successful processing.
139 * @param Notice $object
140 * @param string $queue
142 protected function _done($object, $queue)
144 // XXX: right now, we only handle notices
148 $qi = Queue_item::pkeyGet(array('notice_id' => $notice->id,
149 'transport' => $queue));
152 $this->_log(LOG_INFO, "[$queue:notice $notice->id] Cannot find queue item");
154 if (empty($qi->claimed)) {
155 $this->_log(LOG_WARNING, "[$queue:notice $notice->id] Reluctantly releasing unclaimed queue item");
161 $this->_log(LOG_INFO, "[$queue:notice $notice->id] done with item");
162 $this->stats('handled', $queue);
168 * Free our claimed queue item for later reprocessing in case of
171 * @param Notice $object
172 * @param string $queue
174 protected function _fail($object, $queue)
176 // XXX: right now, we only handle notices
180 $qi = Queue_item::pkeyGet(array('notice_id' => $notice->id,
181 'transport' => $queue));
184 $this->_log(LOG_INFO, "[$queue:notice $notice->id] Cannot find queue item");
186 if (empty($qi->claimed)) {
187 $this->_log(LOG_WARNING, "[$queue:notice $notice->id] Ignoring failure for unclaimed queue item");
196 $this->_log(LOG_INFO, "[$queue:notice $notice->id] done with queue item");
197 $this->stats('error', $queue);
202 protected function _log($level, $msg)
204 common_log($level, 'DBQueueManager: '.$msg);