+ if ($item instanceof Notice) {
+ // Backwards compat
+ return $item->id;
+ } else {
+ return serialize($item);
+ }
+ }
+
+ /**
+ * Decode an object from queued storage.
+ * Accepts notice reference entries and serialized items.
+ *
+ * @param string
+ * @return mixed
+ */
+ protected function decode($frame)
+ {
+ if (is_numeric($frame)) {
+ // Back-compat for notices...
+ return Notice::staticGet(intval($frame));
+ } elseif (substr($frame, 0, 1) == '<') {
+ // Back-compat for XML source
+ return $frame;
+ } else {
+ // Deserialize!
+ #$old = error_reporting();
+ #error_reporting($old & ~E_NOTICE);
+ $out = unserialize($frame);
+ #error_reporting($old);
+
+ if ($out === false && $frame !== 'b:0;') {
+ common_log(LOG_ERR, "Couldn't unserialize queued frame: $frame");
+ return false;
+ }
+ return $out;
+ }
+ }
+
+ /**
+ * Instantiate the appropriate QueueHandler class for the given queue.
+ *
+ * @param string $queue
+ * @return mixed QueueHandler or null
+ */
+ function getHandler($queue)
+ {
+ if (isset($this->handlers[$queue])) {
+ $class = $this->handlers[$queue];
+ if(is_object($class)) {
+ return $class;
+ } else if (class_exists($class)) {
+ return new $class();
+ } else {
+ $this->_log(LOG_ERR, "Nonexistent handler class '$class' for queue '$queue'");
+ }
+ } else {
+ $this->_log(LOG_ERR, "Requested handler for unkown queue '$queue'");
+ }
+ return null;
+ }
+
+ /**
+ * Get a list of registered queue transport names to be used
+ * for listening in this daemon.
+ *
+ * @return array of strings
+ */
+ function activeQueues()
+ {
+ $queues = array();
+ foreach ($this->activeGroups as $group) {
+ if (isset($this->groups[$group])) {
+ $queues = array_merge($queues, $this->groups[$group]);
+ }
+ }
+
+ return array_keys($queues);
+ }
+
+ /**
+ * Initialize the list of queue handlers for the current site.
+ *
+ * @event StartInitializeQueueManager
+ * @event EndInitializeQueueManager
+ */
+ function initialize()
+ {
+ $this->handlers = array();
+ $this->groups = array();
+ $this->groupsByTransport = array();
+
+ if (Event::handle('StartInitializeQueueManager', array($this))) {
+ $this->connect('distrib', 'DistribQueueHandler');
+ $this->connect('omb', 'OmbQueueHandler');
+ $this->connect('ping', 'PingQueueHandler');
+ if (common_config('sms', 'enabled')) {
+ $this->connect('sms', 'SmsQueueHandler');
+ }
+
+ // Broadcasting profile updates to OMB remote subscribers
+ $this->connect('profile', 'ProfileQueueHandler');
+
+ // XMPP output handlers...
+ if (common_config('xmpp', 'enabled')) {
+ // Delivery prep, read by queuedaemon.php:
+ $this->connect('jabber', 'JabberQueueHandler');
+ $this->connect('public', 'PublicQueueHandler');
+
+ // Raw output, read by xmppdaemon.php:
+ $this->connect('xmppout', 'XmppOutQueueHandler', 'xmpp');
+ }
+
+ // For compat with old plugins not registering their own handlers.
+ $this->connect('plugin', 'PluginQueueHandler');
+ }
+ Event::handle('EndInitializeQueueManager', array($this));
+ }
+
+ /**
+ * 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 class name or object instance
+ * @param string $group
+ */
+ public function connect($transport, $class, $group='main')
+ {
+ $this->handlers[$transport] = $class;
+ $this->groups[$group][$transport] = $class;
+ $this->groupsByTransport[$transport] = $group;
+ }
+
+ /**
+ * Set the active group which will be used for listening.
+ * @param string $group
+ */
+ function setActiveGroup($group)
+ {
+ $this->activeGroups = array($group);
+ }
+
+ /**
+ * Set the active group(s) which will be used for listening.
+ * @param array $groups
+ */
+ function setActiveGroups($groups)
+ {
+ $this->activeGroups = $groups;
+ }
+
+ /**
+ * @return string queue group for this queue
+ */
+ function queueGroup($queue)
+ {
+ if (isset($this->groupsByTransport[$queue])) {
+ return $this->groupsByTransport[$queue];
+ } else {
+ throw new Exception("Requested group for unregistered transport $queue");
+ }
+ }
+
+ /**
+ * Send a statistic ping to the queue monitoring system,
+ * optionally with a per-queue id.
+ *
+ * @param string $key
+ * @param string $queue
+ */
+ function stats($key, $queue=false)
+ {
+ $owners = array();
+ if ($queue) {
+ $owners[] = "queue:$queue";
+ $owners[] = "site:" . common_config('site', 'server');
+ }
+ if (isset($this->master)) {
+ $this->master->stats($key, $owners);
+ } else {
+ $monitor = new QueueMonitor();
+ $monitor->stats($key, $owners);
+ }
+ }
+
+ protected function _log($level, $msg)
+ {
+ $class = get_class($this);
+ if ($this->activeGroups) {
+ $groups = ' (' . implode(',', $this->activeGroups) . ')';
+ } else {
+ $groups = '';
+ }
+ common_log($level, "$class$groups: $msg");