Removed executeReadyPeriodicTasks() method
[mediawiki.git] / includes / jobqueue / JobQueueGroup.php
blobfdf7b876cc363efef4cc6355f0c332a4ba51f24e
1 <?php
2 /**
3 * Job queue base code.
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.
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.
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
20 * @file
21 * @author Aaron Schulz
24 /**
25 * Class to handle enqueueing of background jobs
27 * @ingroup JobQueue
28 * @since 1.21
30 class JobQueueGroup {
31 /** @var array */
32 protected static $instances = array();
34 /** @var ProcessCacheLRU */
35 protected $cache;
37 /** @var string Wiki ID */
38 protected $wiki;
40 /** @var array Map of (bucket => (queue => JobQueue, types => list of types) */
41 protected $coalescedQueues;
43 const TYPE_DEFAULT = 1; // integer; jobs popped by default
44 const TYPE_ANY = 2; // integer; any job
46 const USE_CACHE = 1; // integer; use process or persistent cache
48 const PROC_CACHE_TTL = 15; // integer; seconds
50 const CACHE_VERSION = 1; // integer; cache version
52 /**
53 * @param string $wiki Wiki ID
55 protected function __construct( $wiki ) {
56 $this->wiki = $wiki;
57 $this->cache = new ProcessCacheLRU( 10 );
60 /**
61 * @param bool|string $wiki Wiki ID
62 * @return JobQueueGroup
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 );
70 return self::$instances[$wiki];
73 /**
74 * Destroy the singleton instances
76 * @return void
78 public static function destroySingletons() {
79 self::$instances = array();
82 /**
83 * Get the job queue object for a given queue type
85 * @param string $type
86 * @return JobQueue
88 public function get( $type ) {
89 global $wgJobTypeConf;
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'];
97 $conf['aggregator'] = JobQueueAggregator::singleton();
99 return JobQueue::factory( $conf );
103 * Insert jobs into the respective queues of with the belong.
105 * This inserts the jobs into the queue specified by $wgJobTypeConf
106 * and updates the aggregate job queue information cache as needed.
108 * @param Job|Job[] $jobs A single Job or a list of Jobs
109 * @throws MWException
110 * @return void
112 public function push( $jobs ) {
113 $jobs = is_array( $jobs ) ? $jobs : array( $jobs );
114 if ( !count( $jobs ) ) {
115 return;
118 $jobsByType = array(); // (job type => list of jobs)
119 foreach ( $jobs as $job ) {
120 if ( $job instanceof IJobSpecification ) {
121 $jobsByType[$job->getType()][] = $job;
122 } else {
123 throw new MWException( "Attempted to push a non-Job object into a queue." );
127 foreach ( $jobsByType as $type => $jobs ) {
128 $this->get( $type )->push( $jobs );
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' );
140 * Pop a job off one of the job queues
142 * This pops a job off a queue as specified by $wgJobTypeConf and
143 * updates the aggregate job queue information cache as needed.
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
150 public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $blacklist = array() ) {
151 $job = false;
153 if ( is_string( $qtype ) ) { // specific job type
154 if ( !in_array( $qtype, $blacklist ) ) {
155 $job = $this->get( $qtype )->pop();
157 } else { // any job in the "default" jobs types
158 if ( $flags & self::USE_CACHE ) {
159 if ( !$this->cache->has( 'queues-ready', 'list', self::PROC_CACHE_TTL ) ) {
160 $this->cache->set( 'queues-ready', 'list', $this->getQueuesWithJobs() );
162 $types = $this->cache->get( 'queues-ready', 'list' );
163 } else {
164 $types = $this->getQueuesWithJobs();
167 if ( $qtype == self::TYPE_DEFAULT ) {
168 $types = array_intersect( $types, $this->getDefaultQueueTypes() );
171 $types = array_diff( $types, $blacklist ); // avoid selected types
172 shuffle( $types ); // avoid starvation
174 foreach ( $types as $type ) { // for each queue...
175 $job = $this->get( $type )->pop();
176 if ( $job ) { // found
177 break;
178 } else { // not found
179 $this->cache->clear( 'queues-ready' );
184 return $job;
188 * Acknowledge that a job was completed
190 * @param Job $job
191 * @return bool
193 public function ack( Job $job ) {
194 return $this->get( $job->getType() )->ack( $job );
198 * Register the "root job" of a given job into the queue for de-duplication.
199 * This should only be called right *after* all the new jobs have been inserted.
201 * @param Job $job
202 * @return bool
204 public function deduplicateRootJob( Job $job ) {
205 return $this->get( $job->getType() )->deduplicateRootJob( $job );
209 * Wait for any slaves or backup queue servers to catch up.
211 * This does nothing for certain queue classes.
213 * @return void
214 * @throws MWException
216 public function waitForBackups() {
217 global $wgJobTypeConf;
219 // Try to avoid doing this more than once per queue storage medium
220 foreach ( $wgJobTypeConf as $type => $conf ) {
221 $this->get( $type )->waitForBackups();
226 * Get the list of queue types
228 * @return array List of strings
230 public function getQueueTypes() {
231 return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) );
235 * Get the list of default queue types
237 * @return array List of strings
239 public function getDefaultQueueTypes() {
240 global $wgJobTypesExcludedFromDefaultQueue;
242 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
246 * Check if there are any queues with jobs (this is cached)
248 * @param int $type JobQueueGroup::TYPE_* constant
249 * @return bool
250 * @since 1.23
252 public function queuesHaveJobs( $type = self::TYPE_ANY ) {
253 global $wgMemc;
255 $key = wfMemcKey( 'jobqueue', 'queueshavejobs', $type );
257 $value = $wgMemc->get( $key );
258 if ( $value === false ) {
259 $queues = $this->getQueuesWithJobs();
260 if ( $type == self::TYPE_DEFAULT ) {
261 $queues = array_intersect( $queues, $this->getDefaultQueueTypes() );
263 $value = count( $queues ) ? 'true' : 'false';
264 $wgMemc->add( $key, $value, 15 );
267 return ( $value === 'true' );
271 * Get the list of job types that have non-empty queues
273 * @return array List of job types that have non-empty queues
275 public function getQueuesWithJobs() {
276 $types = array();
277 foreach ( $this->getCoalescedQueues() as $info ) {
278 $nonEmpty = $info['queue']->getSiblingQueuesWithJobs( $this->getQueueTypes() );
279 if ( is_array( $nonEmpty ) ) { // batching features supported
280 $types = array_merge( $types, $nonEmpty );
281 } else { // we have to go through the queues in the bucket one-by-one
282 foreach ( $info['types'] as $type ) {
283 if ( !$this->get( $type )->isEmpty() ) {
284 $types[] = $type;
290 return $types;
294 * Get the size of the queus for a list of job types
296 * @return array Map of (job type => size)
298 public function getQueueSizes() {
299 $sizeMap = array();
300 foreach ( $this->getCoalescedQueues() as $info ) {
301 $sizes = $info['queue']->getSiblingQueueSizes( $this->getQueueTypes() );
302 if ( is_array( $sizes ) ) { // batching features supported
303 $sizeMap = $sizeMap + $sizes;
304 } else { // we have to go through the queues in the bucket one-by-one
305 foreach ( $info['types'] as $type ) {
306 $sizeMap[$type] = $this->get( $type )->getSize();
311 return $sizeMap;
315 * @return array
317 protected function getCoalescedQueues() {
318 global $wgJobTypeConf;
320 if ( $this->coalescedQueues === null ) {
321 $this->coalescedQueues = array();
322 foreach ( $wgJobTypeConf as $type => $conf ) {
323 $queue = JobQueue::factory(
324 array( 'wiki' => $this->wiki, 'type' => 'null' ) + $conf );
325 $loc = $queue->getCoalesceLocationInternal();
326 if ( !isset( $this->coalescedQueues[$loc] ) ) {
327 $this->coalescedQueues[$loc]['queue'] = $queue;
328 $this->coalescedQueues[$loc]['types'] = array();
330 if ( $type === 'default' ) {
331 $this->coalescedQueues[$loc]['types'] = array_merge(
332 $this->coalescedQueues[$loc]['types'],
333 array_diff( $this->getQueueTypes(), array_keys( $wgJobTypeConf ) )
335 } else {
336 $this->coalescedQueues[$loc]['types'][] = $type;
341 return $this->coalescedQueues;
345 * @param string $name
346 * @return mixed
348 private function getCachedConfigVar( $name ) {
349 global $wgConf, $wgMemc;
351 if ( $this->wiki === wfWikiID() ) {
352 return $GLOBALS[$name]; // common case
353 } else {
354 list( $db, $prefix ) = wfSplitWikiID( $this->wiki );
355 $key = wfForeignMemcKey( $db, $prefix, 'configvalue', $name );
356 $value = $wgMemc->get( $key ); // ('v' => ...) or false
357 if ( is_array( $value ) ) {
358 return $value['v'];
359 } else {
360 $value = $wgConf->getConfig( $this->wiki, $name );
361 $wgMemc->set( $key, array( 'v' => $value ), 86400 + mt_rand( 0, 86400 ) );
363 return $value;