diff options
author | Derick Alangi <alangiderick@gmail.com> | 2023-09-07 11:53:02 +0100 |
---|---|---|
committer | D3r1ck01 <dalangi-ctr@wikimedia.org> | 2023-09-12 17:00:09 +0000 |
commit | ab1bfd9e3a5fa21dffabb3d3599b83d8c471420c (patch) | |
tree | b0d7486c2f9cc558f1e5a8ec8b7f9b7144180f2b /maintenance/includes | |
parent | 35e0172da00ec0bf60964170e5153aaaf2505888 (diff) | |
download | mediawikicore-ab1bfd9e3a5fa21dffabb3d3599b83d8c471420c.tar.gz mediawikicore-ab1bfd9e3a5fa21dffabb3d3599b83d8c471420c.zip |
maintenance: Move `shutdown()` logic to MaintenanceRunner
The shutdown logic intuitively feels like the job of the runner and
the maintenance script themselves don't need to have knowledge about
this routine.
Maintenance script should really only execute their tasks after being
invoked by the runner and leave the runner to clean the house at the
end of their execution. Hence shifting the logic to the runner class.
Change-Id: I3d6fbf02b5fcd1414fde3a84e98a08e58456d668
Diffstat (limited to 'maintenance/includes')
-rw-r--r-- | maintenance/includes/Maintenance.php | 42 | ||||
-rw-r--r-- | maintenance/includes/MaintenanceRunner.php | 64 |
2 files changed, 62 insertions, 44 deletions
diff --git a/maintenance/includes/Maintenance.php b/maintenance/includes/Maintenance.php index 9d20938fe7d6..5b51335b4061 100644 --- a/maintenance/includes/Maintenance.php +++ b/maintenance/includes/Maintenance.php @@ -972,48 +972,6 @@ abstract class Maintenance { } /** - * Call before exiting CLI process for the last DB commit, and flush - * any remaining buffers and other deferred work. - * - * Equivalent to MediaWiki::restInPeace which handles shutdown for web requests, - * and should perform the same operations and in the same order. - * - * @since 1.36 - */ - public function shutdown() { - $lbFactory = null; - if ( - $this->getDbType() !== self::DB_NONE && - // Service might be disabled, e.g. when running install.php - !$this->getServiceContainer()->isServiceDisabled( 'DBLoadBalancerFactory' ) - ) { - $lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory(); - if ( $lbFactory->isReadyForRoundOperations() ) { - $lbFactory->commitPrimaryChanges( get_class( $this ) ); - } - - DeferredUpdates::doUpdates(); - } - - // Handle profiler outputs - // NOTE: MaintenanceRunner ensures Profiler::setAllowOutput() during setup - $profiler = Profiler::instance(); - $profiler->logData(); - $profiler->logDataPageOutputOnly(); - - MediaWiki::emitBufferedStatsdData( - $this->getServiceContainer()->getStatsdDataFactory(), - $this->getConfig() - ); - - if ( $lbFactory ) { - if ( $lbFactory->isReadyForRoundOperations() ) { - $lbFactory->shutdown( $lbFactory::SHUTDOWN_NO_CHRONPROT ); - } - } - } - - /** * Support function for cleaning up redundant text records * @param bool $delete Whether or not to actually delete the records * @author Rob Church <robchur@gmail.com> diff --git a/maintenance/includes/MaintenanceRunner.php b/maintenance/includes/MaintenanceRunner.php index d68d5fb3316c..658c1f0fb9c6 100644 --- a/maintenance/includes/MaintenanceRunner.php +++ b/maintenance/includes/MaintenanceRunner.php @@ -2,10 +2,13 @@ namespace MediaWiki\Maintenance; +use Config; +use DeferredUpdates; use Exception; use LCStoreNull; use LogicException; use Maintenance; +use MediaWiki; use MediaWiki\Logger\LoggerFactory; use MediaWiki\MainConfigNames; use MediaWiki\MediaWikiServices; @@ -52,6 +55,9 @@ class MaintenanceRunner { /** @var bool */ private bool $withoutLocalSettings = false; + /** @var ?Config */ + private ?Config $config = null; + /** * Default constructor. Children should call this *first* if implementing * their own constructors @@ -63,6 +69,14 @@ class MaintenanceRunner { $this->addDefaultParams(); } + private function getConfig() { + if ( $this->config === null ) { + $this->config = $this->getServiceContainer()->getMainConfig(); + } + + return $this->config; + } + /** * Add the default parameters to the scripts */ @@ -630,6 +644,10 @@ class MaintenanceRunner { $this->scriptObject->finalSetup( $settingsBuilder ); } + private function getServiceContainer(): MediaWikiServices { + return MediaWikiServices::getInstance(); + } + /** * Run the maintenance script. * @@ -645,7 +663,7 @@ class MaintenanceRunner { * passed through from Maintenance::execute(). */ public function run(): bool { - $config = MediaWikiServices::getInstance()->getMainConfig(); + $config = $this->getConfig(); // Apply warning thresholds and output mode to Profiler. // This MUST happen after Setup.php calls MaintenanceRunner::setup, @@ -689,7 +707,7 @@ class MaintenanceRunner { print_r( $GLOBALS ); } - $this->scriptObject->shutdown(); + $this->shutdown(); return $success; } catch ( Exception $ex ) { @@ -774,4 +792,46 @@ class MaintenanceRunner { } } + /** + * Call before exiting CLI process for the last DB commit, and flush + * any remaining buffers and other deferred work. + * + * Equivalent to MediaWiki::restInPeace which handles shutdown for web requests, + * and should perform the same operations and in the same order. + * + * @since 1.41 + */ + private function shutdown() { + $lbFactory = null; + if ( + $this->scriptObject->getDbType() !== Maintenance::DB_NONE && + // Service might be disabled, e.g. when running install.php + !$this->getServiceContainer()->isServiceDisabled( 'DBLoadBalancerFactory' ) + ) { + $lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory(); + if ( $lbFactory->isReadyForRoundOperations() ) { + $lbFactory->commitPrimaryChanges( get_class( $this ) ); + } + + DeferredUpdates::doUpdates(); + } + + // Handle profiler outputs + // NOTE: MaintenanceRunner ensures Profiler::setAllowOutput() during setup + $profiler = Profiler::instance(); + $profiler->logData(); + $profiler->logDataPageOutputOnly(); + + MediaWiki::emitBufferedStatsdData( + $this->getServiceContainer()->getStatsdDataFactory(), + $this->getConfig() + ); + + if ( $lbFactory ) { + if ( $lbFactory->isReadyForRoundOperations() ) { + $lbFactory->shutdown( $lbFactory::SHUTDOWN_NO_CHRONPROT ); + } + } + } + } |