nextJobDB should have a way to pick a db having specific type of job in the queue
[lhc/web/wiklou.git] / maintenance / nextJobDB.php
1 <?php
2
3 /*
4 * Pick a database that has pending jobs
5 */
6
7 $options = array( 'type' );
8
9 require_once( 'commandLine.inc' );
10
11 $type = isset($options['type'])
12 ? $options['type']
13 : false;
14
15 $pendingDBs = $wgMemc->get( 'jobqueue:dbs' );
16 if ( !$pendingDBs ) {
17 $pendingDBs = array();
18 # Cross-reference DBs by master DB server
19 $dbsByMaster = array();
20 $defaultMaster = $wgAlternateMaster['DEFAULT'];
21 foreach ( $wgLocalDatabases as $db ) {
22 if ( isset( $wgAlternateMaster[$db] ) ) {
23 $dbsByMaster[$wgAlternateMaster[$db]][] = $db;
24 } else {
25 $dbsByMaster[$defaultMaster][] = $db;
26 }
27 }
28
29 foreach ( $dbsByMaster as $master => $dbs ) {
30 $dbConn = new Database( $master, $wgDBuser, $wgDBpassword );
31 $stype = $dbConn->addQuotes($type);
32
33 # Padding row for MySQL bug
34 $sql = "(SELECT '-------------------------------------------')";
35 foreach ( $dbs as $dbName ) {
36 if ( $sql != '' ) {
37 $sql .= ' UNION ';
38 }
39 if ($type === false)
40 $sql .= "(SELECT '$dbName' FROM `$dbName`.job LIMIT 1)";
41 else
42 $sql .= "(SELECT '$dbName' FROM `$dbName`.job WHERE job_cmd='$stype' LIMIT 1)";
43 }
44 $res = $dbConn->query( $sql, 'nextJobDB.php' );
45 $row = $dbConn->fetchRow( $res ); // discard padding row
46 while ( $row = $dbConn->fetchRow( $res ) ) {
47 $pendingDBs[] = $row[0];
48 }
49 }
50
51 $wgMemc->set( 'jobqueue:dbs', $pendingDBs, 300 );
52 }
53
54 if ( $pendingDBs ) {
55 echo $pendingDBs[mt_rand(0, count( $pendingDBs ) - 1)];
56 }
57
58 ?>