r54084 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r54083‎ | r54084 | r54085 >
Date:09:57, 31 July 2009
Author:nikerabbit
Status:resolved (Comments)
Tags:todo 
Comment:
Added --threads=N parameter
Modified paths:
  • /trunk/phase3/maintenance/rebuildLocalisationCache.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/rebuildLocalisationCache.php
@@ -8,12 +8,14 @@
99 * php rebuildLocalisationCache.php [--force]
1010 *
1111 * Use --force to rebuild all files, even the ones that are not out of date.
 12+ * Use --threads=N to fork more threads.
1213 */
1314
1415 require( dirname(__FILE__).'/commandLine.inc' );
1516 ini_set( 'memory_limit', '200M' );
1617
1718 $force = isset( $options['force'] );
 19+$threads = intval( isset( $options['threads'] ) ? $options['threads'] : 1 );
1820
1921 $conf = $wgLocalisationCacheConf;
2022 $conf['manualRecache'] = false; // Allow fallbacks to create CDB files
@@ -24,18 +26,44 @@
2527
2628 $codes = array_keys( Language::getLanguageNames( true ) );
2729 sort( $codes );
 30+
 31+// Initialise and split into chunks
2832 $numRebuilt = 0;
29 -foreach ( $codes as $code ) {
30 - if ( $force || $lc->isExpired( $code ) ) {
31 - echo "Rebuilding $code...\n";
32 - $lc->recache( $code );
33 - $numRebuilt++;
 33+$total = count($codes);
 34+$chunks = array_chunk( $codes, ceil(count($codes)/$threads) );
 35+$pids = array();
 36+
 37+foreach ( $chunks as $codes ) {
 38+ // Do not fork for only one thread
 39+ $pid = ( $threads > 1 ) ? pcntl_fork() : -1;
 40+
 41+ if ( $pid === 0 ) {
 42+ // Child
 43+ doRebuild( $codes, $numRebuilt, $lc, $force );
 44+ exit();
 45+ } elseif ($pid === -1) {
 46+ // Fork failed or one thread, do it serialized
 47+ doRebuild( $codes, $numRebuilt, $lc, $force );
 48+ } else {
 49+ // Main thread
 50+ $pids[] = $pid;
3451 }
3552 }
36 -echo "$numRebuilt languages rebuilt out of " . count( $codes ) . ".\n";
 53+
 54+// Wait for all children
 55+foreach ( $pids as $pid ) pcntl_waitpid($pid, $status);
 56+
 57+echo "$numRebuilt languages rebuilt out of $total.\n";
3758 if ( $numRebuilt == 0 ) {
3859 echo "Use --force to rebuild the caches which are still fresh.\n";
3960 }
4061
41 -
42 -
 62+function doRebuild( $codes, &$numRebuilt, $lc, $force ) {
 63+ foreach ( $codes as $code ) {
 64+ if ( $force || $lc->isExpired( $code ) ) {
 65+ echo "Rebuilding $code...\n";
 66+ $lc->recache( $code );
 67+ $numRebuilt++;
 68+ }
 69+ }
 70+}

Comments

#Comment by Brion VIBBER (talk | contribs)   00:30, 20 August 2009

This fails nastily on systems without pnctl PHP extension; was fixed up to gracefully fall back to 1 thread in r55294. Ideally should be redone to launch separate processes instead of forking so it'll be more portable.

Status & tagging log