+ 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('ping', 'PingQueueHandler');
+ if (common_config('sms', 'enabled')) {
+ $this->connect('sms', 'SmsQueueHandler');
+ }
+
+ // Background user management tasks...
+ $this->connect('deluser', 'DelUserQueueHandler');
+ $this->connect('feedimp', 'FeedImporter');
+ $this->connect('actimp', 'ActivityImporter');
+ $this->connect('acctmove', 'AccountMover');
+ $this->connect('actmove', 'ActivityMover');
+
+ // 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");