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();
46 protected $ignoredTransports = array();
49 * Factory function to pull the appropriate QueueManager object
50 * for this site's configuration. It can then be used to queue
51 * events for later processing or to spawn a processing loop.
53 * Plugins can add to the built-in types by hooking StartNewQueueManager.
55 * @return QueueManager
57 public static function get()
59 if (empty(self::$qm)) {
61 if (Event::handle('StartNewQueueManager', array(&self::$qm))) {
63 $enabled = common_config('queue', 'enabled');
64 $type = common_config('queue', 'subsystem');
67 // does everything immediately
68 self::$qm = new UnQueueManager();
72 self::$qm = new DBQueueManager();
75 self::$qm = new StompQueueManager();
78 throw new ServerException("No queue manager class for type '$type'");
88 * @fixme wouldn't necessarily work with other class types.
89 * Better to change the interface...?
91 public static function multiSite()
93 if (common_config('queue', 'subsystem') == 'stomp') {
94 return IoManager::INSTANCE_PER_PROCESS;
96 return IoManager::SINGLE_ONLY;
100 function __construct()
106 * Optional; ping any running queue handler daemons with a notification
107 * such as announcing a new site to handle or requesting clean shutdown.
108 * This avoids having to restart all the daemons manually to update configs
111 * Called from scripts/queuectl.php controller utility.
113 * @param string $event event key
114 * @param string $param optional parameter to append to key
115 * @return boolean success
117 public function sendControlSignal($event, $param='')
119 throw new Exception(get_class($this) . " does not support control signals.");
123 * Store an object (usually/always a Notice) into the given queue
124 * for later processing. No guarantee is made on when it will be
125 * processed; it could be immediately or at some unspecified point
128 * Must be implemented by any queue manager.
130 * @param Notice $object
131 * @param string $queue
133 abstract function enqueue($object, $queue);
136 * Build a representation for an object for logging
140 function logrep($object) {
141 if (is_object($object)) {
142 $class = get_class($object);
143 if (isset($object->id)) {
144 return "$class $object->id";
147 } elseif (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)";
154 } elseif (is_array($object)) {
155 return 'array with ' . count($object) .
156 ' elements (keys:[' . implode(',', array_keys($object)) . '])';
158 return strval($object);
162 * Encode an object for queued storage.
167 protected function encode($item)
169 return serialize($item);
173 * Decode an object from queued storage.
174 * Accepts notice reference entries and serialized items.
179 protected function decode($frame)
181 $object = unserialize($frame);
183 // If it is a string, we really store a JSON object in there
184 // except if it begins with '<', because then it is XML.
185 if (is_string($object) &&
186 substr($object, 0, 1) != '<' &&
187 !is_numeric($object))
189 $json = json_decode($object);
190 if ($json === null) {
191 throw new Exception('Bad frame in queue item');
194 // The JSON object has a type parameter which contains the class
195 if (empty($json->type)) {
196 throw new Exception('Type not specified for queue item');
198 if (!is_a($json->type, 'Managed_DataObject', true)) {
199 throw new Exception('Managed_DataObject class does not exist for queue item');
202 // And each of these types should have a unique id (or uri)
203 if (isset($json->id) && !empty($json->id)) {
204 $object = call_user_func(array($json->type, 'getKV'), 'id', $json->id);
205 } elseif (isset($json->uri) && !empty($json->uri)) {
206 $object = call_user_func(array($json->type, 'getKV'), 'uri', $json->uri);
209 // But if no object was found, there's nothing we can handle
210 if (!$object instanceof Managed_DataObject) {
211 throw new Exception('Queue item frame referenced a non-existant object');
215 // If the frame was not a string, it's either an array or an object.
221 * Instantiate the appropriate QueueHandler class for the given queue.
223 * @param string $queue
224 * @return mixed QueueHandler or null
226 function getHandler($queue)
228 if (isset($this->handlers[$queue])) {
229 $class = $this->handlers[$queue];
230 if(is_object($class)) {
232 } else if (class_exists($class)) {
235 $this->_log(LOG_ERR, "Nonexistent handler class '$class' for queue '$queue'");
238 throw new NoQueueHandlerException($queue);
242 * Get a list of registered queue transport names to be used
243 * for listening in this daemon.
245 * @return array of strings
247 function activeQueues()
250 foreach ($this->activeGroups as $group) {
251 if (isset($this->groups[$group])) {
252 $queues = array_merge($queues, $this->groups[$group]);
256 return array_keys($queues);
259 function getIgnoredTransports()
261 return array_keys($this->ignoredTransports);
264 function ignoreTransport($transport)
266 // key is used for uniqueness, value doesn't mean anything
267 $this->ignoredTransports[$transport] = true;
271 * Initialize the list of queue handlers for the current site.
273 * @event StartInitializeQueueManager
274 * @event EndInitializeQueueManager
276 function initialize()
278 $this->handlers = array();
279 $this->groups = array();
280 $this->groupsByTransport = array();
282 if (Event::handle('StartInitializeQueueManager', array($this))) {
283 $this->connect('distrib', 'DistribQueueHandler');
284 $this->connect('ping', 'PingQueueHandler');
285 if (common_config('sms', 'enabled')) {
286 $this->connect('sms', 'SmsQueueHandler');
289 // Background user management tasks...
290 $this->connect('deluser', 'DelUserQueueHandler');
291 $this->connect('feedimp', 'FeedImporter');
292 $this->connect('actimp', 'ActivityImporter');
293 $this->connect('acctmove', 'AccountMover');
294 $this->connect('actmove', 'ActivityMover');
296 // For compat with old plugins not registering their own handlers.
297 $this->connect('plugin', 'PluginQueueHandler');
299 Event::handle('EndInitializeQueueManager', array($this));
303 * Register a queue transport name and handler class for your plugin.
304 * Only registered transports will be reliably picked up!
306 * @param string $transport
307 * @param string $class class name or object instance
308 * @param string $group
310 public function connect($transport, $class, $group='main')
312 $this->handlers[$transport] = $class;
313 $this->groups[$group][$transport] = $class;
314 $this->groupsByTransport[$transport] = $group;
318 * Set the active group which will be used for listening.
319 * @param string $group
321 function setActiveGroup($group)
323 $this->activeGroups = array($group);
327 * Set the active group(s) which will be used for listening.
328 * @param array $groups
330 function setActiveGroups($groups)
332 $this->activeGroups = $groups;
336 * @return string queue group for this queue
338 function queueGroup($queue)
340 if (isset($this->groupsByTransport[$queue])) {
341 return $this->groupsByTransport[$queue];
343 throw new Exception("Requested group for unregistered transport $queue");
348 * Send a statistic ping to the queue monitoring system,
349 * optionally with a per-queue id.
352 * @param string $queue
354 function stats($key, $queue=false)
358 $owners[] = "queue:$queue";
359 $owners[] = "site:" . common_config('site', 'server');
361 if (isset($this->master)) {
362 $this->master->stats($key, $owners);
364 $monitor = new QueueMonitor();
365 $monitor->stats($key, $owners);
369 protected function _log($level, $msg)
371 $class = get_class($this);
372 if ($this->activeGroups) {
373 $groups = ' (' . implode(',', $this->activeGroups) . ')';
377 common_log($level, "$class$groups: $msg");