3 * StatusNet, the distributed open-source microblogging tool
5 * Abstract class for i/o 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 * @author Brion Vibber <brion@status.net>
27 * @copyright 2009-2010 StatusNet, Inc.
28 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29 * @link http://status.net/
33 * Completed child classes must implement the enqueue() method.
35 * For background processing, classes should implement either socket-based
36 * input (handleInput(), getSockets()) or idle-loop polling (idle()).
38 abstract class QueueManager extends IoManager
42 protected $master = null;
43 protected $handlers = array();
44 protected $groups = array();
45 protected $activeGroups = array();
48 * Factory function to pull the appropriate QueueManager object
49 * for this site's configuration. It can then be used to queue
50 * events for later processing or to spawn a processing loop.
52 * Plugins can add to the built-in types by hooking StartNewQueueManager.
54 * @return QueueManager
56 public static function get()
58 if (empty(self::$qm)) {
60 if (Event::handle('StartNewQueueManager', array(&self::$qm))) {
62 $enabled = common_config('queue', 'enabled');
63 $type = common_config('queue', 'subsystem');
66 // does everything immediately
67 self::$qm = new UnQueueManager();
71 self::$qm = new DBQueueManager();
74 self::$qm = new StompQueueManager();
77 throw new ServerException("No queue manager class for type '$type'");
87 * @fixme wouldn't necessarily work with other class types.
88 * Better to change the interface...?
90 public static function multiSite()
92 if (common_config('queue', 'subsystem') == 'stomp') {
93 return IoManager::INSTANCE_PER_PROCESS;
95 return IoManager::SINGLE_ONLY;
99 function __construct()
105 * Optional; ping any running queue handler daemons with a notification
106 * such as announcing a new site to handle or requesting clean shutdown.
107 * This avoids having to restart all the daemons manually to update configs
110 * Called from scripts/queuectl.php controller utility.
112 * @param string $event event key
113 * @param string $param optional parameter to append to key
114 * @return boolean success
116 public function sendControlSignal($event, $param='')
118 throw new Exception(get_class($this) . " does not support control signals.");
122 * Store an object (usually/always a Notice) into the given queue
123 * for later processing. No guarantee is made on when it will be
124 * processed; it could be immediately or at some unspecified point
127 * Must be implemented by any queue manager.
129 * @param Notice $object
130 * @param string $queue
132 abstract function enqueue($object, $queue);
135 * Build a representation for an object for logging
139 function logrep($object) {
140 if (is_object($object)) {
141 $class = get_class($object);
142 if (isset($object->id)) {
143 return "$class $object->id";
147 if (is_string($object)) {
148 $len = strlen($object);
149 $fragment = mb_substr($object, 0, 32);
150 if (mb_strlen($object) > 32) {
153 return "string '$fragment' ($len bytes)";
155 return strval($object);
159 * Encode an object or variable for queued storage.
160 * Notice objects are currently stored as an id reference;
161 * other items are serialized.
166 protected function encode($item)
168 if ($item instanceof Notice) {
172 return serialize($item);
177 * Decode an object from queued storage.
178 * Accepts notice reference entries and serialized items.
183 protected function decode($frame)
185 if (is_numeric($frame)) {
186 // Back-compat for notices...
187 return Notice::staticGet(intval($frame));
188 } elseif (substr($frame, 0, 1) == '<') {
189 // Back-compat for XML source
193 #$old = error_reporting();
194 #error_reporting($old & ~E_NOTICE);
195 $out = unserialize($frame);
196 #error_reporting($old);
198 if ($out === false && $frame !== 'b:0;') {
199 common_log(LOG_ERR, "Couldn't unserialize queued frame: $frame");
207 * Instantiate the appropriate QueueHandler class for the given queue.
209 * @param string $queue
210 * @return mixed QueueHandler or null
212 function getHandler($queue)
214 if (isset($this->handlers[$queue])) {
215 $class = $this->handlers[$queue];
216 if (class_exists($class)) {
219 $this->_log(LOG_ERR, "Nonexistent handler class '$class' for queue '$queue'");
222 $this->_log(LOG_ERR, "Requested handler for unkown queue '$queue'");
228 * Get a list of registered queue transport names to be used
229 * for listening in this daemon.
231 * @return array of strings
233 function activeQueues()
236 foreach ($this->activeGroups as $group) {
237 if (isset($this->groups[$group])) {
238 $queues = array_merge($queues, $this->groups[$group]);
242 return array_keys($queues);
246 * Initialize the list of queue handlers for the current site.
248 * @event StartInitializeQueueManager
249 * @event EndInitializeQueueManager
251 function initialize()
253 $this->handlers = array();
254 $this->groups = array();
255 $this->groupsByTransport = array();
257 if (Event::handle('StartInitializeQueueManager', array($this))) {
258 $this->connect('distrib', 'DistribQueueHandler');
259 $this->connect('omb', 'OmbQueueHandler');
260 $this->connect('ping', 'PingQueueHandler');
261 if (common_config('sms', 'enabled')) {
262 $this->connect('sms', 'SmsQueueHandler');
265 // XMPP output handlers...
266 if (common_config('xmpp', 'enabled')) {
267 // Delivery prep, read by queuedaemon.php:
268 $this->connect('jabber', 'JabberQueueHandler');
269 $this->connect('public', 'PublicQueueHandler');
271 // Raw output, read by xmppdaemon.php:
272 $this->connect('xmppout', 'XmppOutQueueHandler', 'xmpp');
275 // For compat with old plugins not registering their own handlers.
276 $this->connect('plugin', 'PluginQueueHandler');
278 Event::handle('EndInitializeQueueManager', array($this));
282 * Register a queue transport name and handler class for your plugin.
283 * Only registered transports will be reliably picked up!
285 * @param string $transport
286 * @param string $class
287 * @param string $group
289 public function connect($transport, $class, $group='main')
291 $this->handlers[$transport] = $class;
292 $this->groups[$group][$transport] = $class;
293 $this->groupsByTransport[$transport] = $group;
297 * Set the active group which will be used for listening.
298 * @param string $group
300 function setActiveGroup($group)
302 $this->activeGroups = array($group);
306 * Set the active group(s) which will be used for listening.
307 * @param array $groups
309 function setActiveGroups($groups)
311 $this->activeGroups = $groups;
315 * @return string queue group for this queue
317 function queueGroup($queue)
319 if (isset($this->groupsByTransport[$queue])) {
320 return $this->groupsByTransport[$queue];
322 throw new Exception("Requested group for unregistered transport $queue");
327 * Send a statistic ping to the queue monitoring system,
328 * optionally with a per-queue id.
331 * @param string $queue
333 function stats($key, $queue=false)
337 $owners[] = "queue:$queue";
338 $owners[] = "site:" . common_config('site', 'server');
340 if (isset($this->master)) {
341 $this->master->stats($key, $owners);
343 $monitor = new QueueMonitor();
344 $monitor->stats($key, $owners);
348 protected function _log($level, $msg)
350 $class = get_class($this);
351 if ($this->activeGroups) {
352 $groups = ' (' . implode(',', $this->activeGroups) . ')';
356 common_log($level, "$class$groups: $msg");