00001 <?php
00024 require_once( dirname(__FILE__) . '/Maintenance.php' );
00025
00026 class nextJobDB extends Maintenance {
00027 public function __construct() {
00028 parent::__construct();
00029 $this->mDescription = "Pick a database that has pending jobs";
00030 $this->addOption( 'type', "The type of job to search for", false, true );
00031 }
00032
00033 public function execute() {
00034 global $wgMemc;
00035 $type = $this->getOption( 'type', false );
00036 $mckey = $type === false
00037 ? "jobqueue:dbs"
00038 : "jobqueue:dbs:$type";
00039 $pendingDBs = $wgMemc->get( $mckey );
00040
00041 # If we didn't get it from the cache
00042 if( !$pendingDBs ) {
00043 $pendingDBs = $this->getPendingDbs( $type );
00044 $wgMemc->get( $mckey, $pendingDBs, 300 );
00045 }
00046 # If we've got a pending job in a db, display it.
00047 if ( $pendingDBs ) {
00048 $this->output( $pendingDBs[mt_rand(0, count( $pendingDBs ) - 1)] );
00049 }
00050 }
00051
00057 private function getPendingDbs( $type ) {
00058 global $wgLocalDatabases;
00059 $pendingDBs = array();
00060 # Cross-reference DBs by master DB server
00061 $dbsByMaster = array();
00062 foreach ( $wgLocalDatabases as $db ) {
00063 $lb = wfGetLB( $db );
00064 $dbsByMaster[$lb->getServerName(0)][] = $db;
00065 }
00066
00067 foreach ( $dbsByMaster as $master => $dbs ) {
00068 $dbConn = wfGetDB( DB_MASTER, array(), $dbs[0] );
00069 $stype = $dbConn->addQuotes( $type );
00070
00071 # Padding row for MySQL bug
00072 $sql = "(SELECT '-------------------------------------------' as db)";
00073 foreach ( $dbs as $wikiId ) {
00074 if ( $sql != '' ) {
00075 $sql .= ' UNION ';
00076 }
00077
00078 list( $dbName, $tablePrefix ) = wfSplitWikiID( $wikiId );
00079 $dbConn->tablePrefix( $tablePrefix );
00080 $jobTable = $dbConn->tableName( 'job' );
00081
00082 if ( $type === false )
00083 $sql .= "(SELECT '$wikiId' as db FROM $dbName.$jobTable LIMIT 1)";
00084 else
00085 $sql .= "(SELECT '$wikiId' as db FROM $dbName.$jobTable WHERE job_cmd=$stype LIMIT 1)";
00086 }
00087 $res = $dbConn->query( $sql, __METHOD__ );
00088 $first = true;
00089 foreach ( $res as $row ) {
00090 if ( $first ) {
00091
00092 $first = false;
00093 continue;
00094 }
00095 $pendingDBs[] = $row->db;
00096 }
00097 }
00098 return $pendingDBs;
00099 }
00100 }
00101
00102 $maintClass = "nextJobDb";
00103 require_once( DO_MAINTENANCE );