From cfeca6aaa5aa82dc08538073ca55070b2f7dd960 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Niklas=20Laxstr=C3=B6m?= Date: Fri, 31 Jul 2009 09:57:59 +0000 Subject: [PATCH] Added --threads=N parameter --- maintenance/rebuildLocalisationCache.php | 44 +++++++++++++++++++----- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/maintenance/rebuildLocalisationCache.php b/maintenance/rebuildLocalisationCache.php index fba69a4ee4..bc61eb165f 100644 --- a/maintenance/rebuildLocalisationCache.php +++ b/maintenance/rebuildLocalisationCache.php @@ -8,12 +8,14 @@ * php rebuildLocalisationCache.php [--force] * * Use --force to rebuild all files, even the ones that are not out of date. + * Use --threads=N to fork more threads. */ require( dirname(__FILE__).'/commandLine.inc' ); ini_set( 'memory_limit', '200M' ); $force = isset( $options['force'] ); +$threads = intval( isset( $options['threads'] ) ? $options['threads'] : 1 ); $conf = $wgLocalisationCacheConf; $conf['manualRecache'] = false; // Allow fallbacks to create CDB files @@ -24,18 +26,44 @@ $lc = new LocalisationCache_BulkLoad( $conf ); $codes = array_keys( Language::getLanguageNames( true ) ); sort( $codes ); + +// Initialise and split into chunks $numRebuilt = 0; -foreach ( $codes as $code ) { - if ( $force || $lc->isExpired( $code ) ) { - echo "Rebuilding $code...\n"; - $lc->recache( $code ); - $numRebuilt++; +$total = count($codes); +$chunks = array_chunk( $codes, ceil(count($codes)/$threads) ); +$pids = array(); + +foreach ( $chunks as $codes ) { + // Do not fork for only one thread + $pid = ( $threads > 1 ) ? pcntl_fork() : -1; + + if ( $pid === 0 ) { + // Child + doRebuild( $codes, $numRebuilt, $lc, $force ); + exit(); + } elseif ($pid === -1) { + // Fork failed or one thread, do it serialized + doRebuild( $codes, $numRebuilt, $lc, $force ); + } else { + // Main thread + $pids[] = $pid; } } -echo "$numRebuilt languages rebuilt out of " . count( $codes ) . ".\n"; + +// Wait for all children +foreach ( $pids as $pid ) pcntl_waitpid($pid, $status); + +echo "$numRebuilt languages rebuilt out of $total.\n"; if ( $numRebuilt == 0 ) { echo "Use --force to rebuild the caches which are still fresh.\n"; } - - +function doRebuild( $codes, &$numRebuilt, $lc, $force ) { + foreach ( $codes as $code ) { + if ( $force || $lc->isExpired( $code ) ) { + echo "Rebuilding $code...\n"; + $lc->recache( $code ); + $numRebuilt++; + } + } +} -- 2.20.1