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