WARNING: HUGE COMMIT
[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 $options = array( 'type' );
10
11 require_once( 'commandLine.inc' );
12
13 $type = isset($options['type'])
14 ? $options['type']
15 : false;
16
17 $mckey = $type === false
18 ? "jobqueue:dbs"
19 : "jobqueue:dbs:$type";
20
21 $pendingDBs = $wgMemc->get( $mckey );
22 if ( !$pendingDBs ) {
23 $pendingDBs = array();
24 # Cross-reference DBs by master DB server
25 $dbsByMaster = array();
26 foreach ( $wgLocalDatabases as $db ) {
27 $lb = wfGetLB( $db );
28 $dbsByMaster[$lb->getServerName(0)][] = $db;
29 }
30
31 foreach ( $dbsByMaster as $master => $dbs ) {
32 $dbConn = wfGetDB( DB_MASTER, array(), $dbs[0] );
33 $stype = $dbConn->addQuotes($type);
34
35 # Padding row for MySQL bug
36 $sql = "(SELECT '-------------------------------------------')";
37 foreach ( $dbs as $dbName ) {
38 if ( $sql != '' ) {
39 $sql .= ' UNION ';
40 }
41 if ($type === false)
42 $sql .= "(SELECT '$dbName' FROM `$dbName`.job LIMIT 1)";
43 else
44 $sql .= "(SELECT '$dbName' FROM `$dbName`.job WHERE job_cmd=$stype LIMIT 1)";
45 }
46 $res = $dbConn->query( $sql, 'nextJobDB.php' );
47 $row = $dbConn->fetchRow( $res ); // discard padding row
48 while ( $row = $dbConn->fetchRow( $res ) ) {
49 $pendingDBs[] = $row[0];
50 }
51 }
52
53 $wgMemc->set( $mckey, $pendingDBs, 300 );
54 }
55
56 if ( $pendingDBs ) {
57 echo $pendingDBs[mt_rand(0, count( $pendingDBs ) - 1)];
58 }
59
60