MediaWiki
REL1_23
|
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 true; 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 $ok = true; 00127 foreach ( $jobsByType as $type => $jobs ) { 00128 if ( $this->get( $type )->push( $jobs ) ) { 00129 JobQueueAggregator::singleton()->notifyQueueNonEmpty( $this->wiki, $type ); 00130 } else { 00131 $ok = false; 00132 } 00133 } 00134 00135 if ( $this->cache->has( 'queues-ready', 'list' ) ) { 00136 $list = $this->cache->get( 'queues-ready', 'list' ); 00137 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) { 00138 $this->cache->clear( 'queues-ready' ); 00139 } 00140 } 00141 00142 return $ok; 00143 } 00144 00156 public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $blacklist = array() ) { 00157 $job = false; 00158 00159 if ( is_string( $qtype ) ) { // specific job type 00160 if ( !in_array( $qtype, $blacklist ) ) { 00161 $job = $this->get( $qtype )->pop(); 00162 if ( !$job ) { 00163 JobQueueAggregator::singleton()->notifyQueueEmpty( $this->wiki, $qtype ); 00164 } 00165 } 00166 } else { // any job in the "default" jobs types 00167 if ( $flags & self::USE_CACHE ) { 00168 if ( !$this->cache->has( 'queues-ready', 'list', self::PROC_CACHE_TTL ) ) { 00169 $this->cache->set( 'queues-ready', 'list', $this->getQueuesWithJobs() ); 00170 } 00171 $types = $this->cache->get( 'queues-ready', 'list' ); 00172 } else { 00173 $types = $this->getQueuesWithJobs(); 00174 } 00175 00176 if ( $qtype == self::TYPE_DEFAULT ) { 00177 $types = array_intersect( $types, $this->getDefaultQueueTypes() ); 00178 } 00179 00180 $types = array_diff( $types, $blacklist ); // avoid selected types 00181 shuffle( $types ); // avoid starvation 00182 00183 foreach ( $types as $type ) { // for each queue... 00184 $job = $this->get( $type )->pop(); 00185 if ( $job ) { // found 00186 break; 00187 } else { // not found 00188 JobQueueAggregator::singleton()->notifyQueueEmpty( $this->wiki, $type ); 00189 $this->cache->clear( 'queues-ready' ); 00190 } 00191 } 00192 } 00193 00194 return $job; 00195 } 00196 00203 public function ack( Job $job ) { 00204 return $this->get( $job->getType() )->ack( $job ); 00205 } 00206 00214 public function deduplicateRootJob( Job $job ) { 00215 return $this->get( $job->getType() )->deduplicateRootJob( $job ); 00216 } 00217 00226 public function waitForBackups() { 00227 global $wgJobTypeConf; 00228 00229 wfProfileIn( __METHOD__ ); 00230 // Try to avoid doing this more than once per queue storage medium 00231 foreach ( $wgJobTypeConf as $type => $conf ) { 00232 $this->get( $type )->waitForBackups(); 00233 } 00234 wfProfileOut( __METHOD__ ); 00235 } 00236 00242 public function getQueueTypes() { 00243 return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) ); 00244 } 00245 00251 public function getDefaultQueueTypes() { 00252 global $wgJobTypesExcludedFromDefaultQueue; 00253 00254 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue ); 00255 } 00256 00264 public function queuesHaveJobs( $type = self::TYPE_ANY ) { 00265 global $wgMemc; 00266 00267 $key = wfMemcKey( 'jobqueue', 'queueshavejobs', $type ); 00268 00269 $value = $wgMemc->get( $key ); 00270 if ( $value === false ) { 00271 $queues = $this->getQueuesWithJobs(); 00272 if ( $type == self::TYPE_DEFAULT ) { 00273 $queues = array_intersect( $queues, $this->getDefaultQueueTypes() ); 00274 } 00275 $value = count( $queues ) ? 'true' : 'false'; 00276 $wgMemc->add( $key, $value, 15 ); 00277 } 00278 00279 return ( $value === 'true' ); 00280 } 00281 00287 public function getQueuesWithJobs() { 00288 $types = array(); 00289 foreach ( $this->getCoalescedQueues() as $info ) { 00290 $nonEmpty = $info['queue']->getSiblingQueuesWithJobs( $this->getQueueTypes() ); 00291 if ( is_array( $nonEmpty ) ) { // batching features supported 00292 $types = array_merge( $types, $nonEmpty ); 00293 } else { // we have to go through the queues in the bucket one-by-one 00294 foreach ( $info['types'] as $type ) { 00295 if ( !$this->get( $type )->isEmpty() ) { 00296 $types[] = $type; 00297 } 00298 } 00299 } 00300 } 00301 00302 return $types; 00303 } 00304 00310 public function getQueueSizes() { 00311 $sizeMap = array(); 00312 foreach ( $this->getCoalescedQueues() as $info ) { 00313 $sizes = $info['queue']->getSiblingQueueSizes( $this->getQueueTypes() ); 00314 if ( is_array( $sizes ) ) { // batching features supported 00315 $sizeMap = $sizeMap + $sizes; 00316 } else { // we have to go through the queues in the bucket one-by-one 00317 foreach ( $info['types'] as $type ) { 00318 $sizeMap[$type] = $this->get( $type )->getSize(); 00319 } 00320 } 00321 } 00322 00323 return $sizeMap; 00324 } 00325 00329 protected function getCoalescedQueues() { 00330 global $wgJobTypeConf; 00331 00332 if ( $this->coalescedQueues === null ) { 00333 $this->coalescedQueues = array(); 00334 foreach ( $wgJobTypeConf as $type => $conf ) { 00335 $queue = JobQueue::factory( 00336 array( 'wiki' => $this->wiki, 'type' => 'null' ) + $conf ); 00337 $loc = $queue->getCoalesceLocationInternal(); 00338 if ( !isset( $this->coalescedQueues[$loc] ) ) { 00339 $this->coalescedQueues[$loc]['queue'] = $queue; 00340 $this->coalescedQueues[$loc]['types'] = array(); 00341 } 00342 if ( $type === 'default' ) { 00343 $this->coalescedQueues[$loc]['types'] = array_merge( 00344 $this->coalescedQueues[$loc]['types'], 00345 array_diff( $this->getQueueTypes(), array_keys( $wgJobTypeConf ) ) 00346 ); 00347 } else { 00348 $this->coalescedQueues[$loc]['types'][] = $type; 00349 } 00350 } 00351 } 00352 00353 return $this->coalescedQueues; 00354 } 00355 00365 public function executeReadyPeriodicTasks() { 00366 global $wgMemc; 00367 00368 list( $db, $prefix ) = wfSplitWikiID( $this->wiki ); 00369 $key = wfForeignMemcKey( $db, $prefix, 'jobqueuegroup', 'taskruns', 'v1' ); 00370 $lastRuns = $wgMemc->get( $key ); // (queue => task => UNIX timestamp) 00371 00372 $count = 0; 00373 $tasksRun = array(); // (queue => task => UNIX timestamp) 00374 foreach ( $this->getQueueTypes() as $type ) { 00375 $queue = $this->get( $type ); 00376 foreach ( $queue->getPeriodicTasks() as $task => $definition ) { 00377 if ( $definition['period'] <= 0 ) { 00378 continue; // disabled 00379 } elseif ( !isset( $lastRuns[$type][$task] ) 00380 || $lastRuns[$type][$task] < ( time() - $definition['period'] ) 00381 ) { 00382 try { 00383 if ( call_user_func( $definition['callback'] ) !== null ) { 00384 $tasksRun[$type][$task] = time(); 00385 ++$count; 00386 } 00387 } catch ( JobQueueError $e ) { 00388 MWExceptionHandler::logException( $e ); 00389 } 00390 } 00391 } 00392 // The tasks may have recycled jobs or release delayed jobs into the queue 00393 if ( isset( $tasksRun[$type] ) && !$queue->isEmpty() ) { 00394 JobQueueAggregator::singleton()->notifyQueueNonEmpty( $this->wiki, $type ); 00395 } 00396 } 00397 00398 $wgMemc->merge( $key, function ( $cache, $key, $lastRuns ) use ( $tasksRun ) { 00399 if ( is_array( $lastRuns ) ) { 00400 foreach ( $tasksRun as $type => $tasks ) { 00401 foreach ( $tasks as $task => $timestamp ) { 00402 if ( !isset( $lastRuns[$type][$task] ) 00403 || $timestamp > $lastRuns[$type][$task] 00404 ) { 00405 $lastRuns[$type][$task] = $timestamp; 00406 } 00407 } 00408 } 00409 } else { 00410 $lastRuns = $tasksRun; 00411 } 00412 00413 return $lastRuns; 00414 } ); 00415 00416 return $count; 00417 } 00418 00423 private function getCachedConfigVar( $name ) { 00424 global $wgConf, $wgMemc; 00425 00426 if ( $this->wiki === wfWikiID() ) { 00427 return $GLOBALS[$name]; // common case 00428 } else { 00429 list( $db, $prefix ) = wfSplitWikiID( $this->wiki ); 00430 $key = wfForeignMemcKey( $db, $prefix, 'configvalue', $name ); 00431 $value = $wgMemc->get( $key ); // ('v' => ...) or false 00432 if ( is_array( $value ) ) { 00433 return $value['v']; 00434 } else { 00435 $value = $wgConf->getConfig( $this->wiki, $name ); 00436 $wgMemc->set( $key, array( 'v' => $value ), 86400 + mt_rand( 0, 86400 ) ); 00437 00438 return $value; 00439 } 00440 } 00441 } 00442 }