2009-06-28 19:38:31 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2009-08-25 23:12:20 +01:00
|
|
|
* StatusNet, the distributed open-source microblogging tool
|
2009-06-28 19:38:31 +01:00
|
|
|
*
|
|
|
|
* Simple-minded queue manager for storing items in the database
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* LICENCE: This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @category QueueManager
|
2009-08-25 23:12:20 +01:00
|
|
|
* @package StatusNet
|
2009-08-25 23:19:04 +01:00
|
|
|
* @author Evan Prodromou <evan@status.net>
|
2010-01-13 03:57:15 +00:00
|
|
|
* @author Brion Vibber <brion@status.net>
|
|
|
|
* @copyright 2009-2010 StatusNet, Inc.
|
2009-06-28 19:38:31 +01:00
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
2009-08-25 23:16:46 +01:00
|
|
|
* @link http://status.net/
|
2009-06-28 19:38:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
class DBQueueManager extends QueueManager
|
|
|
|
{
|
2010-01-13 03:57:15 +00:00
|
|
|
/**
|
2010-01-22 20:52:36 +00:00
|
|
|
* Saves an object reference into the queue item table.
|
2010-01-13 03:57:15 +00:00
|
|
|
* @return boolean true on success
|
|
|
|
* @throws ServerException on failure
|
|
|
|
*/
|
|
|
|
public function enqueue($object, $queue)
|
2009-06-28 19:38:31 +01:00
|
|
|
{
|
|
|
|
$qi = new Queue_item();
|
|
|
|
|
2010-01-22 20:52:36 +00:00
|
|
|
$qi->frame = $this->encode($object);
|
2009-06-28 19:38:31 +01:00
|
|
|
$qi->transport = $queue;
|
2010-01-22 20:52:36 +00:00
|
|
|
$qi->created = common_sql_now();
|
2009-06-28 19:38:31 +01:00
|
|
|
$result = $qi->insert();
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
common_log_db_error($qi, 'INSERT', __FILE__);
|
|
|
|
throw new ServerException('DB error inserting queue item');
|
|
|
|
}
|
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
$this->stats('enqueued', $queue);
|
|
|
|
|
2009-06-28 19:38:31 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
/**
|
2010-01-22 20:52:36 +00:00
|
|
|
* Poll every 10 seconds for new events during idle periods.
|
2010-01-13 03:57:15 +00:00
|
|
|
* We'll look in more often when there's data available.
|
|
|
|
*
|
|
|
|
* @return int seconds
|
|
|
|
*/
|
|
|
|
public function pollInterval()
|
|
|
|
{
|
2010-01-22 20:52:36 +00:00
|
|
|
return 10;
|
2010-01-13 03:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a polling cycle during idle processing in the input loop.
|
2010-01-22 20:52:36 +00:00
|
|
|
* @return boolean true if we should poll again for more data immediately
|
2010-01-13 03:57:15 +00:00
|
|
|
*/
|
|
|
|
public function poll()
|
2009-07-04 05:31:28 +01:00
|
|
|
{
|
2013-10-05 12:12:16 +01:00
|
|
|
//$this->_log(LOG_DEBUG, 'Checking for notices...');
|
2010-02-16 17:01:59 +00:00
|
|
|
$qi = Queue_item::top($this->activeQueues());
|
2010-01-22 20:52:36 +00:00
|
|
|
if (empty($qi)) {
|
2013-10-05 12:12:16 +01:00
|
|
|
//$this->_log(LOG_DEBUG, 'No notices waiting; idling.');
|
2010-01-13 03:57:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-22 20:52:36 +00:00
|
|
|
$queue = $qi->transport;
|
2013-10-25 17:15:34 +01:00
|
|
|
try {
|
|
|
|
$item = $this->decode($qi->frame);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->_log(LOG_INFO, "[$queue] Discarding: ".$e->getMessage());
|
|
|
|
$this->_done($qi);
|
|
|
|
return true;
|
|
|
|
}
|
2010-01-22 20:52:36 +00:00
|
|
|
|
2013-10-25 17:15:34 +01:00
|
|
|
$rep = $this->logrep($item);
|
|
|
|
$this->_log(LOG_DEBUG, "Got $rep for transport $queue");
|
|
|
|
|
|
|
|
$handler = $this->getHandler($queue);
|
|
|
|
if ($handler) {
|
|
|
|
if ($handler->handle($item)) {
|
|
|
|
$this->_log(LOG_INFO, "[$queue:$rep] Successfully handled item");
|
2010-01-22 20:52:36 +00:00
|
|
|
$this->_done($qi);
|
2013-10-25 17:15:34 +01:00
|
|
|
} else {
|
|
|
|
$this->_log(LOG_INFO, "[$queue:$rep] Failed to handle item");
|
|
|
|
$this->_fail($qi);
|
2009-07-04 05:31:28 +01:00
|
|
|
}
|
2010-01-13 03:57:15 +00:00
|
|
|
} else {
|
2013-10-25 17:15:34 +01:00
|
|
|
$this->_log(LOG_INFO, "[$queue:$rep] No handler for queue $queue; discarding.");
|
2010-09-20 21:19:58 +01:00
|
|
|
$this->_done($qi);
|
2009-07-04 05:31:28 +01:00
|
|
|
}
|
2010-01-13 03:57:15 +00:00
|
|
|
return true;
|
2009-07-04 05:31:28 +01:00
|
|
|
}
|
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
/**
|
|
|
|
* Delete our claimed item from the queue after successful processing.
|
|
|
|
*
|
2010-01-22 20:52:36 +00:00
|
|
|
* @param QueueItem $qi
|
2010-01-13 03:57:15 +00:00
|
|
|
*/
|
2010-01-22 20:52:36 +00:00
|
|
|
protected function _done($qi)
|
2009-06-28 19:38:31 +01:00
|
|
|
{
|
2010-01-22 20:52:36 +00:00
|
|
|
$queue = $qi->transport;
|
2009-06-28 19:38:31 +01:00
|
|
|
|
2010-01-22 20:52:36 +00:00
|
|
|
if (empty($qi->claimed)) {
|
|
|
|
$this->_log(LOG_WARNING, "Reluctantly releasing unclaimed queue item $qi->id from $qi->queue");
|
2009-06-28 19:38:31 +01:00
|
|
|
}
|
2010-01-22 20:52:36 +00:00
|
|
|
$qi->delete();
|
2009-06-28 19:38:31 +01:00
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
$this->stats('handled', $queue);
|
2009-07-01 17:10:25 +01:00
|
|
|
}
|
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
/**
|
|
|
|
* Free our claimed queue item for later reprocessing in case of
|
|
|
|
* temporary failure.
|
|
|
|
*
|
2010-01-22 20:52:36 +00:00
|
|
|
* @param QueueItem $qi
|
2010-01-13 03:57:15 +00:00
|
|
|
*/
|
2010-01-22 20:52:36 +00:00
|
|
|
protected function _fail($qi)
|
2009-07-01 17:10:25 +01:00
|
|
|
{
|
2010-01-22 20:52:36 +00:00
|
|
|
$queue = $qi->transport;
|
2009-07-01 17:10:25 +01:00
|
|
|
|
2010-01-22 20:52:36 +00:00
|
|
|
if (empty($qi->claimed)) {
|
|
|
|
$this->_log(LOG_WARNING, "[$queue:item $qi->id] Ignoring failure for unclaimed queue item");
|
2009-07-01 17:10:25 +01:00
|
|
|
} else {
|
2010-06-26 20:07:32 +01:00
|
|
|
$qi->releaseClaim();
|
2009-07-01 17:10:25 +01:00
|
|
|
}
|
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
$this->stats('error', $queue);
|
2009-06-28 19:38:31 +01:00
|
|
|
}
|
|
|
|
}
|