Merge maintenance-work branch:
[lhc/web/wiklou.git] / maintenance / nextJobDB.php
1 <?php
2 /**
3 * Pick a database that has pending jobs
4 *
5 * @file
6 * @ingroup Maintenance
7 */
8
9 require_once( "Maintenance.php" );
10
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 );
16 }
17 public function execute() {
18 global $wgMemc;
19 $type = $this->getParam( 'type', false );
20 $mckey = $type === false
21 ? "jobqueue:dbs"
22 : "jobqueue:dbs:$type";
23 $pendingDBs = $wgMemcKey->get( $mckey );
24
25 # If we didn't get it from the cache
26 if( !$pendingDBs ) {
27 $pendingDBs = $this->getPendingDbs( $type );
28 $wgMemc->get( $mckey, $pendingDBs, 300 )
29 }
30 # If we've got a pending job in a db, display it.
31 if ( $pendingDBs ) {
32 $this->output( $pendingDBs[mt_rand(0, count( $pendingDBs ) - 1)] );
33 }
34 }
35
36 /**
37 * Get all databases that have a pending job
38 * @param $type String Job type
39 * @return array
40 */
41 private function getPendingDbs( $type ) {
42 $pendingDBs = array();
43 # Cross-reference DBs by master DB server
44 $dbsByMaster = array();
45 foreach ( $wgLocalDatabases as $db ) {
46 $lb = wfGetLB( $db );
47 $dbsByMaster[$lb->getServerName(0)][] = $db;
48 }
49
50 foreach ( $dbsByMaster as $master => $dbs ) {
51 $dbConn = wfGetDB( DB_MASTER, array(), $dbs[0] );
52 $stype = $dbConn->addQuotes($type);
53 $jobTable = $dbConn->getTable( 'job' );
54
55 # Padding row for MySQL bug
56 $sql = "(SELECT '-------------------------------------------')";
57 foreach ( $dbs as $dbName ) {
58 if ( $sql != '' ) {
59 $sql .= ' UNION ';
60 }
61 if ($type === false)
62 $sql .= "(SELECT '$dbName' FROM `$dbName`.$jobTable LIMIT 1)";
63 else
64 $sql .= "(SELECT '$dbName' FROM `$dbName`.$jobTable WHERE job_cmd=$stype LIMIT 1)";
65 }
66 $res = $dbConn->query( $sql, __METHOD__ );
67 $row = $dbConn->fetchRow( $res ); // discard padding row
68 while ( $row = $dbConn->fetchRow( $res ) ) {
69 $pendingDBs[] = $row[0];
70 }
71 }
72 }
73 }
74
75 $maintClass = "nextJobDb";
76 require_once( DO_MAINTENANCE );