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();
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
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());
|
2010-01-23 05:52:36 +09:00
|
|
|
if (empty($qi)) {
|
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;
|
|
|
|
}
|
|
|
|
|
2010-01-23 05:52:36 +09:00
|
|
|
$queue = $qi->transport;
|
2013-10-26 01:15:34 +09: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-23 05:52:36 +09:00
|
|
|
|
2013-10-26 01:15:34 +09: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-23 05:52:36 +09:00
|
|
|
$this->_done($qi);
|
2013-10-26 01:15:34 +09:00
|
|
|
} else {
|
|
|
|
$this->_log(LOG_INFO, "[$queue:$rep] Failed to handle item");
|
|
|
|
$this->_fail($qi);
|
2009-07-04 13:31:28 +09:00
|
|
|
}
|
2010-01-13 12:57:15 +09:00
|
|
|
} else {
|
2013-10-26 01:15:34 +09:00
|
|
|
$this->_log(LOG_INFO, "[$queue:$rep] No handler for queue $queue; discarding.");
|
2010-09-21 05:19:58 +09:00
|
|
|
$this->_done($qi);
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2010-01-23 05:52:36 +09:00
|
|
|
protected function _done($qi)
|
2009-06-29 03:38:31 +09:00
|
|
|
{
|
2010-01-23 05:52:36 +09:00
|
|
|
$queue = $qi->transport;
|
2009-06-29 03:38:31 +09:00
|
|
|
|
2010-01-23 05:52:36 +09:00
|
|
|
if (empty($qi->claimed)) {
|
|
|
|
$this->_log(LOG_WARNING, "Reluctantly releasing unclaimed queue item $qi->id from $qi->queue");
|
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
|
|
|
|
2010-01-13 12:57:15 +09:00
|
|
|
$this->stats('handled', $queue);
|
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
|
|
|
*/
|
2010-01-23 05:52:36 +09:00
|
|
|
protected function _fail($qi)
|
2009-07-02 01:10:25 +09:00
|
|
|
{
|
2010-01-23 05:52:36 +09:00
|
|
|
$queue = $qi->transport;
|
2009-07-02 01:10:25 +09:00
|
|
|
|
2010-01-23 05:52:36 +09:00
|
|
|
if (empty($qi->claimed)) {
|
|
|
|
$this->_log(LOG_WARNING, "[$queue: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
|
|
|
}
|
|
|
|
|
2010-01-13 12:57:15 +09:00
|
|
|
$this->stats('error', $queue);
|
2009-06-29 03:38:31 +09:00
|
|
|
}
|
|
|
|
}
|