Let nextJobDB.php narrow down the type based on a set.
[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 * @file
21 * @todo Make this work on PostgreSQL and maybe other database servers
22 * @ingroup Maintenance
23 */
24
25 require_once( __DIR__ . '/Maintenance.php' );
26
27 /**
28 * Maintenance script that picks a database that has pending jobs.
29 *
30 * @ingroup Maintenance
31 */
32 class nextJobDB extends Maintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->mDescription = "Pick a database that has pending jobs";
36 $this->addOption( 'type', "Search by job type", false, true );
37 $this->addOption( 'types', "Space separated list of job types to search for", false, true );
38 }
39
40 public function execute() {
41 global $wgMemc;
42
43 $type = false; // job type required/picked
44 if ( $this->hasOption( 'types' ) ) {
45 $types = explode( ' ', $this->getOption( 'types' ) );
46 } elseif ( $this->hasOption( 'type' ) ) {
47 $types = array( $this->getOption( 'type' ) );
48 } else {
49 $types = false;
50 }
51
52 $memcKey = 'jobqueue:dbs:v3';
53 $pendingDbInfo = $wgMemc->get( $memcKey );
54
55 // If the cache entry wasn't present, is stale, or in .1% of cases otherwise,
56 // regenerate the cache. Use any available stale cache if another process is
57 // currently regenerating the pending DB information.
58 if ( !is_array( $pendingDbInfo )
59 || ( time() - $pendingDbInfo['timestamp'] ) > 300 // 5 minutes
60 || mt_rand( 0, 999 ) == 0
61 ) {
62 if ( $wgMemc->add( "$memcKey:rebuild", 1, 1800 ) ) { // lock
63 $pendingDbInfo = array(
64 'pendingDBs' => $this->getPendingDbs(),
65 'timestamp' => time()
66 );
67 for ( $attempts=1; $attempts <= 25; ++$attempts ) {
68 if ( $wgMemc->add( "$memcKey:lock", 1, 60 ) ) { // lock
69 $wgMemc->set( $memcKey, $pendingDbInfo );
70 $wgMemc->delete( "$memcKey:lock" ); // unlock
71 break;
72 }
73 }
74 $wgMemc->delete( "$memcKey:rebuild" ); // unlock
75 }
76 }
77
78 if ( !is_array( $pendingDbInfo ) || !$pendingDbInfo['pendingDBs'] ) {
79 return; // no DBs with jobs or cache is both empty and locked
80 }
81
82 $pendingDBs = $pendingDbInfo['pendingDBs']; // convenience
83 do {
84 $again = false;
85
86 if ( $types === false ) {
87 $candidates = call_user_func_array( 'array_merge', $pendingDBs );
88 } else {
89 $candidates = array();
90 $possTypes = array_intersect( $types, array_keys( $pendingDBs ) );
91 if ( $possTypes ) {
92 $possTypes = array_values( $possTypes );
93 $type = $possTypes[ mt_rand( 0, count( $possTypes ) - 1 ) ];
94 $candidates = $pendingDBs[$type];
95 }
96 }
97 if ( !$candidates ) {
98 return; // no jobs for this type
99 }
100
101 $candidates = array_values( $candidates );
102 $db = $candidates[ mt_rand( 0, count( $candidates ) - 1 ) ];
103 if ( !$this->checkJob( $type, $db ) ) {
104 if ( $type === false ) {
105 // There are no jobs available in the current database
106 foreach ( $pendingDBs as $type2 => $dbs ) {
107 $pendingDBs[$type2] = array_diff( $pendingDBs[$type2], array( $db ) );
108 }
109 } else {
110 // There are no jobs of this type available in the current database
111 $pendingDBs[$type] = array_diff( $pendingDBs[$type], array( $db ) );
112 }
113 // Update the cache to remove the outdated information.
114 // Make sure that this does not race (especially with full rebuilds).
115 $pendingDbInfo['pendingDBs'] = $pendingDBs;
116 if ( $wgMemc->add( "$memcKey:lock", 1, 60 ) ) { // lock
117 $curInfo = $wgMemc->get( $memcKey );
118 if ( $curInfo && $curInfo['timestamp'] === $pendingDbInfo['timestamp'] ) {
119 $wgMemc->set( $memcKey, $pendingDbInfo );
120 }
121 $wgMemc->delete( "$memcKey:lock" ); // unlock
122 }
123 $again = true;
124 }
125 } while ( $again );
126
127 if ( $this->hasOption( 'types' ) ) {
128 $this->output( $db . " " . $type . "\n" );
129 } else {
130 $this->output( $db . "\n" );
131 }
132 }
133
134 /**
135 * Check if the specified database has a job of the specified type in it.
136 * The type may be false to indicate "all".
137 * @param $type string
138 * @param $dbName string
139 * @return bool
140 */
141 function checkJob( $type, $dbName ) {
142 $group = JobQueueGroup::singleton( $dbName );
143 if ( $type === false ) {
144 foreach ( $group->getDefaultQueueTypes() as $type ) {
145 if ( !$group->get( $type )->isEmpty() ) {
146 return true;
147 }
148 }
149 return false;
150 } else {
151 return !$group->get( $type )->isEmpty();
152 }
153 }
154
155 /**
156 * Get all databases that have a pending job
157 * @return array
158 */
159 private function getPendingDbs() {
160 global $wgLocalDatabases;
161
162 $pendingDBs = array(); // (job type => (db list))
163 foreach ( $wgLocalDatabases as $db ) {
164 $types = JobQueueGroup::singleton( $db )->getQueuesWithJobs();
165 foreach ( $types as $type ) {
166 $pendingDBs[$type][] = $db;
167 }
168 }
169
170 return $pendingDBs;
171 }
172 }
173
174 $maintClass = "nextJobDb";
175 require_once( RUN_MAINTENANCE_IF_MAIN );