Fix exit code from rebuildLocalisationCache.php
[lhc/web/wiklou.git] / maintenance / rebuildLocalisationCache.php
index 5833f8c..47866dc 100644 (file)
@@ -52,6 +52,7 @@ class RebuildLocalisationCache extends Maintenance {
                if ( $this->hasOption( 'memory-limit' ) ) {
                        return parent::memoryLimit();
                }
+
                return '1000M';
        }
 
@@ -112,6 +113,7 @@ class RebuildLocalisationCache extends Maintenance {
                $total = count( $codes );
                $chunks = array_chunk( $codes, ceil( count( $codes ) / $threads ) );
                $pids = array();
+               $parentStatus = 0;
                foreach ( $chunks as $codes ) {
                        // Do not fork for only one thread
                        $pid = ( $threads > 1 ) ? pcntl_fork() : -1;
@@ -120,9 +122,8 @@ class RebuildLocalisationCache extends Maintenance {
                                // Child, reseed because there is no bug in PHP:
                                // http://bugs.php.net/bug.php?id=42465
                                mt_srand( getmypid() );
-                               $numRebuilt = $this->doRebuild( $codes, $lc, $force );
-                               // Abuse the exit value for the count of rebuild languages
-                               exit( $numRebuilt );
+                               $this->doRebuild( $codes, $lc, $force );
+                               exit( 0 );
                        } elseif ( $pid === -1 ) {
                                // Fork failed or one thread, do it serialized
                                $numRebuilt += $this->doRebuild( $codes, $lc, $force );
@@ -135,22 +136,29 @@ class RebuildLocalisationCache extends Maintenance {
                foreach ( $pids as $pid ) {
                        $status = 0;
                        pcntl_waitpid( $pid, $status );
-                       // Fetch the count from the return value
-                       $numRebuilt += pcntl_wexitstatus( $status );
+                       if ( pcntl_wexitstatus( $status ) ) {
+                               // Pass a fatal error code through to the caller
+                               $parentStatus = pcntl_wexitstatus( $status );
+                       }
                }
 
-               $this->output( "$numRebuilt languages rebuilt out of $total\n" );
-               if ( $numRebuilt === 0 ) {
-                       $this->output( "Use --force to rebuild the caches which are still fresh.\n" );
+               if ( !$pids ) {
+                       $this->output( "$numRebuilt languages rebuilt out of $total\n" );
+                       if ( $numRebuilt === 0 ) {
+                               $this->output( "Use --force to rebuild the caches which are still fresh.\n" );
+                       }
+               }
+               if ( $parentStatus ) {
+                       exit( $parentStatus );
                }
        }
 
        /**
         * Helper function to rebuild list of languages codes. Prints the code
         * for each language which is rebuilt.
-        * @param $codes array List of language codes to rebuild.
-        * @param $lc LocalisationCache Instance of LocalisationCacheBulkLoad (?)
-        * @param $force bool Rebuild up-to-date languages
+        * @param array $codes List of language codes to rebuild.
+        * @param LocalisationCache $lc Instance of LocalisationCacheBulkLoad (?)
+        * @param bool $force Rebuild up-to-date languages
         * @return int Number of rebuilt languages
         */
        private function doRebuild( $codes, $lc, $force ) {
@@ -162,6 +170,7 @@ class RebuildLocalisationCache extends Maintenance {
                                $numRebuilt++;
                        }
                }
+
                return $numRebuilt;
        }