From 0622fbb077eba5df2bacbb24b03cf3ff7939c417 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Thu, 1 Sep 2016 09:43:01 -0700 Subject: [PATCH] Set "run when DBs idle" DeferredUpdates logic in Maintenance * This is a more robust version of the one inside DeferredUpdates (checking all DBs), which will be replaced in a later commit. * Make sure the listener is restored when the LB gets torn down in tests. Also, it should respect the $wgCommandLineMode value as tests can change it. Change-Id: I1e3faa5a058df44e2d27ab5ac185930867eb68ac --- maintenance/Maintenance.php | 29 ++++++++++++++++++++++++++++- tests/phpunit/MediaWikiTestCase.php | 5 +++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php index 343687e61c..698fd9a4d0 100644 --- a/maintenance/Maintenance.php +++ b/maintenance/Maintenance.php @@ -554,11 +554,38 @@ abstract class Maintenance { * @since 1.28 */ public function setTriggers() { + $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); + self::setLBFactoryTriggers( $lbFactory ); + } + + /** + * @param LBFactory $LBFactory + * @since 1.28 + */ + public static function setLBFactoryTriggers( LBFactory $LBFactory ) { // Hook into period lag checks which often happen in long-running scripts $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); $lbFactory->setWaitForReplicationListener( __METHOD__, - [ 'DeferredUpdates', 'tryOpportunisticExecute' ] + function () { + global $wgCommandLineMode; + // Check config in case of JobRunner and unit tests + if ( $wgCommandLineMode ) { + DeferredUpdates::tryOpportunisticExecute( 'run' ); + } + } + ); + // 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; + // Check config in case of JobRunner and unit tests + if ( $wgCommandLineMode && $trigger === IDatabase::TRIGGER_COMMIT ) { + DeferredUpdates::tryOpportunisticExecute( 'run' ); + } + } ); } diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index 27f1454dca..541ac11518 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -482,6 +482,11 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { DeferredUpdates::clearPendingUpdates(); ObjectCache::getMainWANInstance()->clearProcessCache(); + // XXX: reset maintenance triggers + // Hook into period lag checks which often happen in long-running scripts + $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); + Maintenance::setLBFactoryTriggers( $lbFactory ); + ob_start( 'MediaWikiTestCase::wfResetOutputBuffersBarrier' ); } -- 2.20.1