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
|
|
|
*
|
|
|
|
* Abstract class for queue managers
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
* @author Sarven Capadisli <csarven@status.net>
|
2009-08-25 23:12:20 +01:00
|
|
|
* @copyright 2009 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
|
|
|
*/
|
|
|
|
|
|
|
|
require_once 'Stomp.php';
|
2010-01-29 00:49:32 +00:00
|
|
|
require_once 'Stomp/Exception.php';
|
2009-06-28 19:38:31 +01:00
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
class StompQueueManager extends QueueManager
|
2009-06-28 19:38:31 +01:00
|
|
|
{
|
2010-01-29 00:49:32 +00:00
|
|
|
protected $servers;
|
|
|
|
protected $username;
|
|
|
|
protected $password;
|
|
|
|
protected $base;
|
2010-01-26 19:49:49 +00:00
|
|
|
protected $control;
|
2010-01-29 00:49:32 +00:00
|
|
|
|
|
|
|
protected $useTransactions = true;
|
2010-01-30 18:15:17 +00:00
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
protected $sites = array();
|
2010-01-26 19:49:49 +00:00
|
|
|
protected $subscriptions = array();
|
2009-06-28 19:38:31 +01:00
|
|
|
|
2010-01-29 00:49:32 +00:00
|
|
|
protected $cons = array(); // all open connections
|
|
|
|
protected $disconnect = array();
|
|
|
|
protected $transaction = array();
|
|
|
|
protected $transactionCount = array();
|
|
|
|
protected $defaultIdx = 0;
|
2010-01-22 20:35:05 +00:00
|
|
|
|
2009-06-28 19:38:31 +01:00
|
|
|
function __construct()
|
|
|
|
{
|
2010-01-13 03:57:15 +00:00
|
|
|
parent::__construct();
|
2010-01-29 00:49:32 +00:00
|
|
|
$server = common_config('queue', 'stomp_server');
|
|
|
|
if (is_array($server)) {
|
|
|
|
$this->servers = $server;
|
|
|
|
} else {
|
|
|
|
$this->servers = array($server);
|
|
|
|
}
|
2009-06-28 19:38:31 +01:00
|
|
|
$this->username = common_config('queue', 'stomp_username');
|
|
|
|
$this->password = common_config('queue', 'stomp_password');
|
|
|
|
$this->base = common_config('queue', 'queue_basename');
|
2010-01-26 19:49:49 +00:00
|
|
|
$this->control = common_config('queue', 'control_channel');
|
2009-06-28 19:38:31 +01:00
|
|
|
}
|
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
/**
|
|
|
|
* Tell the i/o master we only need a single instance to cover
|
|
|
|
* all sites running in this process.
|
|
|
|
*/
|
|
|
|
public static function multiSite()
|
2009-06-28 19:38:31 +01:00
|
|
|
{
|
2010-01-13 03:57:15 +00:00
|
|
|
return IoManager::INSTANCE_PER_PROCESS;
|
|
|
|
}
|
2009-06-28 19:38:31 +01:00
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
/**
|
|
|
|
* Record each site we'll be handling input for in this process,
|
|
|
|
* so we can listen to the necessary queues for it.
|
|
|
|
*
|
|
|
|
* @fixme possibly actually do subscription here to save another
|
|
|
|
* loop over all sites later?
|
2010-01-14 08:19:25 +00:00
|
|
|
* @fixme possibly don't assume it's the current site
|
2010-01-13 03:57:15 +00:00
|
|
|
*/
|
|
|
|
public function addSite($server)
|
|
|
|
{
|
|
|
|
$this->sites[] = $server;
|
2010-01-14 08:19:25 +00:00
|
|
|
$this->initialize();
|
|
|
|
}
|
|
|
|
|
2010-01-26 19:49:49 +00:00
|
|
|
/**
|
|
|
|
* Optional; ping any running queue handler daemons with a notification
|
|
|
|
* such as announcing a new site to handle or requesting clean shutdown.
|
|
|
|
* This avoids having to restart all the daemons manually to update configs
|
|
|
|
* and such.
|
|
|
|
*
|
|
|
|
* Currently only relevant for multi-site queue managers such as Stomp.
|
|
|
|
*
|
|
|
|
* @param string $event event key
|
|
|
|
* @param string $param optional parameter to append to key
|
|
|
|
* @return boolean success
|
|
|
|
*/
|
|
|
|
public function sendControlSignal($event, $param='')
|
|
|
|
{
|
|
|
|
$message = $event;
|
|
|
|
if ($param != '') {
|
|
|
|
$message .= ':' . $param;
|
|
|
|
}
|
|
|
|
$this->_connect();
|
2010-01-29 00:49:32 +00:00
|
|
|
$result = $this->_send($this->control,
|
|
|
|
$message,
|
|
|
|
array ('created' => common_sql_now()));
|
2010-01-26 19:49:49 +00:00
|
|
|
if ($result) {
|
|
|
|
$this->_log(LOG_INFO, "Sent control ping to queue daemons: $message");
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
$this->_log(LOG_ERR, "Failed sending control ping to queue daemons: $message");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-01-14 08:19:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiate the appropriate QueueHandler class for the given queue.
|
|
|
|
*
|
|
|
|
* @param string $queue
|
|
|
|
* @return mixed QueueHandler or null
|
|
|
|
*/
|
|
|
|
function getHandler($queue)
|
|
|
|
{
|
2010-01-26 19:49:49 +00:00
|
|
|
$handlers = $this->handlers[$this->currentSite()];
|
2010-01-14 08:19:25 +00:00
|
|
|
if (isset($handlers[$queue])) {
|
|
|
|
$class = $handlers[$queue];
|
|
|
|
if (class_exists($class)) {
|
|
|
|
return new $class();
|
|
|
|
} else {
|
|
|
|
common_log(LOG_ERR, "Nonexistent handler class '$class' for queue '$queue'");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
common_log(LOG_ERR, "Requested handler for unkown queue '$queue'");
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of all registered queue transport names.
|
|
|
|
*
|
|
|
|
* @return array of strings
|
|
|
|
*/
|
|
|
|
function getQueues()
|
|
|
|
{
|
2010-01-22 00:42:50 +00:00
|
|
|
$group = $this->activeGroup();
|
2010-01-26 19:49:49 +00:00
|
|
|
$site = $this->currentSite();
|
2010-01-22 00:42:50 +00:00
|
|
|
if (empty($this->groups[$site][$group])) {
|
2010-01-15 19:13:06 +00:00
|
|
|
return array();
|
|
|
|
} else {
|
2010-01-22 00:42:50 +00:00
|
|
|
return array_keys($this->groups[$site][$group]);
|
2010-01-15 19:13:06 +00:00
|
|
|
}
|
2010-01-14 08:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a queue transport name and handler class for your plugin.
|
|
|
|
* Only registered transports will be reliably picked up!
|
|
|
|
*
|
|
|
|
* @param string $transport
|
|
|
|
* @param string $class
|
2010-01-22 00:42:50 +00:00
|
|
|
* @param string $group
|
2010-01-14 08:19:25 +00:00
|
|
|
*/
|
2010-01-22 00:42:50 +00:00
|
|
|
public function connect($transport, $class, $group='queuedaemon')
|
2010-01-14 08:19:25 +00:00
|
|
|
{
|
2010-01-26 19:49:49 +00:00
|
|
|
$this->handlers[$this->currentSite()][$transport] = $class;
|
|
|
|
$this->groups[$this->currentSite()][$group][$transport] = $class;
|
2009-06-28 19:38:31 +01:00
|
|
|
}
|
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
/**
|
|
|
|
* Saves a notice object reference into the queue item table.
|
|
|
|
* @return boolean true on success
|
2010-01-29 00:49:32 +00:00
|
|
|
* @throws StompException on connection or send error
|
2010-01-13 03:57:15 +00:00
|
|
|
*/
|
|
|
|
public function enqueue($object, $queue)
|
2010-01-29 00:49:32 +00:00
|
|
|
{
|
|
|
|
$this->_connect();
|
|
|
|
return $this->_doEnqueue($object, $queue, $this->defaultIdx);
|
|
|
|
}
|
2010-01-30 18:15:17 +00:00
|
|
|
|
2010-01-29 00:49:32 +00:00
|
|
|
/**
|
|
|
|
* Saves a notice object reference into the queue item table
|
|
|
|
* on the given connection.
|
|
|
|
*
|
|
|
|
* @return boolean true on success
|
|
|
|
* @throws StompException on connection or send error
|
|
|
|
*/
|
|
|
|
protected function _doEnqueue($object, $queue, $idx)
|
2009-06-28 19:38:31 +01:00
|
|
|
{
|
2010-01-22 00:42:50 +00:00
|
|
|
$msg = $this->encode($object);
|
|
|
|
$rep = $this->logrep($object);
|
2009-06-28 19:38:31 +01:00
|
|
|
|
2010-01-28 17:52:35 +00:00
|
|
|
$props = array('created' => common_sql_now());
|
2010-01-29 00:49:32 +00:00
|
|
|
if ($this->isPersistent($queue)) {
|
2010-01-28 17:52:35 +00:00
|
|
|
$props['persistent'] = 'true';
|
|
|
|
}
|
2010-01-29 00:49:32 +00:00
|
|
|
|
|
|
|
$con = $this->cons[$idx];
|
|
|
|
$host = $con->getServer();
|
|
|
|
$result = $con->send($this->queueName($queue), $msg, $props);
|
2009-06-28 19:38:31 +01:00
|
|
|
|
|
|
|
if (!$result) {
|
2010-01-29 00:49:32 +00:00
|
|
|
common_log(LOG_ERR, "Error sending $rep to $queue queue on $host");
|
2009-06-28 19:38:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-29 00:49:32 +00:00
|
|
|
common_log(LOG_DEBUG, "complete remote queueing $rep for $queue on $host");
|
2010-01-13 03:57:15 +00:00
|
|
|
$this->stats('enqueued', $queue);
|
2010-01-29 00:49:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether messages to this queue should be marked as persistent.
|
|
|
|
* Actual persistent storage depends on the queue server's configuration.
|
|
|
|
* @param string $queue
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isPersistent($queue)
|
|
|
|
{
|
|
|
|
$mode = common_config('queue', 'stomp_persistent');
|
|
|
|
if (is_array($mode)) {
|
|
|
|
return in_array($queue, $mode);
|
|
|
|
} else {
|
|
|
|
return (bool)$mode;
|
|
|
|
}
|
2009-06-28 19:38:31 +01:00
|
|
|
}
|
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
/**
|
|
|
|
* Send any sockets we're listening on to the IO manager
|
|
|
|
* to wait for input.
|
|
|
|
*
|
|
|
|
* @return array of resources
|
|
|
|
*/
|
|
|
|
public function getSockets()
|
2009-06-28 19:38:31 +01:00
|
|
|
{
|
2010-01-29 00:49:32 +00:00
|
|
|
$sockets = array();
|
|
|
|
foreach ($this->cons as $con) {
|
|
|
|
if ($con) {
|
|
|
|
$sockets[] = $con->getSocket();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $sockets;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Stomp connection object associated with the given socket.
|
|
|
|
* @param resource $socket
|
|
|
|
* @return int index into connections list
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
protected function connectionFromSocket($socket)
|
|
|
|
{
|
|
|
|
foreach ($this->cons as $i => $con) {
|
|
|
|
if ($con && $con->getSocket() === $socket) {
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Exception(__CLASS__ . " asked to read from unrecognized socket");
|
2010-01-13 03:57:15 +00:00
|
|
|
}
|
2009-06-28 19:38:31 +01:00
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
/**
|
|
|
|
* We've got input to handle on our socket!
|
|
|
|
* Read any waiting Stomp frame(s) and process them.
|
|
|
|
*
|
|
|
|
* @param resource $socket
|
|
|
|
* @return boolean ok on success
|
|
|
|
*/
|
|
|
|
public function handleInput($socket)
|
|
|
|
{
|
2010-01-29 00:49:32 +00:00
|
|
|
$idx = $this->connectionFromSocket($socket);
|
|
|
|
$con = $this->cons[$idx];
|
|
|
|
$host = $con->getServer();
|
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
$ok = true;
|
2010-01-29 00:49:32 +00:00
|
|
|
try {
|
|
|
|
$frames = $con->readFrames();
|
|
|
|
} catch (StompException $e) {
|
|
|
|
common_log(LOG_ERR, "Lost connection to $host: " . $e->getMessage());
|
|
|
|
$this->cons[$idx] = null;
|
|
|
|
$this->transaction[$idx] = null;
|
|
|
|
$this->disconnect[$idx] = time();
|
|
|
|
return false;
|
|
|
|
}
|
2010-01-13 03:57:15 +00:00
|
|
|
foreach ($frames as $frame) {
|
2010-01-26 19:49:49 +00:00
|
|
|
$dest = $frame->headers['destination'];
|
|
|
|
if ($dest == $this->control) {
|
2010-01-29 00:49:32 +00:00
|
|
|
if (!$this->handleControlSignal($idx, $frame)) {
|
2010-01-26 19:49:49 +00:00
|
|
|
// We got a control event that requests a shutdown;
|
|
|
|
// close out and stop handling anything else!
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2010-01-29 00:49:32 +00:00
|
|
|
$ok = $ok && $this->handleItem($idx, $frame);
|
2010-01-26 19:49:49 +00:00
|
|
|
}
|
2010-01-13 03:57:15 +00:00
|
|
|
}
|
|
|
|
return $ok;
|
|
|
|
}
|
2009-06-28 19:38:31 +01:00
|
|
|
|
2010-01-29 00:49:32 +00:00
|
|
|
/**
|
|
|
|
* Attempt to reconnect in background if we lost a connection.
|
|
|
|
*/
|
|
|
|
function idle()
|
|
|
|
{
|
|
|
|
$now = time();
|
|
|
|
foreach ($this->cons as $idx => $con) {
|
|
|
|
if (empty($con)) {
|
|
|
|
$age = $now - $this->disconnect[$idx];
|
|
|
|
if ($age >= 60) {
|
|
|
|
$this->_reconnect($idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
/**
|
|
|
|
* Initialize our connection and subscribe to all the queues
|
2010-01-29 00:49:32 +00:00
|
|
|
* we're going to need to handle... If multiple queue servers
|
|
|
|
* are configured for failover, we'll listen to all of them.
|
2010-01-13 03:57:15 +00:00
|
|
|
*
|
|
|
|
* Side effects: in multi-site mode, may reset site configuration.
|
|
|
|
*
|
|
|
|
* @param IoMaster $master process/event controller
|
|
|
|
* @return bool return false on failure
|
|
|
|
*/
|
|
|
|
public function start($master)
|
|
|
|
{
|
|
|
|
parent::start($master);
|
2010-01-29 00:49:32 +00:00
|
|
|
$this->_connectAll();
|
2010-01-26 19:49:49 +00:00
|
|
|
|
2010-01-28 04:51:04 +00:00
|
|
|
common_log(LOG_INFO, "Subscribing to $this->control");
|
2010-01-29 00:49:32 +00:00
|
|
|
foreach ($this->cons as $con) {
|
|
|
|
if ($con) {
|
|
|
|
$con->subscribe($this->control);
|
|
|
|
}
|
|
|
|
}
|
2010-01-13 03:57:15 +00:00
|
|
|
if ($this->sites) {
|
|
|
|
foreach ($this->sites as $server) {
|
|
|
|
StatusNet::init($server);
|
|
|
|
$this->doSubscribe();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->doSubscribe();
|
|
|
|
}
|
2010-01-29 00:49:32 +00:00
|
|
|
foreach ($this->cons as $i => $con) {
|
|
|
|
if ($con) {
|
|
|
|
$this->begin($i);
|
|
|
|
}
|
|
|
|
}
|
2010-01-13 03:57:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-01-30 18:15:17 +00:00
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
/**
|
|
|
|
* Subscribe to all the queues we're going to need to handle...
|
|
|
|
*
|
|
|
|
* Side effects: in multi-site mode, may reset site configuration.
|
|
|
|
*
|
|
|
|
* @return bool return false on failure
|
|
|
|
*/
|
|
|
|
public function finish()
|
|
|
|
{
|
2010-01-22 20:35:05 +00:00
|
|
|
// If there are any outstanding delivered messages we haven't processed,
|
|
|
|
// free them for another thread to take.
|
2010-01-29 00:49:32 +00:00
|
|
|
foreach ($this->cons as $i => $con) {
|
|
|
|
if ($con) {
|
|
|
|
$this->rollback($i);
|
|
|
|
$con->unsubscribe($this->control);
|
|
|
|
}
|
|
|
|
}
|
2010-01-13 03:57:15 +00:00
|
|
|
if ($this->sites) {
|
|
|
|
foreach ($this->sites as $server) {
|
|
|
|
StatusNet::init($server);
|
|
|
|
$this->doUnsubscribe();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->doUnsubscribe();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2010-01-26 19:49:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get identifier of the currently active site configuration
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function currentSite()
|
|
|
|
{
|
|
|
|
return common_config('site', 'server'); // @fixme switch to nickname
|
|
|
|
}
|
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
/**
|
2010-01-29 00:49:32 +00:00
|
|
|
* Lazy open a single connection to Stomp queue server.
|
|
|
|
* If multiple servers are configured, we let the Stomp client library
|
|
|
|
* worry about finding a working connection among them.
|
2010-01-13 03:57:15 +00:00
|
|
|
*/
|
|
|
|
protected function _connect()
|
|
|
|
{
|
2010-01-29 00:49:32 +00:00
|
|
|
if (empty($this->cons)) {
|
|
|
|
$list = $this->servers;
|
|
|
|
if (count($list) > 1) {
|
|
|
|
shuffle($list); // Randomize to spread load
|
|
|
|
$url = 'failover://(' . implode(',', $list) . ')';
|
2010-01-13 03:57:15 +00:00
|
|
|
} else {
|
2010-01-29 00:49:32 +00:00
|
|
|
$url = $list[0];
|
2010-01-13 03:57:15 +00:00
|
|
|
}
|
2010-01-29 00:49:32 +00:00
|
|
|
$con = $this->_doConnect($url);
|
|
|
|
$this->cons = array($con);
|
|
|
|
$this->transactionCount = array(0);
|
|
|
|
$this->transaction = array(null);
|
|
|
|
$this->disconnect = array(null);
|
2010-01-13 03:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-01 16:10:23 +01:00
|
|
|
|
2010-01-29 00:49:32 +00:00
|
|
|
/**
|
|
|
|
* Lazy open connections to all Stomp servers, if in manual failover
|
|
|
|
* mode. This means the queue servers don't speak to each other, so
|
|
|
|
* we have to listen to all of them to make sure we get all events.
|
|
|
|
*/
|
|
|
|
protected function _connectAll()
|
|
|
|
{
|
|
|
|
if (!common_config('queue', 'stomp_manual_failover')) {
|
|
|
|
return $this->_connect();
|
|
|
|
}
|
|
|
|
if (empty($this->cons)) {
|
|
|
|
$this->cons = array();
|
|
|
|
$this->transactionCount = array();
|
|
|
|
$this->transaction = array();
|
|
|
|
foreach ($this->servers as $idx => $server) {
|
|
|
|
try {
|
|
|
|
$this->cons[] = $this->_doConnect($server);
|
|
|
|
$this->disconnect[] = null;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// s'okay, we'll live
|
|
|
|
$this->cons[] = null;
|
|
|
|
$this->disconnect[] = time();
|
|
|
|
}
|
|
|
|
$this->transactionCount[] = 0;
|
|
|
|
$this->transaction[] = null;
|
|
|
|
}
|
|
|
|
if (empty($this->cons)) {
|
|
|
|
throw new ServerException("No queue servers reachable...");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _reconnect($idx)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$con = $this->_doConnect($this->servers[$idx]);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->_log(LOG_ERR, $e->getMessage());
|
|
|
|
$con = null;
|
|
|
|
}
|
|
|
|
if ($con) {
|
|
|
|
$this->cons[$idx] = $con;
|
|
|
|
$this->disconnect[$idx] = null;
|
2010-01-30 18:15:17 +00:00
|
|
|
|
2010-01-29 00:49:32 +00:00
|
|
|
// now we have to listen to everything...
|
|
|
|
// @fixme refactor this nicer. :P
|
|
|
|
$host = $con->getServer();
|
|
|
|
$this->_log(LOG_INFO, "Resubscribing to $this->control on $host");
|
|
|
|
$con->subscribe($this->control);
|
|
|
|
foreach ($this->subscriptions as $site => $queues) {
|
|
|
|
foreach ($queues as $queue) {
|
|
|
|
$this->_log(LOG_INFO, "Resubscribing to $queue on $host");
|
|
|
|
$con->subscribe($queue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->begin($idx);
|
|
|
|
} else {
|
|
|
|
// Try again later...
|
|
|
|
$this->disconnect[$idx] = time();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _doConnect($server)
|
|
|
|
{
|
|
|
|
$this->_log(LOG_INFO, "Connecting to '$server' as '$this->username'...");
|
|
|
|
$con = new LiberalStomp($server);
|
|
|
|
|
|
|
|
if ($con->connect($this->username, $this->password)) {
|
|
|
|
$this->_log(LOG_INFO, "Connected.");
|
|
|
|
} else {
|
|
|
|
$this->_log(LOG_ERR, 'Failed to connect to queue server');
|
|
|
|
throw new ServerException('Failed to connect to queue server');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $con;
|
|
|
|
}
|
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
/**
|
|
|
|
* Subscribe to all enabled notice queues for the current site.
|
|
|
|
*/
|
|
|
|
protected function doSubscribe()
|
|
|
|
{
|
2010-01-26 19:49:49 +00:00
|
|
|
$site = $this->currentSite();
|
2010-01-13 03:57:15 +00:00
|
|
|
$this->_connect();
|
|
|
|
foreach ($this->getQueues() as $queue) {
|
|
|
|
$rawqueue = $this->queueName($queue);
|
2010-01-26 19:49:49 +00:00
|
|
|
$this->subscriptions[$site][$queue] = $rawqueue;
|
2010-01-13 03:57:15 +00:00
|
|
|
$this->_log(LOG_INFO, "Subscribing to $rawqueue");
|
2010-01-29 00:49:32 +00:00
|
|
|
foreach ($this->cons as $con) {
|
|
|
|
if ($con) {
|
|
|
|
$con->subscribe($rawqueue);
|
|
|
|
}
|
|
|
|
}
|
2010-01-13 03:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-26 19:49:49 +00:00
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
/**
|
|
|
|
* Subscribe from all enabled notice queues for the current site.
|
|
|
|
*/
|
|
|
|
protected function doUnsubscribe()
|
|
|
|
{
|
2010-01-26 19:49:49 +00:00
|
|
|
$site = $this->currentSite();
|
2010-01-13 03:57:15 +00:00
|
|
|
$this->_connect();
|
2010-01-26 19:49:49 +00:00
|
|
|
if (!empty($this->subscriptions[$site])) {
|
|
|
|
foreach ($this->subscriptions[$site] as $queue => $rawqueue) {
|
|
|
|
$this->_log(LOG_INFO, "Unsubscribing from $rawqueue");
|
2010-01-29 00:49:32 +00:00
|
|
|
foreach ($this->cons as $con) {
|
|
|
|
if ($con) {
|
|
|
|
$con->unsubscribe($rawqueue);
|
|
|
|
}
|
|
|
|
}
|
2010-01-26 19:49:49 +00:00
|
|
|
unset($this->subscriptions[$site][$queue]);
|
|
|
|
}
|
2010-01-13 03:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-28 19:38:31 +01:00
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
/**
|
2010-01-22 00:42:50 +00:00
|
|
|
* Handle and acknowledge an event that's come in through a queue.
|
2010-01-13 03:57:15 +00:00
|
|
|
*
|
|
|
|
* If the queue handler reports failure, the message is requeued for later.
|
|
|
|
* Missing notices or handler classes will drop the message.
|
|
|
|
*
|
|
|
|
* Side effects: in multi-site mode, may reset site configuration to
|
|
|
|
* match the site that queued the event.
|
|
|
|
*
|
2010-01-29 00:49:32 +00:00
|
|
|
* @param int $idx connection index
|
2010-01-13 03:57:15 +00:00
|
|
|
* @param StompFrame $frame
|
|
|
|
* @return bool
|
|
|
|
*/
|
2010-01-29 00:49:32 +00:00
|
|
|
protected function handleItem($idx, $frame)
|
2010-01-13 03:57:15 +00:00
|
|
|
{
|
2010-01-29 00:49:32 +00:00
|
|
|
$this->defaultIdx = $idx;
|
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
list($site, $queue) = $this->parseDestination($frame->headers['destination']);
|
2010-01-26 19:49:49 +00:00
|
|
|
if ($site != $this->currentSite()) {
|
2010-01-13 03:57:15 +00:00
|
|
|
$this->stats('switch');
|
|
|
|
StatusNet::init($site);
|
|
|
|
}
|
2009-06-28 19:38:31 +01:00
|
|
|
|
2010-01-29 00:49:32 +00:00
|
|
|
$host = $this->cons[$idx]->getServer();
|
2010-01-22 00:42:50 +00:00
|
|
|
if (is_numeric($frame->body)) {
|
|
|
|
$id = intval($frame->body);
|
2010-01-29 00:49:32 +00:00
|
|
|
$info = "notice $id posted at {$frame->headers['created']} in queue $queue from $host";
|
2009-07-09 17:09:20 +01:00
|
|
|
|
2010-01-22 00:42:50 +00:00
|
|
|
$notice = Notice::staticGet('id', $id);
|
|
|
|
if (empty($notice)) {
|
|
|
|
$this->_log(LOG_WARNING, "Skipping missing $info");
|
2010-01-29 00:49:32 +00:00
|
|
|
$this->ack($idx, $frame);
|
|
|
|
$this->commit($idx);
|
|
|
|
$this->begin($idx);
|
2010-01-22 00:42:50 +00:00
|
|
|
$this->stats('badnotice', $queue);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$item = $notice;
|
|
|
|
} else {
|
|
|
|
// @fixme should we serialize, or json, or what here?
|
2010-01-29 00:49:32 +00:00
|
|
|
$info = "string posted at {$frame->headers['created']} in queue $queue from $host";
|
2010-01-22 00:42:50 +00:00
|
|
|
$item = $frame->body;
|
2010-01-13 03:57:15 +00:00
|
|
|
}
|
2009-07-09 17:09:20 +01:00
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
$handler = $this->getHandler($queue);
|
|
|
|
if (!$handler) {
|
2010-01-26 19:49:49 +00:00
|
|
|
$this->_log(LOG_ERR, "Missing handler class; skipping $info");
|
2010-01-29 00:49:32 +00:00
|
|
|
$this->ack($idx, $frame);
|
|
|
|
$this->commit($idx);
|
|
|
|
$this->begin($idx);
|
2010-01-13 03:57:15 +00:00
|
|
|
$this->stats('badhandler', $queue);
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-09 17:09:20 +01:00
|
|
|
|
2010-01-30 18:15:17 +00:00
|
|
|
// If there's an exception when handling,
|
|
|
|
// log the error and let it get requeued.
|
|
|
|
|
|
|
|
try {
|
|
|
|
$ok = $handler->handle($item);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->_log(LOG_ERR, "Exception on queue $queue: " . $e->getMessage());
|
|
|
|
$ok = false;
|
|
|
|
}
|
2009-07-09 17:09:20 +01:00
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
if (!$ok) {
|
|
|
|
$this->_log(LOG_WARNING, "Failed handling $info");
|
|
|
|
// FIXME we probably shouldn't have to do
|
|
|
|
// this kind of queue management ourselves;
|
|
|
|
// if we don't ack, it should resend...
|
2010-01-29 00:49:32 +00:00
|
|
|
$this->ack($idx, $frame);
|
2010-01-22 00:42:50 +00:00
|
|
|
$this->enqueue($item, $queue);
|
2010-01-29 00:49:32 +00:00
|
|
|
$this->commit($idx);
|
|
|
|
$this->begin($idx);
|
2010-01-13 03:57:15 +00:00
|
|
|
$this->stats('requeued', $queue);
|
|
|
|
return false;
|
2009-07-09 17:33:38 +01:00
|
|
|
}
|
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
$this->_log(LOG_INFO, "Successfully handled $info");
|
2010-01-29 00:49:32 +00:00
|
|
|
$this->ack($idx, $frame);
|
|
|
|
$this->commit($idx);
|
|
|
|
$this->begin($idx);
|
2010-01-13 03:57:15 +00:00
|
|
|
$this->stats('handled', $queue);
|
|
|
|
return true;
|
2009-07-09 17:33:38 +01:00
|
|
|
}
|
2009-07-09 16:40:01 +01:00
|
|
|
|
2010-01-26 19:49:49 +00:00
|
|
|
/**
|
|
|
|
* Process a control signal broadcast.
|
|
|
|
*
|
2010-01-29 00:49:32 +00:00
|
|
|
* @param int $idx connection index
|
2010-01-26 19:49:49 +00:00
|
|
|
* @param array $frame Stomp frame
|
|
|
|
* @return bool true to continue; false to stop further processing.
|
|
|
|
*/
|
2010-01-29 00:49:32 +00:00
|
|
|
protected function handleControlSignal($idx, $frame)
|
2010-01-26 19:49:49 +00:00
|
|
|
{
|
|
|
|
$message = trim($frame->body);
|
|
|
|
if (strpos($message, ':') !== false) {
|
|
|
|
list($event, $param) = explode(':', $message, 2);
|
|
|
|
} else {
|
|
|
|
$event = $message;
|
|
|
|
$param = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$shutdown = false;
|
|
|
|
|
|
|
|
if ($event == 'shutdown') {
|
|
|
|
$this->master->requestShutdown();
|
|
|
|
$shutdown = true;
|
|
|
|
} else if ($event == 'restart') {
|
|
|
|
$this->master->requestRestart();
|
|
|
|
$shutdown = true;
|
|
|
|
} else if ($event == 'update') {
|
|
|
|
$this->updateSiteConfig($param);
|
|
|
|
} else {
|
|
|
|
$this->_log(LOG_ERR, "Ignoring unrecognized control message: $message");
|
|
|
|
}
|
|
|
|
|
2010-01-29 00:49:32 +00:00
|
|
|
$this->ack($idx, $frame);
|
|
|
|
$this->commit($idx);
|
|
|
|
$this->begin($idx);
|
2010-01-26 19:49:49 +00:00
|
|
|
return $shutdown;
|
|
|
|
}
|
2010-01-30 18:15:17 +00:00
|
|
|
|
2010-01-26 19:49:49 +00:00
|
|
|
/**
|
|
|
|
* Set us up with queue subscriptions for a new site added at runtime,
|
|
|
|
* triggered by a broadcast to the 'statusnet-control' topic.
|
|
|
|
*
|
|
|
|
* @param array $frame Stomp frame
|
|
|
|
* @return bool true to continue; false to stop further processing.
|
|
|
|
*/
|
|
|
|
protected function updateSiteConfig($nickname)
|
|
|
|
{
|
|
|
|
if (empty($this->sites)) {
|
|
|
|
if ($nickname == common_config('site', 'nickname')) {
|
|
|
|
StatusNet::init(common_config('site', 'server'));
|
|
|
|
$this->doUnsubscribe();
|
|
|
|
$this->doSubscribe();
|
|
|
|
} else {
|
|
|
|
$this->_log(LOG_INFO, "Ignoring update ping for other site $nickname");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$sn = Status_network::staticGet($nickname);
|
|
|
|
if ($sn) {
|
|
|
|
$server = $sn->getServerName(); // @fixme do config-by-nick
|
|
|
|
StatusNet::init($server);
|
|
|
|
if (empty($this->sites[$server])) {
|
|
|
|
$this->addSite($server);
|
|
|
|
}
|
|
|
|
$this->_log(LOG_INFO, "(Re)subscribing to queues for site $nickname / $server");
|
|
|
|
$this->doUnsubscribe();
|
|
|
|
$this->doSubscribe();
|
|
|
|
$this->stats('siteupdate');
|
|
|
|
} else {
|
|
|
|
$this->_log(LOG_ERR, "Ignoring ping for unrecognized new site $nickname");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
/**
|
|
|
|
* Combines the queue_basename from configuration with the
|
|
|
|
* site server name and queue name to give eg:
|
|
|
|
*
|
|
|
|
* /queue/statusnet/identi.ca/sms
|
|
|
|
*
|
|
|
|
* @param string $queue
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function queueName($queue)
|
2009-07-09 17:33:38 +01:00
|
|
|
{
|
2010-01-13 03:57:15 +00:00
|
|
|
return common_config('queue', 'queue_basename') .
|
2010-01-26 19:49:49 +00:00
|
|
|
$this->currentSite() . '/' . $queue;
|
2009-07-01 16:10:23 +01:00
|
|
|
}
|
2009-07-02 17:43:09 +01:00
|
|
|
|
2010-01-13 03:57:15 +00:00
|
|
|
/**
|
|
|
|
* Returns the site and queue name from the server-side queue.
|
|
|
|
*
|
|
|
|
* @param string queue destination (eg '/queue/statusnet/identi.ca/sms')
|
|
|
|
* @return array of site and queue: ('identi.ca','sms') or false if unrecognized
|
|
|
|
*/
|
|
|
|
protected function parseDestination($dest)
|
2009-07-03 15:05:07 +01:00
|
|
|
{
|
2010-01-13 03:57:15 +00:00
|
|
|
$prefix = common_config('queue', 'queue_basename');
|
|
|
|
if (substr($dest, 0, strlen($prefix)) == $prefix) {
|
|
|
|
$rest = substr($dest, strlen($prefix));
|
|
|
|
return explode("/", $rest, 2);
|
|
|
|
} else {
|
|
|
|
common_log(LOG_ERR, "Got a message from unrecognized stomp queue: $dest");
|
|
|
|
return array(false, false);
|
|
|
|
}
|
2009-07-03 15:05:07 +01:00
|
|
|
}
|
|
|
|
|
2009-07-02 17:43:09 +01:00
|
|
|
function _log($level, $msg)
|
|
|
|
{
|
|
|
|
common_log($level, 'StompQueueManager: '.$msg);
|
|
|
|
}
|
2010-01-22 20:35:05 +00:00
|
|
|
|
2010-01-29 00:49:32 +00:00
|
|
|
protected function begin($idx)
|
2010-01-22 20:35:05 +00:00
|
|
|
{
|
|
|
|
if ($this->useTransactions) {
|
2010-01-29 00:49:32 +00:00
|
|
|
if (!empty($this->transaction[$idx])) {
|
2010-01-22 20:35:05 +00:00
|
|
|
throw new Exception("Tried to start transaction in the middle of a transaction");
|
|
|
|
}
|
2010-01-29 00:49:32 +00:00
|
|
|
$this->transactionCount[$idx]++;
|
|
|
|
$this->transaction[$idx] = $this->master->id . '-' . $this->transactionCount[$idx] . '-' . time();
|
|
|
|
$this->cons[$idx]->begin($this->transaction[$idx]);
|
2010-01-22 20:35:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-29 00:49:32 +00:00
|
|
|
protected function ack($idx, $frame)
|
2010-01-22 20:35:05 +00:00
|
|
|
{
|
|
|
|
if ($this->useTransactions) {
|
2010-01-29 00:49:32 +00:00
|
|
|
if (empty($this->transaction[$idx])) {
|
2010-01-22 20:35:05 +00:00
|
|
|
throw new Exception("Tried to ack but not in a transaction");
|
|
|
|
}
|
2010-01-29 00:49:32 +00:00
|
|
|
$this->cons[$idx]->ack($frame, $this->transaction[$idx]);
|
|
|
|
} else {
|
|
|
|
$this->cons[$idx]->ack($frame);
|
2010-01-22 20:35:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-29 00:49:32 +00:00
|
|
|
protected function commit($idx)
|
2010-01-22 20:35:05 +00:00
|
|
|
{
|
|
|
|
if ($this->useTransactions) {
|
2010-01-29 00:49:32 +00:00
|
|
|
if (empty($this->transaction[$idx])) {
|
2010-01-22 20:35:05 +00:00
|
|
|
throw new Exception("Tried to commit but not in a transaction");
|
|
|
|
}
|
2010-01-29 00:49:32 +00:00
|
|
|
$this->cons[$idx]->commit($this->transaction[$idx]);
|
|
|
|
$this->transaction[$idx] = null;
|
2010-01-22 20:35:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-29 00:49:32 +00:00
|
|
|
protected function rollback($idx)
|
2010-01-22 20:35:05 +00:00
|
|
|
{
|
|
|
|
if ($this->useTransactions) {
|
2010-01-29 00:49:32 +00:00
|
|
|
if (empty($this->transaction[$idx])) {
|
2010-01-22 20:35:05 +00:00
|
|
|
throw new Exception("Tried to rollback but not in a transaction");
|
|
|
|
}
|
2010-01-29 00:49:32 +00:00
|
|
|
$this->cons[$idx]->commit($this->transaction[$idx]);
|
|
|
|
$this->transaction[$idx] = null;
|
2010-01-22 20:35:05 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-28 19:38:31 +01:00
|
|
|
}
|
2010-01-13 03:57:15 +00:00
|
|
|
|