summaryrefslogtreecommitdiff
path: root/maintenance/nextJobDB.php
blob: b2500caf78aaa4e1dc6d86a55142d3f49996a79b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php

/*
 * Pick a database that has pending jobs
 */

$options = array( 'type'  );

require_once( 'commandLine.inc' );

$type = isset($options['type'])
		? $options['type']
		: false;

$mckey = $type === false
            ? "jobqueue:dbs"
            : "jobqueue:dbs:$type";

$pendingDBs = $wgMemc->get( $mckey );
if ( !$pendingDBs ) {
	$pendingDBs = array();
	# Cross-reference DBs by master DB server
	$dbsByMaster = array();
	$defaultMaster = isset( $wgAlternateMaster['DEFAULT'] )
		? $wgAlternateMaster['DEFAULT']
		: $wgDBserver;
	foreach ( $wgLocalDatabases as $db ) {
		if ( isset( $wgAlternateMaster[$db] ) ) {
			$dbsByMaster[$wgAlternateMaster[$db]][] = $db;
		} else {
			$dbsByMaster[$defaultMaster][] = $db;
		}
	}

	foreach ( $dbsByMaster as $master => $dbs ) {
		$dbConn = new Database( $master, $wgDBuser, $wgDBpassword, $dbs[0] );
		$stype = $dbConn->addQuotes($type);

		# Padding row for MySQL bug
		$sql = "(SELECT '-------------------------------------------')";
		foreach ( $dbs as $dbName ) {
			if ( $sql != '' ) {
				$sql .= ' UNION ';
			}
			if ($type === false)
				$sql .= "(SELECT '$dbName' FROM `$dbName`.job LIMIT 1)";
			else
				$sql .= "(SELECT '$dbName' FROM `$dbName`.job WHERE job_cmd=$stype LIMIT 1)";
		}
		$res = $dbConn->query( $sql, 'nextJobDB.php' );
		$row = $dbConn->fetchRow( $res ); // discard padding row
		while ( $row = $dbConn->fetchRow( $res ) ) {
			$pendingDBs[] = $row[0];
		}
	}

	$wgMemc->set( $mckey, $pendingDBs, 300 );
}

if ( $pendingDBs ) {
	echo $pendingDBs[mt_rand(0, count( $pendingDBs ) - 1)];
}