Merge "Remove Preprocessor_HipHop"
[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 = JobQueueGroup::singleton()->getDefaultQueueTypes();
49 }
50
51 // Handle any required periodic queue maintenance
52 $this->executeReadyPeriodicTasks();
53
54 $memcKey = 'jobqueue:dbs:v3';
55 $pendingDbInfo = $wgMemc->get( $memcKey );
56
57 // If the cache entry wasn't present, is stale, or in .1% of cases otherwise,
58 // regenerate the cache. Use any available stale cache if another process is
59 // currently regenerating the pending DB information.
60 if ( !is_array( $pendingDbInfo )
61 || ( time() - $pendingDbInfo['timestamp'] ) > 300 // 5 minutes
62 || mt_rand( 0, 999 ) == 0
63 ) {
64 if ( $wgMemc->add( "$memcKey:rebuild", 1, 1800 ) ) { // lock
65 $pendingDbInfo = array(
66 'pendingDBs' => $this->getPendingDbs(),
67 'timestamp' => time()
68 );
69 for ( $attempts=1; $attempts <= 25; ++$attempts ) {
70 if ( $wgMemc->add( "$memcKey:lock", 1, 60 ) ) { // lock
71 $wgMemc->set( $memcKey, $pendingDbInfo );
72 $wgMemc->delete( "$memcKey:lock" ); // unlock
73 break;
74 }
75 }
76 $wgMemc->delete( "$memcKey:rebuild" ); // unlock
77 }
78 }
79
80 if ( !is_array( $pendingDbInfo ) || !$pendingDbInfo['pendingDBs'] ) {
81 return; // no DBs with jobs or cache is both empty and locked
82 }
83
84 $type = $this->getOption( 'type', false );
85 $pendingDBs = $pendingDbInfo['pendingDBs']; // convenience
86 do {
87 $again = false;
88
89 $candidates = array(); // list of (type, db)
90 // Flatten the tree of candidates into a flat list so that a random
91 // item can be selected, weighing each queue (type/db tuple) equally.
92 foreach ( $pendingDBs as $type => $dbs ) {
93 if ( in_array( $type, $types ) ) {
94 foreach ( $dbs as $db ) {
95 $candidates[] = array( $type, $db );
96 }
97 }
98 }
99 if ( !count( $candidates ) ) {
100 return; // no jobs for this type
101 }
102
103 list( $type, $db ) = $candidates[ mt_rand( 0, count( $candidates ) - 1 ) ];
104 if ( !$this->checkJob( $type, $db ) ) { // queue is actually empty?
105 $pendingDBs = $this->delistDB( $pendingDBs, $db, $type );
106 // Update the cache to remove the outdated information.
107 // Make sure that this does not race (especially with full rebuilds).
108 if ( $wgMemc->add( "$memcKey:lock", 1, 60 ) ) { // lock
109 $curInfo = $wgMemc->get( $memcKey );
110 if ( is_array( $curInfo ) ) {
111 $curInfo['pendingDBs'] =
112 $this->delistDB( $curInfo['pendingDBs'], $db, $type );
113 $wgMemc->set( $memcKey, $curInfo );
114 // May as well make use of this newer information
115 $pendingDBs = $curInfo['pendingDBs'];
116 }
117 $wgMemc->delete( "$memcKey:lock" ); // unlock
118 }
119 $again = true;
120 }
121 } while ( $again );
122
123 if ( $this->hasOption( 'types' ) ) {
124 $this->output( $db . " " . $type . "\n" );
125 } else {
126 $this->output( $db . "\n" );
127 }
128 }
129
130 /**
131 * Remove a type/DB entry from the list of queues with jobs
132 *
133 * @param $pendingDBs array
134 * @param $db string
135 * @param $type string
136 * @return Array
137 */
138 private function delistDB( array $pendingDBs, $db, $type ) {
139 $pendingDBs[$type] = array_diff( $pendingDBs[$type], array( $db ) );
140 return $pendingDBs;
141 }
142
143 /**
144 * Check if the specified database has a job of the specified type in it.
145 * The type may be false to indicate "all".
146 * @param $type string
147 * @param $dbName string
148 * @return bool
149 */
150 private function checkJob( $type, $dbName ) {
151 return !JobQueueGroup::singleton( $dbName )->get( $type )->isEmpty();
152 }
153
154 /**
155 * Get all databases that have a pending job
156 * @return array
157 */
158 private function getPendingDbs() {
159 global $wgLocalDatabases;
160
161 $pendingDBs = array(); // (job type => (db list))
162 foreach ( $wgLocalDatabases as $db ) {
163 foreach ( JobQueueGroup::singleton( $db )->getQueuesWithJobs() as $type ) {
164 $pendingDBs[$type][] = $db;
165 }
166 }
167
168 return $pendingDBs;
169 }
170
171 /**
172 * Do all ready periodic jobs for all databases every 5 minutes (and .1% of the time)
173 * @return integer
174 */
175 private function executeReadyPeriodicTasks() {
176 global $wgLocalDatabases, $wgMemc;
177
178 $count = 0;
179 $memcKey = 'jobqueue:periodic:lasttime';
180 $timestamp = (int)$wgMemc->get( $memcKey ); // UNIX timestamp or 0
181 if ( ( time() - $timestamp ) > 300 || mt_rand( 0, 999 ) == 0 ) { // 5 minutes
182 if ( $wgMemc->add( "$memcKey:rebuild", 1, 1800 ) ) { // lock
183 foreach ( $wgLocalDatabases as $db ) {
184 $count += JobQueueGroup::singleton( $db )->executeReadyPeriodicTasks();
185 }
186 $wgMemc->set( $memcKey, time() );
187 $wgMemc->delete( "$memcKey:rebuild" ); // unlock
188 }
189 }
190
191 return $count;
192 }
193 }
194
195 $maintClass = "nextJobDb";
196 require_once( RUN_MAINTENANCE_IF_MAIN );