X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=maintenance%2FMaintenance.php;h=5adbee5fc31d0e27730ef242a794785a1cef5aba;hb=1a21a63d52b9ebf940cd35f041d675d39c9d474a;hp=10082e9503d9088af3343849619de4f7c1876a70;hpb=fc5dced1ce8151b0354e0efda28a7542713f9cbe;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php index 10082e9503..5adbee5fc3 100644 --- a/maintenance/Maintenance.php +++ b/maintenance/Maintenance.php @@ -50,7 +50,6 @@ use Wikimedia\Rdbms\IMaintainableDatabase; * is the execute() method. See docs/maintenance.txt for more info * and a quick demo of how to use it. * - * @author Chad Horohoe * @since 1.16 * @ingroup Maintenance */ @@ -183,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' ]; @@ -382,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; } @@ -406,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; @@ -592,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 ); } ); } @@ -663,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' ); }