X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=maintenance%2FMaintenance.php;h=7e4bf7c043f461400b6a34a76d32334330d1cf5f;hp=b8c554d226dabe069fde140df9b29903df9ed40c;hb=220bda9175a18458449e9d754fb48830c1f76f25;hpb=fdb7d369035d0826113f9d8b6c3e99a9bba073be diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php index b8c554d226..7e4bf7c043 100644 --- a/maintenance/Maintenance.php +++ b/maintenance/Maintenance.php @@ -182,7 +182,7 @@ abstract class Maintenance { if ( $count < 2 ) { return false; // sanity } - if ( $bt[0]['class'] !== 'Maintenance' || $bt[0]['function'] !== 'shouldExecute' ) { + if ( $bt[0]['class'] !== self::class || $bt[0]['function'] !== 'shouldExecute' ) { return false; // last call should be to this function } $includeFuncs = [ 'require_once', 'require', 'include', 'include_once' ]; @@ -381,6 +381,15 @@ abstract class Maintenance { * @param mixed $channel Unique identifier for the channel. See function outputChanneled. */ protected function output( $out, $channel = null ) { + // This is sometimes called very early, before Setup.php is included. + if ( class_exists( MediaWikiServices::class ) ) { + // Try to periodically flush buffered metrics to avoid OOMs + $stats = MediaWikiServices::getInstance()->getStatsdDataFactory(); + if ( $stats->getDataCount() > 1000 ) { + MediaWiki::emitBufferedStatsdData( $stats, $this->getConfig() ); + } + } + if ( $this->mQuiet ) { return; } @@ -405,7 +414,10 @@ abstract class Maintenance { $this->fatalError( $err, intval( $die ) ); } $this->outputChanneled( false ); - if ( PHP_SAPI == 'cli' ) { + if ( + ( PHP_SAPI == 'cli' || PHP_SAPI == 'phpdbg' ) && + !defined( 'MW_PHPUNIT_TEST' ) + ) { fwrite( STDERR, $err . "\n" ); } else { print $err; @@ -593,36 +605,41 @@ abstract class Maintenance { $lbFactory->setAgentName( mb_strlen( $agent ) > 15 ? mb_substr( $agent, 0, 15 ) . '...' : $agent ); - self::setLBFactoryTriggers( $lbFactory ); + self::setLBFactoryTriggers( $lbFactory, $this->getConfig() ); } /** * @param LBFactory $LBFactory + * @param Config $config * @since 1.28 */ - public static function setLBFactoryTriggers( LBFactory $LBFactory ) { + public static function setLBFactoryTriggers( LBFactory $LBFactory, Config $config ) { + $services = MediaWikiServices::getInstance(); + $stats = $services->getStatsdDataFactory(); // Hook into period lag checks which often happen in long-running scripts - $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); + $lbFactory = $services->getDBLoadBalancerFactory(); $lbFactory->setWaitForReplicationListener( __METHOD__, - function () { - global $wgCommandLineMode; + function () use ( $stats, $config ) { // Check config in case of JobRunner and unit tests - if ( $wgCommandLineMode ) { + if ( $config->get( 'CommandLineMode' ) ) { DeferredUpdates::tryOpportunisticExecute( 'run' ); } + // Try to periodically flush buffered metrics to avoid OOMs + MediaWiki::emitBufferedStatsdData( $stats, $config ); } ); // Check for other windows to run them. A script may read or do a few writes // to the master but mostly be writing to something else, like a file store. $lbFactory->getMainLB()->setTransactionListener( __METHOD__, - function ( $trigger ) { - global $wgCommandLineMode; + function ( $trigger ) use ( $stats, $config ) { // Check config in case of JobRunner and unit tests - if ( $wgCommandLineMode && $trigger === IDatabase::TRIGGER_COMMIT ) { + if ( $config->get( 'CommandLineMode' ) && $trigger === IDatabase::TRIGGER_COMMIT ) { DeferredUpdates::tryOpportunisticExecute( 'run' ); } + // Try to periodically flush buffered metrics to avoid OOMs + MediaWiki::emitBufferedStatsdData( $stats, $config ); } ); } @@ -664,7 +681,8 @@ abstract class Maintenance { global $IP, $wgCommandLineMode, $wgRequestTime; # Abort if called from a web server - if ( PHP_SAPI !== 'cli' ) { + # wfIsCLI() is not available yet + if ( PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ) { $this->fatalError( 'This script must be run from the command line' ); } @@ -1145,9 +1163,9 @@ abstract class Maintenance { $wgShowSQLErrors = true; - MediaWiki\suppressWarnings(); + Wikimedia\suppressWarnings(); set_time_limit( 0 ); - MediaWiki\restoreWarnings(); + Wikimedia\restoreWarnings(); $this->adjustMemoryLimit(); } @@ -1199,6 +1217,9 @@ abstract class Maintenance { "must exist and be readable in the source directory.\n" . "Use --conf to specify it." ); } + if ( isset( $this->mOptions['server'] ) ) { + $_SERVER['SERVER_NAME'] = $this->mOptions['server']; + } $wgCommandLineMode = true; return $settingsFile;