X-Git-Url: https://git.cyclocoop.org/%7B%7B%20url_for%28%27votes%27%2C%20votes=%27waiting%27%29%20%7D%7D?a=blobdiff_plain;f=maintenance%2FMaintenance.php;h=861a61328ebe6c8dcc2af13737f14738ac610620;hb=129556f6fe3189ce2b515089757bdb9da80cb43c;hp=255892b52dcb803a952e6774a582c4b08228d7b3;hpb=4a811bd14b28af7465e6d1df1fb61291de4b5911;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php index 255892b52d..861a61328e 100644 --- a/maintenance/Maintenance.php +++ b/maintenance/Maintenance.php @@ -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,7 @@ abstract class Maintenance { $this->fatalError( $err, intval( $die ) ); } $this->outputChanneled( false ); - if ( PHP_SAPI == 'cli' ) { + if ( PHP_SAPI == 'cli' || PHP_SAPI == 'phpdbg' ) { fwrite( STDERR, $err . "\n" ); } else { print $err; @@ -591,36 +600,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 ); } ); } @@ -662,7 +676,8 @@ abstract class Maintenance { global $IP, $wgCommandLineMode, $wgRequestTime; # Abort if called from a web server - if ( isset( $_SERVER ) && isset( $_SERVER['REQUEST_METHOD'] ) ) { + # wfIsCLI() is not available yet + if ( PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ) { $this->fatalError( 'This script must be run from the command line' ); }