Merge "Give TestCase::checkHasDiff3 a better name"
[lhc/web/wiklou.git] / includes / jobqueue / jobs / CategoryMembershipChangeJob.php
index 2b0018d..5dc2acb 100644 (file)
@@ -49,21 +49,22 @@ class CategoryMembershipChangeJob extends Job {
                }
 
                $dbw = wfGetDB( DB_MASTER );
-
                // Use a named lock so that jobs for this page see each others' changes
-               $fname = __METHOD__;
                $lockKey = "CategoryMembershipUpdates:{$page->getId()}";
-               if ( !$dbw->lock( $lockKey, $fname, 10 ) ) {
+               $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__, 10 );
+               if ( !$scopedLock ) {
                        $this->setLastError( "Could not acquire lock '$lockKey'" );
                        return false;
                }
 
-               $unlocker = new ScopedCallback( function () use ( $dbw, $lockKey, $fname ) {
-                       $dbw->unlock( $lockKey, $fname );
-               } );
-
-               // Sanity: clear any DB transaction snapshot
-               $dbw->commit( __METHOD__, 'flush' );
+               $dbr = wfGetDB( DB_SLAVE, array( 'recentchanges' ) );
+               // Wait till the slave is caught up so that jobs for this page see each others' changes
+               if ( !wfGetLB()->safeWaitForMasterPos( $dbr ) ) {
+                       $this->setLastError( "Timed out while waiting for slave to catch up" );
+                       return false;
+               }
+               // Clear any stale REPEATABLE-READ snapshot
+               $dbr->commit( __METHOD__, 'flush' );
 
                $cutoffUnix = wfTimestamp( TS_UNIX, $this->params['revTimestamp'] );
                // Using ENQUEUE_FUDGE_SEC handles jobs inserted out of revision order due to the delay
@@ -71,12 +72,12 @@ class CategoryMembershipChangeJob extends Job {
                $cutoffUnix -= self::ENQUEUE_FUDGE_SEC;
 
                // Get the newest revision that has a SRC_CATEGORIZE row...
-               $row = $dbw->selectRow(
+               $row = $dbr->selectRow(
                        array( 'revision', 'recentchanges' ),
                        array( 'rev_timestamp', 'rev_id' ),
                        array(
                                'rev_page' => $page->getId(),
-                               'rev_timestamp >= ' . $dbw->addQuotes( $dbw->timestamp( $cutoffUnix ) )
+                               'rev_timestamp >= ' . $dbr->addQuotes( $dbr->timestamp( $cutoffUnix ) )
                        ),
                        __METHOD__,
                        array( 'ORDER BY' => 'rev_timestamp DESC, rev_id DESC' ),
@@ -103,8 +104,8 @@ class CategoryMembershipChangeJob extends Job {
 
                // Find revisions to this page made around and after this revision which lack category
                // notifications in recent changes. This lets jobs pick up were the last one left off.
-               $encCutoff = $dbw->addQuotes( $dbw->timestamp( $cutoffUnix ) );
-               $res = $dbw->select(
+               $encCutoff = $dbr->addQuotes( $dbr->timestamp( $cutoffUnix ) );
+               $res = $dbr->select(
                        'revision',
                        Revision::selectFields(),
                        array(
@@ -121,8 +122,6 @@ class CategoryMembershipChangeJob extends Job {
                        $this->notifyUpdatesForRevision( $page, Revision::newFromRow( $row ) );
                }
 
-               ScopedCallback::consume( $unlocker );
-
                return true;
        }
 
@@ -165,20 +164,20 @@ class CategoryMembershipChangeJob extends Job {
                $insertCount = 0;
 
                foreach ( $categoryInserts as $categoryName ) {
-                       $categoryTitle = Title::newFromText( $categoryName, NS_CATEGORY );
+                       $categoryTitle = Title::makeTitle( NS_CATEGORY, $categoryName );
                        $catMembChange->triggerCategoryAddedNotification( $categoryTitle );
                        if ( $insertCount++ && ( $insertCount % $batchSize ) == 0 ) {
                                $dbw->commit( __METHOD__, 'flush' );
-                               wfWaitForSlaves();
+                               wfGetLBFactory()->waitForReplication();
                        }
                }
 
                foreach ( $categoryDeletes as $categoryName ) {
-                       $categoryTitle = Title::newFromText( $categoryName, NS_CATEGORY );
+                       $categoryTitle = Title::makeTitle( NS_CATEGORY, $categoryName );
                        $catMembChange->triggerCategoryRemovedNotification( $categoryTitle );
                        if ( $insertCount++ && ( $insertCount++ % $batchSize ) == 0 ) {
                                $dbw->commit( __METHOD__, 'flush' );
-                               wfWaitForSlaves();
+                               wfGetLBFactory()->waitForReplication();
                        }
                }
        }