MediaWiki
REL1_24
|
00001 <?php 00030 class JobQueueGroup { 00032 protected static $instances = array(); 00033 00035 protected $cache; 00036 00038 protected $wiki; 00039 00041 protected $coalescedQueues; 00042 00043 const TYPE_DEFAULT = 1; // integer; jobs popped by default 00044 const TYPE_ANY = 2; // integer; any job 00045 00046 const USE_CACHE = 1; // integer; use process or persistent cache 00047 00048 const PROC_CACHE_TTL = 15; // integer; seconds 00049 00050 const CACHE_VERSION = 1; // integer; cache version 00051 00055 protected function __construct( $wiki ) { 00056 $this->wiki = $wiki; 00057 $this->cache = new ProcessCacheLRU( 10 ); 00058 } 00059 00064 public static function singleton( $wiki = false ) { 00065 $wiki = ( $wiki === false ) ? wfWikiID() : $wiki; 00066 if ( !isset( self::$instances[$wiki] ) ) { 00067 self::$instances[$wiki] = new self( $wiki ); 00068 } 00069 00070 return self::$instances[$wiki]; 00071 } 00072 00078 public static function destroySingletons() { 00079 self::$instances = array(); 00080 } 00081 00088 public function get( $type ) { 00089 global $wgJobTypeConf; 00090 00091 $conf = array( 'wiki' => $this->wiki, 'type' => $type ); 00092 if ( isset( $wgJobTypeConf[$type] ) ) { 00093 $conf = $conf + $wgJobTypeConf[$type]; 00094 } else { 00095 $conf = $conf + $wgJobTypeConf['default']; 00096 } 00097 00098 return JobQueue::factory( $conf ); 00099 } 00100 00111 public function push( $jobs ) { 00112 $jobs = is_array( $jobs ) ? $jobs : array( $jobs ); 00113 if ( !count( $jobs ) ) { 00114 return; 00115 } 00116 00117 $jobsByType = array(); // (job type => list of jobs) 00118 foreach ( $jobs as $job ) { 00119 if ( $job instanceof IJobSpecification ) { 00120 $jobsByType[$job->getType()][] = $job; 00121 } else { 00122 throw new MWException( "Attempted to push a non-Job object into a queue." ); 00123 } 00124 } 00125 00126 foreach ( $jobsByType as $type => $jobs ) { 00127 $this->get( $type )->push( $jobs ); 00128 JobQueueAggregator::singleton()->notifyQueueNonEmpty( $this->wiki, $type ); 00129 } 00130 00131 if ( $this->cache->has( 'queues-ready', 'list' ) ) { 00132 $list = $this->cache->get( 'queues-ready', 'list' ); 00133 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) { 00134 $this->cache->clear( 'queues-ready' ); 00135 } 00136 } 00137 } 00138 00150 public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $blacklist = array() ) { 00151 $job = false; 00152 00153 if ( is_string( $qtype ) ) { // specific job type 00154 if ( !in_array( $qtype, $blacklist ) ) { 00155 $job = $this->get( $qtype )->pop(); 00156 if ( !$job ) { 00157 JobQueueAggregator::singleton()->notifyQueueEmpty( $this->wiki, $qtype ); 00158 } 00159 } 00160 } else { // any job in the "default" jobs types 00161 if ( $flags & self::USE_CACHE ) { 00162 if ( !$this->cache->has( 'queues-ready', 'list', self::PROC_CACHE_TTL ) ) { 00163 $this->cache->set( 'queues-ready', 'list', $this->getQueuesWithJobs() ); 00164 } 00165 $types = $this->cache->get( 'queues-ready', 'list' ); 00166 } else { 00167 $types = $this->getQueuesWithJobs(); 00168 } 00169 00170 if ( $qtype == self::TYPE_DEFAULT ) { 00171 $types = array_intersect( $types, $this->getDefaultQueueTypes() ); 00172 } 00173 00174 $types = array_diff( $types, $blacklist ); // avoid selected types 00175 shuffle( $types ); // avoid starvation 00176 00177 foreach ( $types as $type ) { // for each queue... 00178 $job = $this->get( $type )->pop(); 00179 if ( $job ) { // found 00180 break; 00181 } else { // not found 00182 JobQueueAggregator::singleton()->notifyQueueEmpty( $this->wiki, $type ); 00183 $this->cache->clear( 'queues-ready' ); 00184 } 00185 } 00186 } 00187 00188 return $job; 00189 } 00190 00197 public function ack( Job $job ) { 00198 return $this->get( $job->getType() )->ack( $job ); 00199 } 00200 00208 public function deduplicateRootJob( Job $job ) { 00209 return $this->get( $job->getType() )->deduplicateRootJob( $job ); 00210 } 00211 00220 public function waitForBackups() { 00221 global $wgJobTypeConf; 00222 00223 wfProfileIn( __METHOD__ ); 00224 // Try to avoid doing this more than once per queue storage medium 00225 foreach ( $wgJobTypeConf as $type => $conf ) { 00226 $this->get( $type )->waitForBackups(); 00227 } 00228 wfProfileOut( __METHOD__ ); 00229 } 00230 00236 public function getQueueTypes() { 00237 return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) ); 00238 } 00239 00245 public function getDefaultQueueTypes() { 00246 global $wgJobTypesExcludedFromDefaultQueue; 00247 00248 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue ); 00249 } 00250 00258 public function queuesHaveJobs( $type = self::TYPE_ANY ) { 00259 global $wgMemc; 00260 00261 $key = wfMemcKey( 'jobqueue', 'queueshavejobs', $type ); 00262 00263 $value = $wgMemc->get( $key ); 00264 if ( $value === false ) { 00265 $queues = $this->getQueuesWithJobs(); 00266 if ( $type == self::TYPE_DEFAULT ) { 00267 $queues = array_intersect( $queues, $this->getDefaultQueueTypes() ); 00268 } 00269 $value = count( $queues ) ? 'true' : 'false'; 00270 $wgMemc->add( $key, $value, 15 ); 00271 } 00272 00273 return ( $value === 'true' ); 00274 } 00275 00281 public function getQueuesWithJobs() { 00282 $types = array(); 00283 foreach ( $this->getCoalescedQueues() as $info ) { 00284 $nonEmpty = $info['queue']->getSiblingQueuesWithJobs( $this->getQueueTypes() ); 00285 if ( is_array( $nonEmpty ) ) { // batching features supported 00286 $types = array_merge( $types, $nonEmpty ); 00287 } else { // we have to go through the queues in the bucket one-by-one 00288 foreach ( $info['types'] as $type ) { 00289 if ( !$this->get( $type )->isEmpty() ) { 00290 $types[] = $type; 00291 } 00292 } 00293 } 00294 } 00295 00296 return $types; 00297 } 00298 00304 public function getQueueSizes() { 00305 $sizeMap = array(); 00306 foreach ( $this->getCoalescedQueues() as $info ) { 00307 $sizes = $info['queue']->getSiblingQueueSizes( $this->getQueueTypes() ); 00308 if ( is_array( $sizes ) ) { // batching features supported 00309 $sizeMap = $sizeMap + $sizes; 00310 } else { // we have to go through the queues in the bucket one-by-one 00311 foreach ( $info['types'] as $type ) { 00312 $sizeMap[$type] = $this->get( $type )->getSize(); 00313 } 00314 } 00315 } 00316 00317 return $sizeMap; 00318 } 00319 00323 protected function getCoalescedQueues() { 00324 global $wgJobTypeConf; 00325 00326 if ( $this->coalescedQueues === null ) { 00327 $this->coalescedQueues = array(); 00328 foreach ( $wgJobTypeConf as $type => $conf ) { 00329 $queue = JobQueue::factory( 00330 array( 'wiki' => $this->wiki, 'type' => 'null' ) + $conf ); 00331 $loc = $queue->getCoalesceLocationInternal(); 00332 if ( !isset( $this->coalescedQueues[$loc] ) ) { 00333 $this->coalescedQueues[$loc]['queue'] = $queue; 00334 $this->coalescedQueues[$loc]['types'] = array(); 00335 } 00336 if ( $type === 'default' ) { 00337 $this->coalescedQueues[$loc]['types'] = array_merge( 00338 $this->coalescedQueues[$loc]['types'], 00339 array_diff( $this->getQueueTypes(), array_keys( $wgJobTypeConf ) ) 00340 ); 00341 } else { 00342 $this->coalescedQueues[$loc]['types'][] = $type; 00343 } 00344 } 00345 } 00346 00347 return $this->coalescedQueues; 00348 } 00349 00359 public function executeReadyPeriodicTasks() { 00360 global $wgMemc; 00361 00362 list( $db, $prefix ) = wfSplitWikiID( $this->wiki ); 00363 $key = wfForeignMemcKey( $db, $prefix, 'jobqueuegroup', 'taskruns', 'v1' ); 00364 $lastRuns = $wgMemc->get( $key ); // (queue => task => UNIX timestamp) 00365 00366 $count = 0; 00367 $tasksRun = array(); // (queue => task => UNIX timestamp) 00368 foreach ( $this->getQueueTypes() as $type ) { 00369 $queue = $this->get( $type ); 00370 foreach ( $queue->getPeriodicTasks() as $task => $definition ) { 00371 if ( $definition['period'] <= 0 ) { 00372 continue; // disabled 00373 } elseif ( !isset( $lastRuns[$type][$task] ) 00374 || $lastRuns[$type][$task] < ( time() - $definition['period'] ) 00375 ) { 00376 try { 00377 if ( call_user_func( $definition['callback'] ) !== null ) { 00378 $tasksRun[$type][$task] = time(); 00379 ++$count; 00380 } 00381 } catch ( JobQueueError $e ) { 00382 MWExceptionHandler::logException( $e ); 00383 } 00384 } 00385 } 00386 // The tasks may have recycled jobs or release delayed jobs into the queue 00387 if ( isset( $tasksRun[$type] ) && !$queue->isEmpty() ) { 00388 JobQueueAggregator::singleton()->notifyQueueNonEmpty( $this->wiki, $type ); 00389 } 00390 } 00391 00392 if ( $count === 0 ) { 00393 return $count; // nothing to update 00394 } 00395 00396 $wgMemc->merge( $key, function ( $cache, $key, $lastRuns ) use ( $tasksRun ) { 00397 if ( is_array( $lastRuns ) ) { 00398 foreach ( $tasksRun as $type => $tasks ) { 00399 foreach ( $tasks as $task => $timestamp ) { 00400 if ( !isset( $lastRuns[$type][$task] ) 00401 || $timestamp > $lastRuns[$type][$task] 00402 ) { 00403 $lastRuns[$type][$task] = $timestamp; 00404 } 00405 } 00406 } 00407 } else { 00408 $lastRuns = $tasksRun; 00409 } 00410 00411 return $lastRuns; 00412 } ); 00413 00414 return $count; 00415 } 00416 00421 private function getCachedConfigVar( $name ) { 00422 global $wgConf, $wgMemc; 00423 00424 if ( $this->wiki === wfWikiID() ) { 00425 return $GLOBALS[$name]; // common case 00426 } else { 00427 list( $db, $prefix ) = wfSplitWikiID( $this->wiki ); 00428 $key = wfForeignMemcKey( $db, $prefix, 'configvalue', $name ); 00429 $value = $wgMemc->get( $key ); // ('v' => ...) or false 00430 if ( is_array( $value ) ) { 00431 return $value['v']; 00432 } else { 00433 $value = $wgConf->getConfig( $this->wiki, $name ); 00434 $wgMemc->set( $key, array( 'v' => $value ), 86400 + mt_rand( 0, 86400 ) ); 00435 00436 return $value; 00437 } 00438 } 00439 } 00440 }