2009-06-29 03:38:31 +09:00
|
|
|
<?php
|
|
|
|
/**
|
2009-08-26 07:12:20 +09:00
|
|
|
* StatusNet, the distributed open-source microblogging tool
|
2009-06-29 03:38:31 +09: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-26 07:12:20 +09:00
|
|
|
* @package StatusNet
|
2009-08-26 07:19:04 +09:00
|
|
|
* @author Evan Prodromou <evan@status.net>
|
2010-01-13 12:57:15 +09:00
|
|
|
* @author Brion Vibber <brion@status.net>
|
|
|
|
* @copyright 2009-2010 StatusNet, Inc.
|
2009-06-29 03:38:31 +09:00
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
2009-08-26 07:16:46 +09:00
|
|
|
* @link http://status.net/
|
2009-06-29 03:38:31 +09:00
|
|
|
*/
|
|
|
|
|
|
|
|
class DBQueueManager extends QueueManager
|
|
|
|
{
|
2010-01-13 12:57:15 +09:00
|
|
|
/**
|
2010-01-23 05:52:36 +09:00
|
|
|
* Saves an object reference into the queue item table.
|
2010-01-13 12:57:15 +09:00
|
|
|
* @return boolean true on success
|
|
|
|
* @throws ServerException on failure
|
|
|
|
*/
|
|
|
|
public function enqueue($object, $queue)
|
2009-06-29 03:38:31 +09:00
|
|
|
{
|
|
|
|
$qi = new Queue_item();
|
|
|
|
|
2010-01-23 05:52:36 +09:00
|
|
|
$qi->frame = $this->encode($object);
|
2009-06-29 03:38:31 +09:00
|
|
|
$qi->transport = $queue;
|
2010-01-23 05:52:36 +09:00
|
|
|
$qi->created = common_sql_now();
|
2009-06-29 03:38:31 +09:00
|
|
|
$result = $qi->insert();
|
|
|
|
|
2013-11-14 02:15:21 +09:00
|
|
|
if ($result === false) {
|
2009-06-29 03:38:31 +09:00
|
|
|
common_log_db_error($qi, 'INSERT', __FILE__);
|
|
|
|
throw new ServerException('DB error inserting queue item');
|
|
|
|
}
|
|
|
|
|
2010-01-13 12:57:15 +09:00
|
|
|
$this->stats('enqueued', $queue);
|
|
|
|
|
2009-06-29 03:38:31 +09:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-01-13 12:57:15 +09:00
|
|
|
/**
|
2010-01-23 05:52:36 +09:00
|
|
|
* Poll every 10 seconds for new events during idle periods.
|
2010-01-13 12:57:15 +09:00
|
|
|
* We'll look in more often when there's data available.
|
|
|
|
*
|
|
|
|
* @return int seconds
|
|
|
|
*/
|
|
|
|
public function pollInterval()
|
|
|
|
{
|
2010-01-23 05:52:36 +09:00
|
|
|
return 10;
|
2010-01-13 12:57:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a polling cycle during idle processing in the input loop.
|
2010-01-23 05:52:36 +09:00
|
|
|
* @return boolean true if we should poll again for more data immediately
|
2010-01-13 12:57:15 +09:00
|
|
|
*/
|
|
|
|
public function poll()
|
2009-07-04 13:31:28 +09:00
|
|
|
{
|
2013-10-05 20:12:16 +09:00
|
|
|
//$this->_log(LOG_DEBUG, 'Checking for notices...');
|
2010-02-17 02:01:59 +09:00
|
|
|
$qi = Queue_item::top($this->activeQueues());
|
2015-04-09 19:15:34 +09:00
|
|
|
if (!$qi instanceof Queue_item) {
|
2013-10-05 20:12:16 +09:00
|
|
|
//$this->_log(LOG_DEBUG, 'No notices waiting; idling.');
|
2010-01-13 12:57:15 +09:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-26 01:15:34 +09:00
|
|
|
try {
|
|
|
|
$item = $this->decode($qi->frame);
|
|
|
|
} catch (Exception $e) {
|
2015-04-09 19:15:34 +09:00
|
|
|
$this->_log(LOG_INFO, "[{$qi->transport}] Discarding: ".$e->getMessage());
|
2013-10-26 01:15:34 +09:00
|
|
|
$this->_done($qi);
|
|
|
|
return true;
|
|
|
|
}
|
2010-01-23 05:52:36 +09:00
|
|
|
|
2013-10-26 01:15:34 +09:00
|
|
|
$rep = $this->logrep($item);
|
2015-04-09 19:15:34 +09:00
|
|
|
$this->_log(LOG_DEBUG, "Got {$rep} for transport {$qi->transport}");
|
2013-10-26 01:15:34 +09:00
|
|
|
|
2015-04-09 19:15:34 +09:00
|
|
|
$handler = $this->getHandler($qi->transport);
|
2013-10-26 01:15:34 +09:00
|
|
|
if ($handler) {
|
|
|
|
if ($handler->handle($item)) {
|
2015-04-09 19:15:34 +09:00
|
|
|
$this->_log(LOG_INFO, "[{$qi->transport}:$rep] Successfully handled item");
|
2010-01-23 05:52:36 +09:00
|
|
|
$this->_done($qi);
|
2013-10-26 01:15:34 +09:00
|
|
|
} else {
|
2015-04-09 19:15:34 +09:00
|
|
|
$this->_log(LOG_INFO, "[{$qi->transport}:$rep] Failed to handle item");
|
2013-10-26 01:15:34 +09:00
|
|
|
$this->_fail($qi);
|
2009-07-04 13:31:28 +09:00
|
|
|
}
|
2010-01-13 12:57:15 +09:00
|
|
|
} else {
|
2015-04-09 19:15:34 +09:00
|
|
|
$this->noHandlerFound($qi, $rep);
|
2009-07-04 13:31:28 +09:00
|
|
|
}
|
2010-01-13 12:57:15 +09:00
|
|
|
return true;
|
2009-07-04 13:31:28 +09:00
|
|
|
}
|
|
|
|
|
2015-04-09 19:15:34 +09:00
|
|
|
// What to do if no handler was found. For example, the OpportunisticQM
|
|
|
|
// should avoid deleting items just because it can't reach XMPP queues etc.
|
|
|
|
protected function noHandlerFound(Queue_item $qi, $rep=null) {
|
|
|
|
$this->_log(LOG_INFO, "[{$qi->transport}:{$rep}] No handler for queue {$qi->transport}; discarding.");
|
|
|
|
$this->_done($qi);
|
|
|
|
}
|
|
|
|
|
2010-01-13 12:57:15 +09:00
|
|
|
/**
|
|
|
|
* Delete our claimed item from the queue after successful processing.
|
|
|
|
*
|
2010-01-23 05:52:36 +09:00
|
|
|
* @param QueueItem $qi
|
2010-01-13 12:57:15 +09:00
|
|
|
*/
|
2015-04-09 19:15:34 +09:00
|
|
|
protected function _done(Queue_item $qi)
|
2009-06-29 03:38:31 +09:00
|
|
|
{
|
2010-01-23 05:52:36 +09:00
|
|
|
if (empty($qi->claimed)) {
|
2015-04-09 19:15:34 +09:00
|
|
|
$this->_log(LOG_WARNING, "Reluctantly releasing unclaimed queue item {$qi->id} from {$qi->transport}");
|
2009-06-29 03:38:31 +09:00
|
|
|
}
|
2010-01-23 05:52:36 +09:00
|
|
|
$qi->delete();
|
2009-06-29 03:38:31 +09:00
|
|
|
|
2015-04-09 19:15:34 +09:00
|
|
|
$this->stats('handled', $qi->transport);
|
2009-07-02 01:10:25 +09:00
|
|
|
}
|
|
|
|
|
2010-01-13 12:57:15 +09:00
|
|
|
/**
|
|
|
|
* Free our claimed queue item for later reprocessing in case of
|
|
|
|
* temporary failure.
|
|
|
|
*
|
2010-01-23 05:52:36 +09:00
|
|
|
* @param QueueItem $qi
|
2010-01-13 12:57:15 +09:00
|
|
|
*/
|
2015-04-09 19:15:34 +09:00
|
|
|
protected function _fail(Queue_item $qi, $releaseOnly=false)
|
2009-07-02 01:10:25 +09:00
|
|
|
{
|
2010-01-23 05:52:36 +09:00
|
|
|
if (empty($qi->claimed)) {
|
2015-04-09 19:15:34 +09:00
|
|
|
$this->_log(LOG_WARNING, "[{$qi->transport}:item {$qi->id}] Ignoring failure for unclaimed queue item");
|
2009-07-02 01:10:25 +09:00
|
|
|
} else {
|
2010-06-27 04:07:32 +09:00
|
|
|
$qi->releaseClaim();
|
2009-07-02 01:10:25 +09:00
|
|
|
}
|
|
|
|
|
2015-04-09 19:15:34 +09:00
|
|
|
if (!$releaseOnly) {
|
|
|
|
$this->stats('error', $qi->transport);
|
|
|
|
}
|
2009-06-29 03:38:31 +09:00
|
|
|
}
|
|
|
|
}
|