Improved handling of pending queue delisting.
authorAaron Schulz <aschulz@wikimedia.org>
Sun, 27 Jan 2013 00:57:55 +0000 (16:57 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Sun, 27 Jan 2013 00:59:06 +0000 (16:59 -0800)
* Also removed outdated comment.

Change-Id: I5b9a4bf4ed854d58af09e377378997b23549c230

maintenance/nextJobDB.php

index 17a3f2e..7e6fd96 100644 (file)
@@ -18,7 +18,6 @@
  * http://www.gnu.org/copyleft/gpl.html
  *
  * @file
- * @todo Make this work on PostgreSQL and maybe other database servers
  * @ingroup Maintenance
  */
 
@@ -89,22 +88,17 @@ class nextJobDB extends Maintenance {
                        $candidates = array_values( $candidates );
                        $db = $candidates[ mt_rand( 0, count( $candidates ) - 1 ) ];
                        if ( !$this->checkJob( $type, $db ) ) {
-                               if ( $type === false ) {
-                                       // There are no jobs available in the current database
-                                       foreach ( $pendingDBs as $type2 => $dbs ) {
-                                               $pendingDBs[$type2] = array_diff( $pendingDBs[$type2], array( $db ) );
-                                       }
-                               } else {
-                                       // There are no jobs of this type available in the current database
-                                       $pendingDBs[$type] = array_diff( $pendingDBs[$type], array( $db ) );
-                               }
+                               $pendingDBs = $this->delistDB( $pendingDBs, $db, $type );
                                // Update the cache to remove the outdated information.
                                // Make sure that this does not race (especially with full rebuilds).
-                               $pendingDbInfo['pendingDBs'] = $pendingDBs;
                                if ( $wgMemc->add( "$memcKey:lock", 1, 60 ) ) { // lock
                                        $curInfo = $wgMemc->get( $memcKey );
-                                       if ( $curInfo && $curInfo['timestamp'] === $pendingDbInfo['timestamp'] ) {
-                                               $wgMemc->set( $memcKey, $pendingDbInfo );
+                                       if ( is_array( $curInfo ) ) {
+                                               $curInfo['pendingDBs'] =
+                                                       $this->delistDB( $curInfo['pendingDBs'], $db, $type );
+                                               $wgMemc->set( $memcKey, $curInfo );
+                                               // May as well make use of this newer information
+                                               $pendingDBs = $curInfo['pendingDBs'];
                                        }
                                        $wgMemc->delete( "$memcKey:lock" ); // unlock
                                }
@@ -115,6 +109,19 @@ class nextJobDB extends Maintenance {
                $this->output( $db . "\n" );
        }
 
+       private function delistDB( array $pendingDBs, $db, $type ) {
+               if ( $type === false ) {
+                       // There are no jobs available in the current database
+                       foreach ( $pendingDBs as $type2 => $dbs ) {
+                               $pendingDBs[$type2] = array_diff( $pendingDBs[$type2], array( $db ) );
+                       }
+               } else {
+                       // There are no jobs of this type available in the current database
+                       $pendingDBs[$type] = array_diff( $pendingDBs[$type], array( $db ) );
+               }
+               return $pendingDBs;
+       }
+
        /**
         * Check if the specified database has a job of the specified type in it.
         * The type may be false to indicate "all".
@@ -122,7 +129,7 @@ class nextJobDB extends Maintenance {
         * @param $dbName string
         * @return bool
         */
-       function checkJob( $type, $dbName ) {
+       private function checkJob( $type, $dbName ) {
                $group = JobQueueGroup::singleton( $dbName );
                if ( $type === false ) {
                        foreach ( $group->getDefaultQueueTypes() as $type ) {