Merge "DatabaseMssql: Don't duplicate body of makeList()"
[lhc/web/wiklou.git] / includes / jobqueue / JobQueueGroup.php
1 <?php
2 /**
3 * Job queue base code.
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 * @author Aaron Schulz
22 */
23
24 /**
25 * Class to handle enqueueing of background jobs
26 *
27 * @ingroup JobQueue
28 * @since 1.21
29 */
30 class JobQueueGroup {
31 /** @var array */
32 protected static $instances = array();
33
34 /** @var ProcessCacheLRU */
35 protected $cache;
36
37 /** @var string Wiki ID */
38 protected $wiki;
39
40 /** @var array Map of (bucket => (queue => JobQueue, types => list of types) */
41 protected $coalescedQueues;
42
43 const TYPE_DEFAULT = 1; // integer; jobs popped by default
44 const TYPE_ANY = 2; // integer; any job
45
46 const USE_CACHE = 1; // integer; use process or persistent cache
47
48 const PROC_CACHE_TTL = 15; // integer; seconds
49
50 const CACHE_VERSION = 1; // integer; cache version
51
52 /**
53 * @param string $wiki Wiki ID
54 */
55 protected function __construct( $wiki ) {
56 $this->wiki = $wiki;
57 $this->cache = new ProcessCacheLRU( 10 );
58 }
59
60 /**
61 * @param bool|string $wiki Wiki ID
62 * @return JobQueueGroup
63 */
64 public static function singleton( $wiki = false ) {
65 $wiki = ( $wiki === false ) ? wfWikiID() : $wiki;
66 if ( !isset( self::$instances[$wiki] ) ) {
67 self::$instances[$wiki] = new self( $wiki );
68 }
69
70 return self::$instances[$wiki];
71 }
72
73 /**
74 * Destroy the singleton instances
75 *
76 * @return void
77 */
78 public static function destroySingletons() {
79 self::$instances = array();
80 }
81
82 /**
83 * Get the job queue object for a given queue type
84 *
85 * @param string $type
86 * @return JobQueue
87 */
88 public function get( $type ) {
89 global $wgJobTypeConf;
90
91 $conf = array( 'wiki' => $this->wiki, 'type' => $type );
92 if ( isset( $wgJobTypeConf[$type] ) ) {
93 $conf = $conf + $wgJobTypeConf[$type];
94 } else {
95 $conf = $conf + $wgJobTypeConf['default'];
96 }
97
98 return JobQueue::factory( $conf );
99 }
100
101 /**
102 * Insert jobs into the respective queues of with the belong.
103 *
104 * This inserts the jobs into the queue specified by $wgJobTypeConf
105 * and updates the aggregate job queue information cache as needed.
106 *
107 * @param Job|Job[] $jobs A single Job or a list of Jobs
108 * @throws MWException
109 * @return void
110 */
111 public function push( $jobs ) {
112 $jobs = is_array( $jobs ) ? $jobs : array( $jobs );
113 if ( !count( $jobs ) ) {
114 return;
115 }
116
117 $jobsByType = array(); // (job type => list of jobs)
118 foreach ( $jobs as $job ) {
119 if ( $job instanceof IJobSpecification ) {
120 $jobsByType[$job->getType()][] = $job;
121 } else {
122 throw new MWException( "Attempted to push a non-Job object into a queue." );
123 }
124 }
125
126 foreach ( $jobsByType as $type => $jobs ) {
127 $this->get( $type )->push( $jobs );
128 JobQueueAggregator::singleton()->notifyQueueNonEmpty( $this->wiki, $type );
129 }
130
131 if ( $this->cache->has( 'queues-ready', 'list' ) ) {
132 $list = $this->cache->get( 'queues-ready', 'list' );
133 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
134 $this->cache->clear( 'queues-ready' );
135 }
136 }
137 }
138
139 /**
140 * Pop a job off one of the job queues
141 *
142 * This pops a job off a queue as specified by $wgJobTypeConf and
143 * updates the aggregate job queue information cache as needed.
144 *
145 * @param int|string $qtype JobQueueGroup::TYPE_* constant or job type string
146 * @param int $flags Bitfield of JobQueueGroup::USE_* constants
147 * @param array $blacklist List of job types to ignore
148 * @return Job|bool Returns false on failure
149 */
150 public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $blacklist = array() ) {
151 $job = false;
152
153 if ( is_string( $qtype ) ) { // specific job type
154 if ( !in_array( $qtype, $blacklist ) ) {
155 $job = $this->get( $qtype )->pop();
156 if ( !$job ) {
157 JobQueueAggregator::singleton()->notifyQueueEmpty( $this->wiki, $qtype );
158 }
159 }
160 } else { // any job in the "default" jobs types
161 if ( $flags & self::USE_CACHE ) {
162 if ( !$this->cache->has( 'queues-ready', 'list', self::PROC_CACHE_TTL ) ) {
163 $this->cache->set( 'queues-ready', 'list', $this->getQueuesWithJobs() );
164 }
165 $types = $this->cache->get( 'queues-ready', 'list' );
166 } else {
167 $types = $this->getQueuesWithJobs();
168 }
169
170 if ( $qtype == self::TYPE_DEFAULT ) {
171 $types = array_intersect( $types, $this->getDefaultQueueTypes() );
172 }
173
174 $types = array_diff( $types, $blacklist ); // avoid selected types
175 shuffle( $types ); // avoid starvation
176
177 foreach ( $types as $type ) { // for each queue...
178 $job = $this->get( $type )->pop();
179 if ( $job ) { // found
180 break;
181 } else { // not found
182 JobQueueAggregator::singleton()->notifyQueueEmpty( $this->wiki, $type );
183 $this->cache->clear( 'queues-ready' );
184 }
185 }
186 }
187
188 return $job;
189 }
190
191 /**
192 * Acknowledge that a job was completed
193 *
194 * @param Job $job
195 * @return bool
196 */
197 public function ack( Job $job ) {
198 return $this->get( $job->getType() )->ack( $job );
199 }
200
201 /**
202 * Register the "root job" of a given job into the queue for de-duplication.
203 * This should only be called right *after* all the new jobs have been inserted.
204 *
205 * @param Job $job
206 * @return bool
207 */
208 public function deduplicateRootJob( Job $job ) {
209 return $this->get( $job->getType() )->deduplicateRootJob( $job );
210 }
211
212 /**
213 * Wait for any slaves or backup queue servers to catch up.
214 *
215 * This does nothing for certain queue classes.
216 *
217 * @return void
218 * @throws MWException
219 */
220 public function waitForBackups() {
221 global $wgJobTypeConf;
222
223 // Try to avoid doing this more than once per queue storage medium
224 foreach ( $wgJobTypeConf as $type => $conf ) {
225 $this->get( $type )->waitForBackups();
226 }
227 }
228
229 /**
230 * Get the list of queue types
231 *
232 * @return array List of strings
233 */
234 public function getQueueTypes() {
235 return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) );
236 }
237
238 /**
239 * Get the list of default queue types
240 *
241 * @return array List of strings
242 */
243 public function getDefaultQueueTypes() {
244 global $wgJobTypesExcludedFromDefaultQueue;
245
246 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
247 }
248
249 /**
250 * Check if there are any queues with jobs (this is cached)
251 *
252 * @param int $type JobQueueGroup::TYPE_* constant
253 * @return bool
254 * @since 1.23
255 */
256 public function queuesHaveJobs( $type = self::TYPE_ANY ) {
257 global $wgMemc;
258
259 $key = wfMemcKey( 'jobqueue', 'queueshavejobs', $type );
260
261 $value = $wgMemc->get( $key );
262 if ( $value === false ) {
263 $queues = $this->getQueuesWithJobs();
264 if ( $type == self::TYPE_DEFAULT ) {
265 $queues = array_intersect( $queues, $this->getDefaultQueueTypes() );
266 }
267 $value = count( $queues ) ? 'true' : 'false';
268 $wgMemc->add( $key, $value, 15 );
269 }
270
271 return ( $value === 'true' );
272 }
273
274 /**
275 * Get the list of job types that have non-empty queues
276 *
277 * @return array List of job types that have non-empty queues
278 */
279 public function getQueuesWithJobs() {
280 $types = array();
281 foreach ( $this->getCoalescedQueues() as $info ) {
282 $nonEmpty = $info['queue']->getSiblingQueuesWithJobs( $this->getQueueTypes() );
283 if ( is_array( $nonEmpty ) ) { // batching features supported
284 $types = array_merge( $types, $nonEmpty );
285 } else { // we have to go through the queues in the bucket one-by-one
286 foreach ( $info['types'] as $type ) {
287 if ( !$this->get( $type )->isEmpty() ) {
288 $types[] = $type;
289 }
290 }
291 }
292 }
293
294 return $types;
295 }
296
297 /**
298 * Get the size of the queus for a list of job types
299 *
300 * @return array Map of (job type => size)
301 */
302 public function getQueueSizes() {
303 $sizeMap = array();
304 foreach ( $this->getCoalescedQueues() as $info ) {
305 $sizes = $info['queue']->getSiblingQueueSizes( $this->getQueueTypes() );
306 if ( is_array( $sizes ) ) { // batching features supported
307 $sizeMap = $sizeMap + $sizes;
308 } else { // we have to go through the queues in the bucket one-by-one
309 foreach ( $info['types'] as $type ) {
310 $sizeMap[$type] = $this->get( $type )->getSize();
311 }
312 }
313 }
314
315 return $sizeMap;
316 }
317
318 /**
319 * @return array
320 */
321 protected function getCoalescedQueues() {
322 global $wgJobTypeConf;
323
324 if ( $this->coalescedQueues === null ) {
325 $this->coalescedQueues = array();
326 foreach ( $wgJobTypeConf as $type => $conf ) {
327 $queue = JobQueue::factory(
328 array( 'wiki' => $this->wiki, 'type' => 'null' ) + $conf );
329 $loc = $queue->getCoalesceLocationInternal();
330 if ( !isset( $this->coalescedQueues[$loc] ) ) {
331 $this->coalescedQueues[$loc]['queue'] = $queue;
332 $this->coalescedQueues[$loc]['types'] = array();
333 }
334 if ( $type === 'default' ) {
335 $this->coalescedQueues[$loc]['types'] = array_merge(
336 $this->coalescedQueues[$loc]['types'],
337 array_diff( $this->getQueueTypes(), array_keys( $wgJobTypeConf ) )
338 );
339 } else {
340 $this->coalescedQueues[$loc]['types'][] = $type;
341 }
342 }
343 }
344
345 return $this->coalescedQueues;
346 }
347
348 /**
349 * Execute any due periodic queue maintenance tasks for all queues.
350 *
351 * A task is "due" if the time ellapsed since the last run is greater than
352 * the defined run period. Concurrent calls to this function will cause tasks
353 * to be attempted twice, so they may need their own methods of mutual exclusion.
354 *
355 * @return int Number of tasks run
356 */
357 public function executeReadyPeriodicTasks() {
358 global $wgMemc;
359
360 list( $db, $prefix ) = wfSplitWikiID( $this->wiki );
361 $key = wfForeignMemcKey( $db, $prefix, 'jobqueuegroup', 'taskruns', 'v1' );
362 $lastRuns = $wgMemc->get( $key ); // (queue => task => UNIX timestamp)
363
364 $count = 0;
365 $tasksRun = array(); // (queue => task => UNIX timestamp)
366 foreach ( $this->getQueueTypes() as $type ) {
367 $queue = $this->get( $type );
368 foreach ( $queue->getPeriodicTasks() as $task => $definition ) {
369 if ( $definition['period'] <= 0 ) {
370 continue; // disabled
371 } elseif ( !isset( $lastRuns[$type][$task] )
372 || $lastRuns[$type][$task] < ( time() - $definition['period'] )
373 ) {
374 try {
375 if ( call_user_func( $definition['callback'] ) !== null ) {
376 $tasksRun[$type][$task] = time();
377 ++$count;
378 }
379 } catch ( JobQueueError $e ) {
380 MWExceptionHandler::logException( $e );
381 }
382 }
383 }
384 // The tasks may have recycled jobs or release delayed jobs into the queue
385 if ( isset( $tasksRun[$type] ) && !$queue->isEmpty() ) {
386 JobQueueAggregator::singleton()->notifyQueueNonEmpty( $this->wiki, $type );
387 }
388 }
389
390 if ( $count === 0 ) {
391 return $count; // nothing to update
392 }
393
394 $wgMemc->merge( $key, function ( $cache, $key, $lastRuns ) use ( $tasksRun ) {
395 if ( is_array( $lastRuns ) ) {
396 foreach ( $tasksRun as $type => $tasks ) {
397 foreach ( $tasks as $task => $timestamp ) {
398 if ( !isset( $lastRuns[$type][$task] )
399 || $timestamp > $lastRuns[$type][$task]
400 ) {
401 $lastRuns[$type][$task] = $timestamp;
402 }
403 }
404 }
405 } else {
406 $lastRuns = $tasksRun;
407 }
408
409 return $lastRuns;
410 } );
411
412 return $count;
413 }
414
415 /**
416 * @param string $name
417 * @return mixed
418 */
419 private function getCachedConfigVar( $name ) {
420 global $wgConf, $wgMemc;
421
422 if ( $this->wiki === wfWikiID() ) {
423 return $GLOBALS[$name]; // common case
424 } else {
425 list( $db, $prefix ) = wfSplitWikiID( $this->wiki );
426 $key = wfForeignMemcKey( $db, $prefix, 'configvalue', $name );
427 $value = $wgMemc->get( $key ); // ('v' => ...) or false
428 if ( is_array( $value ) ) {
429 return $value['v'];
430 } else {
431 $value = $wgConf->getConfig( $this->wiki, $name );
432 $wgMemc->set( $key, array( 'v' => $value ), 86400 + mt_rand( 0, 86400 ) );
433
434 return $value;
435 }
436 }
437 }
438 }