Add unit test for bug 32888
[lhc/web/wiklou.git] / maintenance / nextJobDB.php
1 <?php
2 /**
3 * Pick a database that has pending jobs
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @todo Make this work on PostgreSQL and maybe other database servers
21 * @ingroup Maintenance
22 */
23
24 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
25
26 class nextJobDB extends Maintenance {
27 public function __construct() {
28 parent::__construct();
29 $this->mDescription = "Pick a database that has pending jobs";
30 $this->addOption( 'type', "The type of job to search for", false, true );
31 }
32
33 public function execute() {
34 global $wgMemc;
35 $type = $this->getOption( 'type', false );
36
37 $memcKey = 'jobqueue:dbs:v2';
38 $pendingDBs = $wgMemc->get( $memcKey );
39
40 // If the cache entry wasn't present, or in 1% of cases otherwise,
41 // regenerate the cache.
42 if ( !$pendingDBs || mt_rand( 0, 100 ) == 0 ) {
43 $pendingDBs = $this->getPendingDbs();
44 $wgMemc->set( $memcKey, $pendingDBs, 300 );
45 }
46
47 if ( !$pendingDBs ) {
48 return;
49 }
50
51 do {
52 $again = false;
53
54 if ( $type === false ) {
55 $candidates = call_user_func_array( 'array_merge', $pendingDBs );
56 } elseif ( isset( $pendingDBs[$type] ) ) {
57 $candidates = $pendingDBs[$type];
58 } else {
59 $candidates = array();
60 }
61 if ( !$candidates ) {
62 return;
63 }
64
65 $candidates = array_values( $candidates );
66 $db = $candidates[ mt_rand( 0, count( $candidates ) - 1 ) ];
67 if ( !$this->checkJob( $type, $db ) ) {
68 // This job is not available in the current database. Remove it from
69 // the cache.
70 if ( $type === false ) {
71 foreach ( $pendingDBs as $type2 => $dbs ) {
72 $pendingDBs[$type2] = array_diff( $pendingDBs[$type2], array( $db ) );
73 }
74 } else {
75 $pendingDBs[$type] = array_diff( $pendingDBs[$type], array( $db ) );
76 }
77
78 $wgMemc->set( $memcKey, $pendingDBs, 300 );
79 $again = true;
80 }
81 } while ( $again );
82
83 $this->output( $db . "\n" );
84 }
85
86 /**
87 * Check if the specified database has a job of the specified type in it.
88 * The type may be false to indicate "all".
89 * @param $type string
90 * @param $dbName string
91 * @return bool
92 */
93 function checkJob( $type, $dbName ) {
94 $lb = wfGetLB( $dbName );
95 $db = $lb->getConnection( DB_MASTER, array(), $dbName );
96 if ( $type === false ) {
97 $conds = array();
98 } else {
99 $conds = array( 'job_cmd' => $type );
100 }
101
102 $exists = (bool) $db->selectField( 'job', '1', $conds, __METHOD__ );
103 $lb->reuseConnection( $db );
104 return $exists;
105 }
106
107 /**
108 * Get all databases that have a pending job
109 * @param $type String Job type
110 * @return array
111 */
112 private function getPendingDbs() {
113 global $wgLocalDatabases;
114 $pendingDBs = array();
115 # Cross-reference DBs by master DB server
116 $dbsByMaster = array();
117 foreach ( $wgLocalDatabases as $db ) {
118 $lb = wfGetLB( $db );
119 $dbsByMaster[$lb->getServerName( 0 )][] = $db;
120 }
121
122 foreach ( $dbsByMaster as $dbs ) {
123 $dbConn = wfGetDB( DB_MASTER, array(), $dbs[0] );
124
125 # Padding row for MySQL bug
126 $pad = str_repeat( '-', 40 );
127 $sql = "(SELECT '$pad' as db, '$pad' as job_cmd)";
128 foreach ( $dbs as $wikiId ) {
129 if ( $sql != '' ) {
130 $sql .= ' UNION ';
131 }
132
133 list( $dbName, $tablePrefix ) = wfSplitWikiID( $wikiId );
134 $dbConn->tablePrefix( $tablePrefix );
135 $jobTable = $dbConn->tableName( 'job' );
136
137 $sql .= "(SELECT DISTINCT '$wikiId' as db, job_cmd FROM $dbName.$jobTable GROUP BY job_cmd)";
138 }
139 $res = $dbConn->query( $sql, __METHOD__ );
140 $first = true;
141 foreach ( $res as $row ) {
142 if ( $first ) {
143 // discard padding row
144 $first = false;
145 continue;
146 }
147 $pendingDBs[$row->job_cmd][] = $row->db;
148 }
149 }
150 return $pendingDBs;
151 }
152 }
153
154 $maintClass = "nextJobDb";
155 require_once( RUN_MAINTENANCE_IF_MAIN );