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 * Optional; ping any running queue handler daemons with a notification
105 * such as announcing a new site to handle or requesting clean shutdown.
106 * This avoids having to restart all the daemons manually to update configs
109 * Called from scripts/queuectl.php controller utility.
111 * @param string $event event key
112 * @param string $param optional parameter to append to key
113 * @return boolean success
115 public function sendControlSignal($event, $param='')
117 throw new Exception(get_class($this) . " does not support control signals.");
121 * Store an object (usually/always a Notice) into the given queue
122 * for later processing. No guarantee is made on when it will be
123 * processed; it could be immediately or at some unspecified point
126 * Must be implemented by any queue manager.
128 * @param Notice $object
129 * @param string $queue
131 abstract function enqueue($object, $queue);
134 * Build a representation for an object for logging
138 function logrep($object) {
139 if (is_object($object)) {
140 $class = get_class($object);
141 if (isset($object->id)) {
142 return "$class $object->id";
146 if (is_string($object)) {
147 $len = strlen($object);
148 $fragment = mb_substr($object, 0, 32);
149 if (mb_strlen($object) > 32) {
152 return "string '$fragment' ($len bytes)";
154 return strval($object);
158 * Encode an object for queued storage.
160 * @param mixed $object
163 protected function encode($object)
165 return serialize($object);
169 * Decode an object from queued storage.
170 * Accepts back-compat notice reference entries and strings for now.
175 protected function decode($frame)
177 return unserialize($frame);
181 * Instantiate the appropriate QueueHandler class for the given queue.
183 * @param string $queue
184 * @return mixed QueueHandler or null
186 function getHandler($queue)
188 if (isset($this->handlers[$queue])) {
189 $class = $this->handlers[$queue];
190 if(is_object($class)) {
192 } else if (class_exists($class)) {
195 common_log(LOG_ERR, "Nonexistent handler class '$class' for queue '$queue'");
198 common_log(LOG_ERR, "Requested handler for unkown queue '$queue'");
204 * Get a list of registered queue transport names to be used
207 * @return array of strings
211 $group = $this->activeGroup();
212 return array_keys($this->groups[$group]);
216 * Initialize the list of queue handlers
218 * @event StartInitializeQueueManager
219 * @event EndInitializeQueueManager
221 function initialize()
223 // @fixme we'll want to be able to listen to particular queues...
224 if (Event::handle('StartInitializeQueueManager', array($this))) {
225 $this->connect('plugin', 'PluginQueueHandler');
226 $this->connect('omb', 'OmbQueueHandler');
227 $this->connect('ping', 'PingQueueHandler');
228 $this->connect('distrib', 'DistribQueueHandler');
229 if (common_config('sms', 'enabled')) {
230 $this->connect('sms', 'SmsQueueHandler');
233 // For compat with old plugins not registering their own handlers.
234 $this->connect('plugin', 'PluginQueueHandler');
236 Event::handle('EndInitializeQueueManager', array($this));
240 * Register a queue transport name and handler class for your plugin.
241 * Only registered transports will be reliably picked up!
243 * @param string $transport
244 * @param string $class class name or object instance
245 * @param string $group
247 public function connect($transport, $class, $group='queuedaemon')
249 $this->handlers[$transport] = $class;
250 $this->groups[$group][$transport] = $class;
254 * @return string queue group to use for this request
256 function activeGroup()
258 $group = 'queuedaemon';
261 if ($this->master instanceof ImMaster) {
269 * Send a statistic ping to the queue monitoring system,
270 * optionally with a per-queue id.
273 * @param string $queue
275 function stats($key, $queue=false)
279 $owners[] = "queue:$queue";
280 $owners[] = "site:" . common_config('site', 'server');
282 if (isset($this->master)) {
283 $this->master->stats($key, $owners);
285 $monitor = new QueueMonitor();
286 $monitor->stats($key, $owners);