X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/mediawiki.git/blobdiff_plain/d7967d5e4460e08b6b258307afbca0596b18a3dd..19e297c21b10b1b8a3acad5e73fc71dcb35db44a:/maintenance/nextJobDB.php diff --git a/maintenance/nextJobDB.php b/maintenance/nextJobDB.php index dfa8d028..8d8229e2 100644 --- a/maintenance/nextJobDB.php +++ b/maintenance/nextJobDB.php @@ -1,62 +1,103 @@ get( $mckey ); -if ( !$pendingDBs ) { - $pendingDBs = array(); - # Cross-reference DBs by master DB server - $dbsByMaster = array(); - $defaultMaster = $wgAlternateMaster['DEFAULT']; - foreach ( $wgLocalDatabases as $db ) { - if ( isset( $wgAlternateMaster[$db] ) ) { - $dbsByMaster[$wgAlternateMaster[$db]][] = $db; - } else { - $dbsByMaster[$defaultMaster][] = $db; - } +require_once( dirname( __FILE__ ) . '/Maintenance.php' ); + +class nextJobDB extends Maintenance { + public function __construct() { + parent::__construct(); + $this->mDescription = "Pick a database that has pending jobs"; + $this->addOption( 'type', "The type of job to search for", false, true ); } - foreach ( $dbsByMaster as $master => $dbs ) { - $dbConn = new Database( $master, $wgDBuser, $wgDBpassword ); - $stype = $dbConn->addQuotes($type); + public function execute() { + global $wgMemc; + $type = $this->getOption( 'type', false ); + $mckey = $type === false + ? "jobqueue:dbs" + : "jobqueue:dbs:$type"; + $pendingDBs = $wgMemc->get( $mckey ); - # Padding row for MySQL bug - $sql = "(SELECT '-------------------------------------------')"; - foreach ( $dbs as $dbName ) { - if ( $sql != '' ) { - $sql .= ' UNION '; - } - if ($type === false) - $sql .= "(SELECT '$dbName' FROM `$dbName`.job LIMIT 1)"; - else - $sql .= "(SELECT '$dbName' FROM `$dbName`.job WHERE job_cmd=$stype LIMIT 1)"; + # If we didn't get it from the cache + if ( !$pendingDBs ) { + $pendingDBs = $this->getPendingDbs( $type ); + $wgMemc->set( $mckey, $pendingDBs, 300 ); } - $res = $dbConn->query( $sql, 'nextJobDB.php' ); - $row = $dbConn->fetchRow( $res ); // discard padding row - while ( $row = $dbConn->fetchRow( $res ) ) { - $pendingDBs[] = $row[0]; + # If we've got a pending job in a db, display it. + if ( $pendingDBs ) { + $this->output( $pendingDBs[mt_rand( 0, count( $pendingDBs ) - 1 )] ); } } - $wgMemc->set( $mckey, $pendingDBs, 300 ); -} + /** + * Get all databases that have a pending job + * @param $type String Job type + * @return array + */ + private function getPendingDbs( $type ) { + global $wgLocalDatabases; + $pendingDBs = array(); + # Cross-reference DBs by master DB server + $dbsByMaster = array(); + foreach ( $wgLocalDatabases as $db ) { + $lb = wfGetLB( $db ); + $dbsByMaster[$lb->getServerName( 0 )][] = $db; + } -if ( $pendingDBs ) { - echo $pendingDBs[mt_rand(0, count( $pendingDBs ) - 1)]; -} + foreach ( $dbsByMaster as $dbs ) { + $dbConn = wfGetDB( DB_MASTER, array(), $dbs[0] ); + $stype = $dbConn->addQuotes( $type ); + # Padding row for MySQL bug + $sql = "(SELECT '-------------------------------------------' as db)"; + foreach ( $dbs as $wikiId ) { + if ( $sql != '' ) { + $sql .= ' UNION '; + } + + list( $dbName, $tablePrefix ) = wfSplitWikiID( $wikiId ); + $dbConn->tablePrefix( $tablePrefix ); + $jobTable = $dbConn->tableName( 'job' ); + + if ( $type === false ) + $sql .= "(SELECT '$wikiId' as db FROM $dbName.$jobTable LIMIT 1)"; + else + $sql .= "(SELECT '$wikiId' as db FROM $dbName.$jobTable WHERE job_cmd=$stype LIMIT 1)"; + } + $res = $dbConn->query( $sql, __METHOD__ ); + $first = true; + foreach ( $res as $row ) { + if ( $first ) { + // discard padding row + $first = false; + continue; + } + $pendingDBs[] = $row->db; + } + } + return $pendingDBs; + } +} +$maintClass = "nextJobDb"; +require_once( RUN_MAINTENANCE_IF_MAIN );