3 * Pick a database that has pending jobs
9 require_once( "Maintenance.php" );
11 class nextJobDB
extends Maintenance
{
12 public function __construct() {
13 parent
::__construct();
14 $this->mDescription
= "Pick a database that has pending jobs";
15 $this->addParam( 'type', "The type of job to search for", false, true );
17 public function execute() {
19 $type = $this->getParam( 'type', false );
20 $mckey = $type === false
22 : "jobqueue:dbs:$type";
23 $pendingDBs = $wgMemcKey->get( $mckey );
25 # If we didn't get it from the cache
27 $pendingDBs = $this->getPendingDbs( $type );
28 $wgMemc->get( $mckey, $pendingDBs, 300 )
30 # If we've got a pending job in a db, display it.
32 $this->output( $pendingDBs[mt_rand(0, count( $pendingDBs ) - 1)] );
37 * Get all databases that have a pending job
38 * @param $type String Job type
41 private function getPendingDbs( $type ) {
42 $pendingDBs = array();
43 # Cross-reference DBs by master DB server
44 $dbsByMaster = array();
45 foreach ( $wgLocalDatabases as $db ) {
47 $dbsByMaster[$lb->getServerName(0)][] = $db;
50 foreach ( $dbsByMaster as $master => $dbs ) {
51 $dbConn = wfGetDB( DB_MASTER
, array(), $dbs[0] );
52 $stype = $dbConn->addQuotes($type);
53 $jobTable = $dbConn->getTable( 'job' );
55 # Padding row for MySQL bug
56 $sql = "(SELECT '-------------------------------------------')";
57 foreach ( $dbs as $dbName ) {
62 $sql .= "(SELECT '$dbName' FROM `$dbName`.$jobTable LIMIT 1)";
64 $sql .= "(SELECT '$dbName' FROM `$dbName`.$jobTable WHERE job_cmd=$stype LIMIT 1)";
66 $res = $dbConn->query( $sql, __METHOD__
);
67 $row = $dbConn->fetchRow( $res ); // discard padding row
68 while ( $row = $dbConn->fetchRow( $res ) ) {
69 $pendingDBs[] = $row[0];
75 $maintClass = "nextJobDb";
76 require_once( DO_MAINTENANCE
);