Merge "CologneBlue rewrite: kill mWatchLinkNum, watchThisPage() is only called once...
[lhc/web/wiklou.git] / includes / ForkController.php
index 6a38699..448bc03 100644 (file)
@@ -1,11 +1,33 @@
 <?php
+/**
+ * Class for managing forking command line scripts.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
 
 /**
  * Class for managing forking command line scripts.
- * Currently just does forking and process control, but it could easily be extended 
+ * Currently just does forking and process control, but it could easily be extended
  * to provide IPC and job dispatch.
  *
  * This class requires the posix and pcntl extensions.
+ *
+ * @ingroup Maintenance
  */
 class ForkController {
        var $children = array();
@@ -32,21 +54,22 @@ class ForkController {
 
        public function __construct( $numProcs, $flags = 0 ) {
                if ( php_sapi_name() != 'cli' ) {
-                       throw new MWException( "MultiProcess cannot be used from the web." );
+                       throw new MWException( "ForkController cannot be used from the web." );
                }
                $this->procsToStart = $numProcs;
                $this->flags = $flags;
        }
 
        /**
-        * Start the child processes. 
+        * Start the child processes.
         *
-        * This should only be called from the command line. It should be called 
+        * This should only be called from the command line. It should be called
         * as early as possible during execution.
         *
-        * This will return 'child' in the child processes. In the parent process, 
-        * it will run until all the child processes exit or a TERM signal is 
+        * This will return 'child' in the child processes. In the parent process,
+        * it will run until all the child processes exit or a TERM signal is
         * received. It will then return 'done'.
+        * @return string
         */
        public function start() {
                // Trap SIGTERM
@@ -73,7 +96,7 @@ class ForkController {
                                                // Restart if the signal was abnormal termination
                                                // Don't restart if it was deliberately killed
                                                $signal = pcntl_wtermsig( $status );
-                                               if ( in_array( $signal, self::$restartableSignals ) ) { 
+                                               if ( in_array( $signal, self::$restartableSignals ) ) {
                                                        echo "Worker exited with signal $signal, restarting\n";
                                                        $this->procsToStart++;
                                                }
@@ -98,7 +121,7 @@ class ForkController {
                        if ( function_exists( 'pcntl_signal_dispatch' ) ) {
                                pcntl_signal_dispatch();
                        } else {
-                               declare (ticks=1) { $status = $status; } 
+                               declare (ticks=1) { $status = $status; }
                        }
                        // Respond to TERM signal
                        if ( $this->termReceived ) {
@@ -113,19 +136,21 @@ class ForkController {
        }
 
        protected function prepareEnvironment() {
-               global $wgCaches, $wgMemc;
-               // Don't share DB or memcached connections
+               global $wgMemc;
+               // Don't share DB, storage, or memcached connections
                wfGetLBFactory()->destroyInstance();
-               $wgCaches = array();
-               unset( $wgMemc );
+               FileBackendGroup::destroySingleton();
+               LockManagerGroup::destroySingleton();
+               ObjectCache::clear();
+               $wgMemc = null;
        }
 
        /**
         * Fork a number of worker processes.
+        *
+        * @return string
         */
        protected function forkWorkers( $numProcs ) {
-               global $wgMemc, $wgCaches, $wgMainCacheType;
-       
                $this->prepareEnvironment();
 
                // Create the child processes
@@ -153,7 +178,7 @@ class ForkController {
                global $wgMemc, $wgMainCacheType;
                $wgMemc = wfGetCache( $wgMainCacheType );
                $this->children = null;
-               pcntl_signal( SIGTERM, SIG_DFL );               
+               pcntl_signal( SIGTERM, SIG_DFL );
        }
 
        protected function handleTermSignal( $signal ) {