MediaWiki  1.32.5
JobQueueGroup.php
Go to the documentation of this file.
1 <?php
31  protected static $instances = [];
32 
34  protected $cache;
35 
37  protected $domain;
39  protected $readOnlyReason;
41  protected $invalidWiki = false;
42 
44  protected $coalescedQueues;
45 
46  const TYPE_DEFAULT = 1; // integer; jobs popped by default
47  const TYPE_ANY = 2; // integer; any job
48 
49  const USE_CACHE = 1; // integer; use process or persistent cache
50 
51  const PROC_CACHE_TTL = 15; // integer; seconds
52 
53  const CACHE_VERSION = 1; // integer; cache version
54 
59  protected function __construct( $domain, $readOnlyReason ) {
60  $this->domain = $domain;
61  $this->readOnlyReason = $readOnlyReason;
62  $this->cache = new MapCacheLRU( 10 );
63  }
64 
69  public static function singleton( $domain = false ) {
70  global $wgLocalDatabases;
71 
72  if ( $domain === false ) {
74  }
75 
76  if ( !isset( self::$instances[$domain] ) ) {
77  self::$instances[$domain] = new self( $domain, wfConfiguredReadOnlyReason() );
78  // Make sure jobs are not getting pushed to bogus wikis. This can confuse
79  // the job runner system into spawning endless RPC requests that fail (T171371).
81  if (
83  !in_array( $wikiId, $wgLocalDatabases )
84  ) {
85  self::$instances[$domain]->invalidWiki = true;
86  }
87  }
88 
89  return self::$instances[$domain];
90  }
91 
97  public static function destroySingletons() {
98  self::$instances = [];
99  }
100 
107  public function get( $type ) {
108  global $wgJobTypeConf;
109 
110  $conf = [ 'wiki' => $this->domain, 'type' => $type ];
111  if ( isset( $wgJobTypeConf[$type] ) ) {
112  $conf = $conf + $wgJobTypeConf[$type];
113  } else {
114  $conf = $conf + $wgJobTypeConf['default'];
115  }
116  $conf['aggregator'] = JobQueueAggregator::singleton();
117  if ( !isset( $conf['readOnlyReason'] ) ) {
118  $conf['readOnlyReason'] = $this->readOnlyReason;
119  }
120 
121  return JobQueue::factory( $conf );
122  }
123 
134  public function push( $jobs ) {
136 
137  if ( $this->invalidWiki ) {
138  // Do not enqueue job that cannot be run (T171371)
139  $e = new LogicException( "Domain '{$this->domain}' is not recognized." );
141  return;
142  }
143 
144  $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
145  if ( !count( $jobs ) ) {
146  return;
147  }
148 
149  $this->assertValidJobs( $jobs );
150 
151  $jobsByType = []; // (job type => list of jobs)
152  foreach ( $jobs as $job ) {
153  $jobsByType[$job->getType()][] = $job;
154  }
155 
156  foreach ( $jobsByType as $type => $jobs ) {
157  $this->get( $type )->push( $jobs );
158  }
159 
160  if ( $this->cache->hasField( 'queues-ready', 'list' ) ) {
161  $list = $this->cache->getField( 'queues-ready', 'list' );
162  if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
163  $this->cache->clear( 'queues-ready' );
164  }
165  }
166 
168  $cache->set(
169  $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', self::TYPE_ANY ),
170  'true',
171  15
172  );
173  if ( array_diff( array_keys( $jobsByType ), $wgJobTypesExcludedFromDefaultQueue ) ) {
174  $cache->set(
175  $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', self::TYPE_DEFAULT ),
176  'true',
177  15
178  );
179  }
180  }
181 
193  public function lazyPush( $jobs ) {
194  if ( $this->invalidWiki ) {
195  // Do not enqueue job that cannot be run (T171371)
196  throw new LogicException( "Domain '{$this->domain}' is not recognized." );
197  }
198 
199  if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
200  $this->push( $jobs );
201  return;
202  }
203 
204  $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
205 
206  // Throw errors now instead of on push(), when other jobs may be buffered
207  $this->assertValidJobs( $jobs );
208 
209  DeferredUpdates::addUpdate( new JobQueueEnqueueUpdate( $this->domain, $jobs ) );
210  }
211 
219  public static function pushLazyJobs() {
220  wfDeprecated( __METHOD__, '1.33' );
221  }
222 
234  public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $blacklist = [] ) {
235  $job = false;
236 
237  if ( is_string( $qtype ) ) { // specific job type
238  if ( !in_array( $qtype, $blacklist ) ) {
239  $job = $this->get( $qtype )->pop();
240  }
241  } else { // any job in the "default" jobs types
242  if ( $flags & self::USE_CACHE ) {
243  if ( !$this->cache->hasField( 'queues-ready', 'list', self::PROC_CACHE_TTL ) ) {
244  $this->cache->setField( 'queues-ready', 'list', $this->getQueuesWithJobs() );
245  }
246  $types = $this->cache->getField( 'queues-ready', 'list' );
247  } else {
248  $types = $this->getQueuesWithJobs();
249  }
250 
251  if ( $qtype == self::TYPE_DEFAULT ) {
252  $types = array_intersect( $types, $this->getDefaultQueueTypes() );
253  }
254 
255  $types = array_diff( $types, $blacklist ); // avoid selected types
256  shuffle( $types ); // avoid starvation
257 
258  foreach ( $types as $type ) { // for each queue...
259  $job = $this->get( $type )->pop();
260  if ( $job ) { // found
261  break;
262  } else { // not found
263  $this->cache->clear( 'queues-ready' );
264  }
265  }
266  }
267 
268  return $job;
269  }
270 
277  public function ack( Job $job ) {
278  $this->get( $job->getType() )->ack( $job );
279  }
280 
288  public function deduplicateRootJob( Job $job ) {
289  return $this->get( $job->getType() )->deduplicateRootJob( $job );
290  }
291 
299  public function waitForBackups() {
300  global $wgJobTypeConf;
301 
302  // Try to avoid doing this more than once per queue storage medium
303  foreach ( $wgJobTypeConf as $type => $conf ) {
304  $this->get( $type )->waitForBackups();
305  }
306  }
307 
313  public function getQueueTypes() {
314  return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) );
315  }
316 
322  public function getDefaultQueueTypes() {
324 
325  return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
326  }
327 
335  public function queuesHaveJobs( $type = self::TYPE_ANY ) {
337  $key = $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', $type );
338 
339  $value = $cache->get( $key );
340  if ( $value === false ) {
341  $queues = $this->getQueuesWithJobs();
342  if ( $type == self::TYPE_DEFAULT ) {
343  $queues = array_intersect( $queues, $this->getDefaultQueueTypes() );
344  }
345  $value = count( $queues ) ? 'true' : 'false';
346  $cache->add( $key, $value, 15 );
347  }
348 
349  return ( $value === 'true' );
350  }
351 
357  public function getQueuesWithJobs() {
358  $types = [];
359  foreach ( $this->getCoalescedQueues() as $info ) {
360  $nonEmpty = $info['queue']->getSiblingQueuesWithJobs( $this->getQueueTypes() );
361  if ( is_array( $nonEmpty ) ) { // batching features supported
362  $types = array_merge( $types, $nonEmpty );
363  } else { // we have to go through the queues in the bucket one-by-one
364  foreach ( $info['types'] as $type ) {
365  if ( !$this->get( $type )->isEmpty() ) {
366  $types[] = $type;
367  }
368  }
369  }
370  }
371 
372  return $types;
373  }
374 
380  public function getQueueSizes() {
381  $sizeMap = [];
382  foreach ( $this->getCoalescedQueues() as $info ) {
383  $sizes = $info['queue']->getSiblingQueueSizes( $this->getQueueTypes() );
384  if ( is_array( $sizes ) ) { // batching features supported
385  $sizeMap = $sizeMap + $sizes;
386  } else { // we have to go through the queues in the bucket one-by-one
387  foreach ( $info['types'] as $type ) {
388  $sizeMap[$type] = $this->get( $type )->getSize();
389  }
390  }
391  }
392 
393  return $sizeMap;
394  }
395 
399  protected function getCoalescedQueues() {
400  global $wgJobTypeConf;
401 
402  if ( $this->coalescedQueues === null ) {
403  $this->coalescedQueues = [];
404  foreach ( $wgJobTypeConf as $type => $conf ) {
406  [ 'wiki' => $this->domain, 'type' => 'null' ] + $conf );
407  $loc = $queue->getCoalesceLocationInternal();
408  if ( !isset( $this->coalescedQueues[$loc] ) ) {
409  $this->coalescedQueues[$loc]['queue'] = $queue;
410  $this->coalescedQueues[$loc]['types'] = [];
411  }
412  if ( $type === 'default' ) {
413  $this->coalescedQueues[$loc]['types'] = array_merge(
414  $this->coalescedQueues[$loc]['types'],
415  array_diff( $this->getQueueTypes(), array_keys( $wgJobTypeConf ) )
416  );
417  } else {
418  $this->coalescedQueues[$loc]['types'][] = $type;
419  }
420  }
421  }
422 
423  return $this->coalescedQueues;
424  }
425 
430  private function getCachedConfigVar( $name ) {
431  // @TODO: cleanup this whole method with a proper config system
432  if ( WikiMap::isCurrentWikiDbDomain( $this->domain ) ) {
433  return $GLOBALS[$name]; // common case
434  } else {
435  $wiki = WikiMap::getWikiIdFromDomain( $this->domain );
437  $value = $cache->getWithSetCallback(
438  $cache->makeGlobalKey( 'jobqueue', 'configvalue', $this->domain, $name ),
439  $cache::TTL_DAY + mt_rand( 0, $cache::TTL_DAY ),
440  function () use ( $wiki, $name ) {
441  global $wgConf;
442  // @TODO: use the full domain ID here
443  return [ 'v' => $wgConf->getConfig( $wiki, $name ) ];
444  },
445  [ 'pcTTL' => WANObjectCache::TTL_PROC_LONG ]
446  );
447 
448  return $value['v'];
449  }
450  }
451 
456  private function assertValidJobs( array $jobs ) {
457  foreach ( $jobs as $job ) { // sanity checks
458  if ( !( $job instanceof IJobSpecification ) ) {
459  throw new InvalidArgumentException( "Expected IJobSpecification objects" );
460  }
461  }
462  }
463 }
$wgConf
$wgConf
wgConf hold the site configuration.
Definition: DefaultSettings.php:58
WikiMap\isCurrentWikiDbDomain
static isCurrentWikiDbDomain( $domain)
Definition: WikiMap.php:267
JobQueueGroup\pop
pop( $qtype=self::TYPE_DEFAULT, $flags=0, array $blacklist=[])
Pop a job off one of the job queues.
Definition: JobQueueGroup.php:234
JobQueueGroup\USE_CACHE
const USE_CACHE
Definition: JobQueueGroup.php:49
Job\getType
getType()
Definition: Job.php:128
JobQueueGroup\waitForBackups
waitForBackups()
Wait for any replica DBs or backup queue servers to catch up.
Definition: JobQueueGroup.php:299
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:289
ObjectCache\getLocalClusterInstance
static getLocalClusterInstance()
Get the main cluster-local cache object.
Definition: ObjectCache.php:365
JobQueueGroup\PROC_CACHE_TTL
const PROC_CACHE_TTL
Definition: JobQueueGroup.php:51
JobQueueGroup\CACHE_VERSION
const CACHE_VERSION
Definition: JobQueueGroup.php:53
$wgJobTypeConf
$wgJobTypeConf
Map of job types to configuration arrays.
Definition: DefaultSettings.php:7605
JobQueueGroup\$coalescedQueues
array $coalescedQueues
Map of (bucket => (queue => JobQueue, types => list of types)
Definition: JobQueueGroup.php:44
JobQueueEnqueueUpdate
Enqueue lazy-pushed jobs that have accumulated from JobQueueGroup.
Definition: JobQueueEnqueueUpdate.php:31
captcha-old.count
count
Definition: captcha-old.py:249
JobQueueGroup\$invalidWiki
bool $invalidWiki
Whether the wiki is not recognized in configuration.
Definition: JobQueueGroup.php:41
JobQueueGroup\TYPE_DEFAULT
const TYPE_DEFAULT
Definition: JobQueueGroup.php:46
wfConfiguredReadOnlyReason
wfConfiguredReadOnlyReason()
Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
Definition: GlobalFunctions.php:1261
JobQueueGroup\getCoalescedQueues
getCoalescedQueues()
Definition: JobQueueGroup.php:399
DeferredUpdates\addUpdate
static addUpdate(DeferrableUpdate $update, $stage=self::POSTSEND)
Add an update to the deferred list to be run later by execute()
Definition: DeferredUpdates.php:79
JobQueueGroup\$instances
static JobQueueGroup[] $instances
Definition: JobQueueGroup.php:31
JobQueueGroup\queuesHaveJobs
queuesHaveJobs( $type=self::TYPE_ANY)
Check if there are any queues with jobs (this is cached)
Definition: JobQueueGroup.php:335
JobQueueGroup\destroySingletons
static destroySingletons()
Destroy the singleton instances.
Definition: JobQueueGroup.php:97
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Job
Class to both describe a background job and handle jobs.
Definition: Job.php:30
ProcessCacheLRU\set
set( $key, $prop, $value)
Set a property field for a cache entry.
Definition: ProcessCacheLRU.php:54
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1118
JobQueueGroup\$domain
string $domain
Wiki DB domain ID.
Definition: JobQueueGroup.php:37
JobQueueGroup\push
push( $jobs)
Insert jobs into the respective queues of which they belong.
Definition: JobQueueGroup.php:134
JobQueueGroup\$cache
ProcessCacheLRU $cache
Definition: JobQueueGroup.php:34
$queue
$queue
Definition: mergeMessageFileList.php:160
MapCacheLRU
Handles a simple LRU key/value map with a maximum number of entries.
Definition: MapCacheLRU.php:37
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
JobQueueGroup\getCachedConfigVar
getCachedConfigVar( $name)
Definition: JobQueueGroup.php:430
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
JobQueueGroup\pushLazyJobs
static pushLazyJobs()
Push all jobs buffered via lazyPush() into their respective queues.
Definition: JobQueueGroup.php:219
WikiMap\getWikiIdFromDomain
static getWikiIdFromDomain( $domain)
Get the wiki ID of a database domain.
Definition: WikiMap.php:252
$wgLocalDatabases
$wgLocalDatabases
Other wikis on this site, can be administered from a single developer account.
Definition: DefaultSettings.php:2159
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
$wgJobTypesExcludedFromDefaultQueue
$wgJobTypesExcludedFromDefaultQueue
Jobs that must be explicitly requested, i.e.
Definition: DefaultSettings.php:7571
JobQueueGroup\getDefaultQueueTypes
getDefaultQueueTypes()
Get the list of default queue types.
Definition: JobQueueGroup.php:322
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2221
$value
$value
Definition: styleTest.css.php:49
JobQueueGroup\$readOnlyReason
string bool $readOnlyReason
Read only rationale (or false if r/w)
Definition: JobQueueGroup.php:39
JobQueue\factory
static factory(array $params)
Get a job queue object of the specified type.
Definition: JobQueue.php:104
ProcessCacheLRU\get
get( $key, $prop)
Get a property field for a cache entry.
Definition: ProcessCacheLRU.php:79
JobQueueGroup\deduplicateRootJob
deduplicateRootJob(Job $job)
Register the "root job" of a given job into the queue for de-duplication.
Definition: JobQueueGroup.php:288
JobQueueGroup\ack
ack(Job $job)
Acknowledge that a job was completed.
Definition: JobQueueGroup.php:277
JobQueueGroup\singleton
static singleton( $domain=false)
Definition: JobQueueGroup.php:69
JobQueueGroup\TYPE_ANY
const TYPE_ANY
Definition: JobQueueGroup.php:47
ObjectCache\getMainWANInstance
static getMainWANInstance()
Get the main WAN cache object.
Definition: ObjectCache.php:378
JobQueueGroup\assertValidJobs
assertValidJobs(array $jobs)
Definition: JobQueueGroup.php:456
$job
if(count( $args)< 1) $job
Definition: recompressTracked.php:48
JobQueueGroup\getQueueTypes
getQueueTypes()
Get the list of queue types.
Definition: JobQueueGroup.php:313
JobQueueGroup\getQueuesWithJobs
getQueuesWithJobs()
Get the list of job types that have non-empty queues.
Definition: JobQueueGroup.php:357
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ProcessCacheLRU
Class for process caching individual properties of expiring items.
Definition: ProcessCacheLRU.php:32
JobQueueGroup\getQueueSizes
getQueueSizes()
Get the size of the queus for a list of job types.
Definition: JobQueueGroup.php:380
JobQueueAggregator\singleton
static singleton()
Definition: JobQueueAggregator.php:43
IJobSpecification
Job queue task description interface.
Definition: JobSpecification.php:29
$GLOBALS
$GLOBALS['IP']
Definition: ComposerHookHandler.php:6
IExpiringStore\TTL_PROC_LONG
const TTL_PROC_LONG
Definition: IExpiringStore.php:43
JobQueueGroup\lazyPush
lazyPush( $jobs)
Buffer jobs for insertion via push() or call it now if in CLI mode.
Definition: JobQueueGroup.php:193
JobQueueGroup
Class to handle enqueueing of background jobs.
Definition: JobQueueGroup.php:29
MWExceptionHandler\logException
static logException( $e, $catcher=self::CAUGHT_BY_OTHER)
Log an exception to the exception log (if enabled).
Definition: MWExceptionHandler.php:683
JobQueueGroup\__construct
__construct( $domain, $readOnlyReason)
Definition: JobQueueGroup.php:59
$type
$type
Definition: testCompression.php:48