3 * StatusNet, the distributed open-source microblogging tool
5 * Abstract class for queue managers
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 Sarven Capadisli <csarven@status.net>
26 * @copyright 2009 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 require_once 'Stomp.php';
34 class StompQueueManager extends QueueManager
42 protected $master = null;
43 protected $sites = array();
45 function __construct()
47 parent::__construct();
48 $this->server = common_config('queue', 'stomp_server');
49 $this->username = common_config('queue', 'stomp_username');
50 $this->password = common_config('queue', 'stomp_password');
51 $this->base = common_config('queue', 'queue_basename');
55 * Tell the i/o master we only need a single instance to cover
56 * all sites running in this process.
58 public static function multiSite()
60 return IoManager::INSTANCE_PER_PROCESS;
64 * Record each site we'll be handling input for in this process,
65 * so we can listen to the necessary queues for it.
67 * @fixme possibly actually do subscription here to save another
68 * loop over all sites later?
69 * @fixme possibly don't assume it's the current site
71 public function addSite($server)
73 $this->sites[] = $server;
79 * Instantiate the appropriate QueueHandler class for the given queue.
81 * @param string $queue
82 * @return mixed QueueHandler or null
84 function getHandler($queue)
86 $handlers = $this->handlers[common_config('site', 'server')];
87 if (isset($handlers[$queue])) {
88 $class = $handlers[$queue];
89 if (class_exists($class)) {
92 common_log(LOG_ERR, "Nonexistent handler class '$class' for queue '$queue'");
95 common_log(LOG_ERR, "Requested handler for unkown queue '$queue'");
101 * Get a list of all registered queue transport names.
103 * @return array of strings
107 return array_keys($this->handlers[common_config('site', 'server')]);
111 * Register a queue transport name and handler class for your plugin.
112 * Only registered transports will be reliably picked up!
114 * @param string $transport
115 * @param string $class
117 public function connect($transport, $class)
119 $this->handlers[common_config('site', 'server')][$transport] = $class;
123 * Saves a notice object reference into the queue item table.
124 * @return boolean true on success
126 public function enqueue($object, $queue)
132 // XXX: serialize and send entire notice
134 $result = $this->con->send($this->queueName($queue),
135 $notice->id, // BODY of the message
136 array ('created' => $notice->created));
139 common_log(LOG_ERR, 'Error sending to '.$queue.' queue');
143 common_log(LOG_DEBUG, 'complete remote queueing notice ID = '
144 . $notice->id . ' for ' . $queue);
145 $this->stats('enqueued', $queue);
149 * Send any sockets we're listening on to the IO manager
152 * @return array of resources
154 public function getSockets()
156 return array($this->con->getSocket());
160 * We've got input to handle on our socket!
161 * Read any waiting Stomp frame(s) and process them.
163 * @param resource $socket
164 * @return boolean ok on success
166 public function handleInput($socket)
168 assert($socket === $this->con->getSocket());
170 $frames = $this->con->readFrames();
171 foreach ($frames as $frame) {
172 $ok = $ok && $this->_handleNotice($frame);
178 * Initialize our connection and subscribe to all the queues
179 * we're going to need to handle...
181 * Side effects: in multi-site mode, may reset site configuration.
183 * @param IoMaster $master process/event controller
184 * @return bool return false on failure
186 public function start($master)
188 parent::start($master);
190 foreach ($this->sites as $server) {
191 StatusNet::init($server);
192 $this->doSubscribe();
195 $this->doSubscribe();
201 * Subscribe to all the queues we're going to need to handle...
203 * Side effects: in multi-site mode, may reset site configuration.
205 * @return bool return false on failure
207 public function finish()
210 foreach ($this->sites as $server) {
211 StatusNet::init($server);
212 $this->doUnsubscribe();
215 $this->doUnsubscribe();
221 * Lazy open connection to Stomp queue server.
223 protected function _connect()
225 if (empty($this->con)) {
226 $this->_log(LOG_INFO, "Connecting to '$this->server' as '$this->username'...");
227 $this->con = new LiberalStomp($this->server);
229 if ($this->con->connect($this->username, $this->password)) {
230 $this->_log(LOG_INFO, "Connected.");
232 $this->_log(LOG_ERR, 'Failed to connect to queue server');
233 throw new ServerException('Failed to connect to queue server');
239 * Subscribe to all enabled notice queues for the current site.
241 protected function doSubscribe()
244 foreach ($this->getQueues() as $queue) {
245 $rawqueue = $this->queueName($queue);
246 $this->_log(LOG_INFO, "Subscribing to $rawqueue");
247 $this->con->subscribe($rawqueue);
252 * Subscribe from all enabled notice queues for the current site.
254 protected function doUnsubscribe()
257 foreach ($this->getQueues() as $queue) {
258 $this->con->unsubscribe($this->queueName($queue));
263 * Handle and acknowledge a notice event that's come in through a queue.
265 * If the queue handler reports failure, the message is requeued for later.
266 * Missing notices or handler classes will drop the message.
268 * Side effects: in multi-site mode, may reset site configuration to
269 * match the site that queued the event.
271 * @param StompFrame $frame
274 protected function _handleNotice($frame)
276 list($site, $queue) = $this->parseDestination($frame->headers['destination']);
277 if ($site != common_config('site', 'server')) {
278 $this->stats('switch');
279 StatusNet::init($site);
282 $id = intval($frame->body);
283 $info = "notice $id posted at {$frame->headers['created']} in queue $queue";
285 $notice = Notice::staticGet('id', $id);
286 if (empty($notice)) {
287 $this->_log(LOG_WARNING, "Skipping missing $info");
288 $this->con->ack($frame);
289 $this->stats('badnotice', $queue);
293 $handler = $this->getHandler($queue);
295 $this->_log(LOG_ERROR, "Missing handler class; skipping $info");
296 $this->con->ack($frame);
297 $this->stats('badhandler', $queue);
301 $ok = $handler->handle_notice($notice);
304 $this->_log(LOG_WARNING, "Failed handling $info");
305 // FIXME we probably shouldn't have to do
306 // this kind of queue management ourselves;
307 // if we don't ack, it should resend...
308 $this->con->ack($frame);
309 $this->enqueue($notice, $queue);
310 $this->stats('requeued', $queue);
314 $this->_log(LOG_INFO, "Successfully handled $info");
315 $this->con->ack($frame);
316 $this->stats('handled', $queue);
321 * Combines the queue_basename from configuration with the
322 * site server name and queue name to give eg:
324 * /queue/statusnet/identi.ca/sms
326 * @param string $queue
329 protected function queueName($queue)
331 return common_config('queue', 'queue_basename') .
332 common_config('site', 'server') . '/' . $queue;
336 * Returns the site and queue name from the server-side queue.
338 * @param string queue destination (eg '/queue/statusnet/identi.ca/sms')
339 * @return array of site and queue: ('identi.ca','sms') or false if unrecognized
341 protected function parseDestination($dest)
343 $prefix = common_config('queue', 'queue_basename');
344 if (substr($dest, 0, strlen($prefix)) == $prefix) {
345 $rest = substr($dest, strlen($prefix));
346 return explode("/", $rest, 2);
348 common_log(LOG_ERR, "Got a message from unrecognized stomp queue: $dest");
349 return array(false, false);
353 function _log($level, $msg)
355 common_log($level, 'StompQueueManager: '.$msg);