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 public $master = null;
43 public $handlers = array();
44 public $groups = array();
47 * Factory function to pull the appropriate QueueManager object
48 * for this site's configuration. It can then be used to queue
49 * events for later processing or to spawn a processing loop.
51 * Plugins can add to the built-in types by hooking StartNewQueueManager.
53 * @return QueueManager
55 public static function get()
57 if (empty(self::$qm)) {
59 if (Event::handle('StartNewQueueManager', array(&self::$qm))) {
61 $enabled = common_config('queue', 'enabled');
62 $type = common_config('queue', 'subsystem');
65 // does everything immediately
66 self::$qm = new UnQueueManager();
70 self::$qm = new DBQueueManager();
73 self::$qm = new StompQueueManager();
76 throw new ServerException("No queue manager class for type '$type'");
86 * @fixme wouldn't necessarily work with other class types.
87 * Better to change the interface...?
89 public static function multiSite()
91 if (common_config('queue', 'subsystem') == 'stomp') {
92 return IoManager::INSTANCE_PER_PROCESS;
94 return IoManager::SINGLE_ONLY;
98 function __construct()
104 * Store an object (usually/always a Notice) into the given queue
105 * for later processing. No guarantee is made on when it will be
106 * processed; it could be immediately or at some unspecified point
109 * Must be implemented by any queue manager.
111 * @param Notice $object
112 * @param string $queue
114 abstract function enqueue($object, $queue);
117 * Build a representation for an object for logging
121 function logrep($object) {
122 if (is_object($object)) {
123 $class = get_class($object);
124 if (isset($object->id)) {
125 return "$class $object->id";
129 if (is_string($object)) {
130 $len = strlen($object);
131 $fragment = mb_substr($object, 0, 32);
132 if (mb_strlen($object) > 32) {
135 return "string '$fragment' ($len bytes)";
137 return strval($object);
141 * Encode an object for queued storage.
143 * @param mixed $object
146 protected function encode($object)
148 return serialize($object);
152 * Decode an object from queued storage.
153 * Accepts back-compat notice reference entries and strings for now.
158 protected function decode($frame)
160 return unserialize($frame);
164 * Instantiate the appropriate QueueHandler class for the given queue.
166 * @param string $queue
167 * @return mixed QueueHandler or null
169 function getHandler($queue)
171 if (isset($this->handlers[$queue])) {
172 $class = $this->handlers[$queue];
173 if (class_exists($class)) {
176 common_log(LOG_ERR, "Nonexistent handler class '$class' for queue '$queue'");
179 common_log(LOG_ERR, "Requested handler for unkown queue '$queue'");
185 * Get a list of registered queue transport names to be used
188 * @return array of strings
192 $group = $this->activeGroup();
193 return array_keys($this->groups[$group]);
197 * Initialize the list of queue handlers
199 * @event StartInitializeQueueManager
200 * @event EndInitializeQueueManager
202 function initialize()
204 // @fixme we'll want to be able to listen to particular queues...
205 if (Event::handle('StartInitializeQueueManager', array($this))) {
206 $this->connect('plugin', 'PluginQueueHandler');
207 $this->connect('omb', 'OmbQueueHandler');
208 $this->connect('ping', 'PingQueueHandler');
209 if (common_config('sms', 'enabled')) {
210 $this->connect('sms', 'SmsQueueHandler');
213 // XMPP output handlers...
214 $this->connect('jabber', 'JabberQueueHandler');
215 $this->connect('public', 'PublicQueueHandler');
217 // @fixme this should get an actual queue
218 //$this->connect('confirm', 'XmppConfirmHandler');
220 // For compat with old plugins not registering their own handlers.
221 $this->connect('plugin', 'PluginQueueHandler');
223 $this->connect('xmppout', 'XmppOutQueueHandler', 'xmppdaemon');
226 Event::handle('EndInitializeQueueManager', array($this));
230 * Register a queue transport name and handler class for your plugin.
231 * Only registered transports will be reliably picked up!
233 * @param string $transport
234 * @param string $class
235 * @param string $group
237 public function connect($transport, $class, $group='queuedaemon')
239 $this->handlers[$transport] = $class;
240 $this->groups[$group][$transport] = $class;
244 * @return string queue group to use for this request
246 function activeGroup()
248 $group = 'queuedaemon';
251 if ($this->master instanceof XmppMaster) {
259 * Send a statistic ping to the queue monitoring system,
260 * optionally with a per-queue id.
263 * @param string $queue
265 function stats($key, $queue=false)
269 $owners[] = "queue:$queue";
270 $owners[] = "site:" . common_config('site', 'server');
272 if (isset($this->master)) {
273 $this->master->stats($key, $owners);
275 $monitor = new QueueMonitor();
276 $monitor->stats($key, $owners);