Merge "Change "slave" => "replica DB" in /maintenance"
[lhc/web/wiklou.git] / maintenance / Maintenance.php
index f42a35c..0b61738 100644 (file)
@@ -37,6 +37,7 @@ define( 'DO_MAINTENANCE', RUN_MAINTENANCE_IF_MAIN ); // original name, harmless
 $maintClass = false;
 
 use MediaWiki\Logger\LoggerFactory;
+use MediaWiki\MediaWikiServices;
 
 /**
  * Abstract maintenance class for quickly writing and churning out
@@ -548,6 +549,46 @@ abstract class Maintenance {
 
        }
 
+       /**
+        * Set triggers like when to try to run deferred updates
+        * @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__,
+                       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' );
+                               }
+                       }
+               );
+       }
+
        /**
         * Run a child maintenance script. Pass all of the current arguments
         * to it.