]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/queuemanager.php
Introduced common_location_shared() to check if location sharing is always,
[quix0rs-gnu-social.git] / lib / queuemanager.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Abstract class for i/o managers
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  QueueManager
23  * @package   StatusNet
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/
30  */
31
32 /**
33  * Completed child classes must implement the enqueue() method.
34  *
35  * For background processing, classes should implement either socket-based
36  * input (handleInput(), getSockets()) or idle-loop polling (idle()).
37  */
38 abstract class QueueManager extends IoManager
39 {
40     static $qm = null;
41
42     protected $master = null;
43     protected $handlers = array();
44     protected $groups = array();
45     protected $activeGroups = array();
46     protected $ignoredTransports = array();
47
48     /**
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.
52      *
53      * Plugins can add to the built-in types by hooking StartNewQueueManager.
54      *
55      * @return QueueManager
56      */
57     public static function get()
58     {
59         if (empty(self::$qm)) {
60
61             if (Event::handle('StartNewQueueManager', array(&self::$qm))) {
62
63                 $enabled = common_config('queue', 'enabled');
64                 $type = common_config('queue', 'subsystem');
65
66                 if (!$enabled) {
67                     // does everything immediately
68                     self::$qm = new UnQueueManager();
69                 } else {
70                     switch ($type) {
71                      case 'db':
72                         self::$qm = new DBQueueManager();
73                         break;
74                      case 'stomp':
75                         self::$qm = new StompQueueManager();
76                         break;
77                      default:
78                         throw new ServerException("No queue manager class for type '$type'");
79                     }
80                 }
81             }
82         }
83
84         return self::$qm;
85     }
86
87     /**
88      * @fixme wouldn't necessarily work with other class types.
89      * Better to change the interface...?
90      */
91     public static function multiSite()
92     {
93         if (common_config('queue', 'subsystem') == 'stomp') {
94             return IoManager::INSTANCE_PER_PROCESS;
95         } else {
96             return IoManager::SINGLE_ONLY;
97         }
98     }
99
100     function __construct()
101     {
102         $this->initialize();
103     }
104
105     /**
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
109      * and such.
110      *
111      * Called from scripts/queuectl.php controller utility.
112      *
113      * @param string $event event key
114      * @param string $param optional parameter to append to key
115      * @return boolean success
116      */
117     public function sendControlSignal($event, $param='')
118     {
119         throw new Exception(get_class($this) . " does not support control signals.");
120     }
121
122     /**
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
126      * in the future.
127      *
128      * Must be implemented by any queue manager.
129      *
130      * @param Notice $object
131      * @param string $queue
132      */
133     abstract function enqueue($object, $queue);
134
135     /**
136      * Build a representation for an object for logging
137      * @param mixed
138      * @return string
139      */
140     function logrep($object) {
141         if (is_object($object)) {
142             $class = get_class($object);
143             if (isset($object->id)) {
144                 return "$class $object->id";
145             }
146             return $class;
147         } elseif (is_string($object)) {
148             $len = strlen($object);
149             $fragment = mb_substr($object, 0, 32);
150             if (mb_strlen($object) > 32) {
151                 $fragment .= '...';
152             }
153             return "string '$fragment' ($len bytes)";
154         } elseif (is_array($object)) {
155             return 'array with ' . count($object) .
156                    ' elements (keys:[' .  implode(',', array_keys($object)) . '])';
157         }
158         return strval($object);
159     }
160
161     /**
162      * Encode an object for queued storage.
163      *
164      * @param mixed $item
165      * @return string
166      */
167     protected function encode($item)
168     {
169         return serialize($item);
170     }
171
172     /**
173      * Decode an object from queued storage.
174      * Accepts notice reference entries and serialized items.
175      *
176      * @param string
177      * @return mixed
178      */
179     protected function decode($frame)
180     {
181         $object = unserialize($frame);
182
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))
188         {
189             $json = json_decode($object);
190             if ($json === null) {
191                 throw new Exception('Bad frame in queue item');
192             }
193
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');
197             }
198             if (!is_a($json->type, 'Managed_DataObject', true)) {
199                 throw new Exception('Managed_DataObject class does not exist for queue item');
200             }
201
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);
207             }
208
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');
212             }
213         }
214
215         // If the frame was not a string, it's either an array or an object.
216
217         return $object;
218     }
219
220     /**
221      * Instantiate the appropriate QueueHandler class for the given queue.
222      *
223      * @param string $queue
224      * @return mixed QueueHandler or null
225      */
226     function getHandler($queue)
227     {
228         if (isset($this->handlers[$queue])) {
229             $class = $this->handlers[$queue];
230             if(is_object($class)) {
231                 return $class;
232             } else if (class_exists($class)) {
233                 return new $class();
234             } else {
235                 $this->_log(LOG_ERR, "Nonexistent handler class '$class' for queue '$queue'");
236             }
237         }
238         return null;
239     }
240
241     /**
242      * Get a list of registered queue transport names to be used
243      * for listening in this daemon.
244      *
245      * @return array of strings
246      */
247     function activeQueues()
248     {
249         $queues = array();
250         foreach ($this->activeGroups as $group) {
251             if (isset($this->groups[$group])) {
252                 $queues = array_merge($queues, $this->groups[$group]);
253             }
254         }
255
256         return array_keys($queues);
257     }
258
259     function getIgnoredTransports()
260     {
261         return array_keys($this->ignoredTransports);
262     }
263
264     function ignoreTransport($transport)
265     {
266         // key is used for uniqueness, value doesn't mean anything
267         $this->ignoredTransports[$transport] = true;
268     }
269
270     /**
271      * Initialize the list of queue handlers for the current site.
272      *
273      * @event StartInitializeQueueManager
274      * @event EndInitializeQueueManager
275      */
276     function initialize()
277     {
278         $this->handlers = array();
279         $this->groups = array();
280         $this->groupsByTransport = array();
281
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');
287             }
288
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');
295
296             // For compat with old plugins not registering their own handlers.
297             $this->connect('plugin', 'PluginQueueHandler');
298         }
299         Event::handle('EndInitializeQueueManager', array($this));
300     }
301
302     /**
303      * Register a queue transport name and handler class for your plugin.
304      * Only registered transports will be reliably picked up!
305      *
306      * @param string $transport
307      * @param string $class class name or object instance
308      * @param string $group
309      */
310     public function connect($transport, $class, $group='main')
311     {
312         $this->handlers[$transport] = $class;
313         $this->groups[$group][$transport] = $class;
314         $this->groupsByTransport[$transport] = $group;
315     }
316
317     /**
318      * Set the active group which will be used for listening.
319      * @param string $group
320      */
321     function setActiveGroup($group)
322     {
323         $this->activeGroups = array($group);
324     }
325
326     /**
327      * Set the active group(s) which will be used for listening.
328      * @param array $groups
329      */
330     function setActiveGroups($groups)
331     {
332         $this->activeGroups = $groups;
333     }
334
335     /**
336      * @return string queue group for this queue
337      */
338     function queueGroup($queue)
339     {
340         if (isset($this->groupsByTransport[$queue])) {
341             return $this->groupsByTransport[$queue];
342         } else {
343             throw new Exception("Requested group for unregistered transport $queue");
344         }
345     }
346
347     /**
348      * Send a statistic ping to the queue monitoring system,
349      * optionally with a per-queue id.
350      *
351      * @param string $key
352      * @param string $queue
353      */
354     function stats($key, $queue=false)
355     {
356         $owners = array();
357         if ($queue) {
358             $owners[] = "queue:$queue";
359             $owners[] = "site:" . common_config('site', 'server');
360         }
361         if (isset($this->master)) {
362             $this->master->stats($key, $owners);
363         } else {
364             $monitor = new QueueMonitor();
365             $monitor->stats($key, $owners);
366         }
367     }
368
369     protected function _log($level, $msg)
370     {
371         $class = get_class($this);
372         if ($this->activeGroups) {
373             $groups = ' (' . implode(',', $this->activeGroups) . ')';
374         } else {
375             $groups = '';
376         }
377         common_log($level, "$class$groups: $msg");
378     }
379 }