MediaWiki master
JobQueueGroup.php
Go to the documentation of this file.
1<?php
28
41 protected $cache;
42
44 protected $domain;
46 protected $readOnlyMode;
48 private $localJobClasses;
50 private $jobTypeConfiguration;
52 private $jobTypesExcludedFromDefaultQueue;
54 private $statsdDataFactory;
56 private $wanCache;
58 private $globalIdGenerator;
59
62
63 public const TYPE_DEFAULT = 1; // integer; jobs popped by default
64 private const TYPE_ANY = 2; // integer; any job
65
66 public const USE_CACHE = 1; // integer; use process or persistent cache
67
68 private const PROC_CACHE_TTL = 15; // integer; seconds
69
83 public function __construct(
84 $domain,
85 ReadOnlyMode $readOnlyMode,
86 ?array $localJobClasses,
87 array $jobTypeConfiguration,
88 array $jobTypesExcludedFromDefaultQueue,
89 IBufferingStatsdDataFactory $statsdDataFactory,
90 WANObjectCache $wanCache,
91 GlobalIdGenerator $globalIdGenerator
92 ) {
93 $this->domain = $domain;
94 $this->readOnlyMode = $readOnlyMode;
95 $this->cache = new MapCacheLRU( 10 );
96 $this->localJobClasses = $localJobClasses;
97 $this->jobTypeConfiguration = $jobTypeConfiguration;
98 $this->jobTypesExcludedFromDefaultQueue = $jobTypesExcludedFromDefaultQueue;
99 $this->statsdDataFactory = $statsdDataFactory;
100 $this->wanCache = $wanCache;
101 $this->globalIdGenerator = $globalIdGenerator;
102 }
103
110 public function get( $type ) {
111 $conf = [ 'domain' => $this->domain, 'type' => $type ];
112 $conf += $this->jobTypeConfiguration[$type] ?? $this->jobTypeConfiguration['default'];
113 if ( !isset( $conf['readOnlyReason'] ) ) {
114 $conf['readOnlyReason'] = $this->readOnlyMode->getConfiguredReason();
115 }
116
117 $conf['stats'] = $this->statsdDataFactory;
118 $conf['wanCache'] = $this->wanCache;
119 $conf['idGenerator'] = $this->globalIdGenerator;
120
121 return JobQueue::factory( $conf );
122 }
123
133 public function push( $jobs ) {
134 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
135 if ( $jobs === [] ) {
136 return;
137 }
138
139 $this->assertValidJobs( $jobs );
140
141 $jobsByType = []; // (job type => list of jobs)
142 foreach ( $jobs as $job ) {
143 $type = $job->getType();
144 if ( isset( $this->jobTypeConfiguration[$type] ) ) {
145 $jobsByType[$type][] = $job;
146 } else {
147 if (
148 isset( $this->jobTypeConfiguration['default']['typeAgnostic'] ) &&
149 $this->jobTypeConfiguration['default']['typeAgnostic']
150 ) {
151 $jobsByType['default'][] = $job;
152 } else {
153 $jobsByType[$type][] = $job;
154 }
155 }
156 }
157
158 foreach ( $jobsByType as $type => $jobs ) {
159 $this->get( $type )->push( $jobs );
160 }
161
162 if ( $this->cache->hasField( 'queues-ready', 'list' ) ) {
163 $list = $this->cache->getField( 'queues-ready', 'list' );
164 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
165 $this->cache->clear( 'queues-ready' );
166 }
167 }
168
169 $cache = MediaWikiServices::getInstance()->getObjectCacheFactory()->getLocalClusterInstance();
170 $cache->set(
171 $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', self::TYPE_ANY ),
172 'true',
173 15
174 );
175 if ( array_diff( array_keys( $jobsByType ), $this->jobTypesExcludedFromDefaultQueue ) ) {
176 $cache->set(
177 $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', self::TYPE_DEFAULT ),
178 'true',
179 15
180 );
181 }
182 }
183
191 public function lazyPush( $jobs ) {
192 if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
193 $this->push( $jobs );
194 return;
195 }
196
197 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
198
199 // Throw errors now instead of on push(), when other jobs may be buffered
200 $this->assertValidJobs( $jobs );
201
202 DeferredUpdates::addUpdate( new JobQueueEnqueueUpdate( $this->domain, $jobs ) );
203 }
204
219 public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $ignored = [] ) {
220 $job = false;
221
222 if ( !$this->localJobClasses ) {
223 throw new JobQueueError(
224 "Cannot pop '{$qtype}' job off foreign '{$this->domain}' wiki queue." );
225 }
226 if ( is_string( $qtype ) && !isset( $this->localJobClasses[$qtype] ) ) {
227 // Do not pop jobs if there is no class for the queue type
228 throw new JobQueueError( "Unrecognized job type '$qtype'." );
229 }
230
231 if ( is_string( $qtype ) ) { // specific job type
232 if ( !in_array( $qtype, $ignored ) ) {
233 $job = $this->get( $qtype )->pop();
234 }
235 } else { // any job in the "default" jobs types
236 if ( $flags & self::USE_CACHE ) {
237 if ( !$this->cache->hasField( 'queues-ready', 'list', self::PROC_CACHE_TTL ) ) {
238 $this->cache->setField( 'queues-ready', 'list', $this->getQueuesWithJobs() );
239 }
240 $types = $this->cache->getField( 'queues-ready', 'list' );
241 } else {
242 $types = $this->getQueuesWithJobs();
243 }
244
245 if ( $qtype == self::TYPE_DEFAULT ) {
246 $types = array_intersect( $types, $this->getDefaultQueueTypes() );
247 }
248
249 $types = array_diff( $types, $ignored ); // avoid selected types
250 shuffle( $types ); // avoid starvation
251
252 foreach ( $types as $type ) { // for each queue...
253 $job = $this->get( $type )->pop();
254 if ( $job ) { // found
255 break;
256 } else { // not found
257 $this->cache->clear( 'queues-ready' );
258 }
259 }
260 }
261
262 return $job;
263 }
264
271 public function ack( RunnableJob $job ) {
272 $this->get( $job->getType() )->ack( $job );
273 }
274
284 wfDeprecated( __METHOD__, '1.40' );
285 return true;
286 }
287
297 public function waitForBackups() {
298 wfDeprecated( __METHOD__, '1.41' );
299 // Try to avoid doing this more than once per queue storage medium
300 foreach ( $this->jobTypeConfiguration as $type => $conf ) {
301 $this->get( $type )->waitForBackups();
302 }
303 }
304
312 public function getQueueTypes() {
313 if ( !$this->localJobClasses ) {
314 throw new JobQueueError( 'Cannot inspect job queue from foreign wiki' );
315 }
316 return array_keys( $this->localJobClasses );
317 }
318
326 public function getDefaultQueueTypes() {
327 return array_diff( $this->getQueueTypes(), $this->jobTypesExcludedFromDefaultQueue );
328 }
329
339 public function queuesHaveJobs( $type = self::TYPE_ANY ) {
340 $cache = MediaWikiServices::getInstance()->getObjectCacheFactory()->getLocalClusterInstance();
341 $key = $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', $type );
342
343 $value = $cache->get( $key );
344 if ( $value === false ) {
345 $queues = $this->getQueuesWithJobs();
346 if ( $type == self::TYPE_DEFAULT ) {
347 $queues = array_intersect( $queues, $this->getDefaultQueueTypes() );
348 }
349 $value = count( $queues ) ? 'true' : 'false';
350 $cache->add( $key, $value, 15 );
351 }
352
353 return ( $value === 'true' );
354 }
355
363 public function getQueuesWithJobs() {
364 $types = [];
365 foreach ( $this->getCoalescedQueues() as $info ) {
367 $queue = $info['queue'];
368 $nonEmpty = $queue->getSiblingQueuesWithJobs( $this->getQueueTypes() );
369 if ( is_array( $nonEmpty ) ) { // batching features supported
370 $types = array_merge( $types, $nonEmpty );
371 } else { // we have to go through the queues in the bucket one-by-one
372 foreach ( $info['types'] as $type ) {
373 if ( !$this->get( $type )->isEmpty() ) {
374 $types[] = $type;
375 }
376 }
377 }
378 }
379
380 return $types;
381 }
382
390 public function getQueueSizes() {
391 $sizeMap = [];
392 foreach ( $this->getCoalescedQueues() as $info ) {
394 $queue = $info['queue'];
395 $sizes = $queue->getSiblingQueueSizes( $this->getQueueTypes() );
396 if ( is_array( $sizes ) ) { // batching features supported
397 $sizeMap += $sizes;
398 } else { // we have to go through the queues in the bucket one-by-one
399 foreach ( $info['types'] as $type ) {
400 $sizeMap[$type] = $this->get( $type )->getSize();
401 }
402 }
403 }
404
405 return $sizeMap;
406 }
407
412 protected function getCoalescedQueues() {
413 if ( $this->coalescedQueues === null ) {
414 $this->coalescedQueues = [];
415 foreach ( $this->jobTypeConfiguration as $type => $conf ) {
416 $conf['domain'] = $this->domain;
417 $conf['type'] = 'null';
418 $conf['stats'] = $this->statsdDataFactory;
419 $conf['wanCache'] = $this->wanCache;
420 $conf['idGenerator'] = $this->globalIdGenerator;
421
422 $queue = JobQueue::factory( $conf );
423 $loc = $queue->getCoalesceLocationInternal();
424 if ( !isset( $this->coalescedQueues[$loc] ) ) {
425 $this->coalescedQueues[$loc]['queue'] = $queue;
426 $this->coalescedQueues[$loc]['types'] = [];
427 }
428 if ( $type === 'default' ) {
429 $this->coalescedQueues[$loc]['types'] = array_merge(
430 $this->coalescedQueues[$loc]['types'],
431 array_diff( $this->getQueueTypes(), array_keys( $this->jobTypeConfiguration ) )
432 );
433 } else {
434 $this->coalescedQueues[$loc]['types'][] = $type;
435 }
436 }
437 }
438
439 return $this->coalescedQueues;
440 }
441
445 private function assertValidJobs( array $jobs ) {
446 foreach ( $jobs as $job ) {
447 if ( !( $job instanceof IJobSpecification ) ) {
448 $type = get_debug_type( $job );
449 throw new InvalidArgumentException( "Expected IJobSpecification objects, got " . $type );
450 }
451 }
452 }
453}
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Handle enqueueing of background jobs.
push( $jobs)
Insert jobs into the respective queues of which they belong.
string $domain
Wiki domain ID.
getQueueSizes()
Get the size of the queues for a list of job types.
waitForBackups()
Wait for any replica DBs or backup queue servers to catch up.
array $coalescedQueues
Map of (bucket => (queue => JobQueue, types => list of types)
getDefaultQueueTypes()
Get the list of default queue types.
lazyPush( $jobs)
Buffer jobs for insertion via push() or call it now if in CLI mode.
pop( $qtype=self::TYPE_DEFAULT, $flags=0, array $ignored=[])
Pop one job off a job queue.
ack(RunnableJob $job)
Acknowledge that a job was completed.
queuesHaveJobs( $type=self::TYPE_ANY)
Check if there are any queues with jobs (this is cached)
ReadOnlyMode $readOnlyMode
Read only mode.
__construct( $domain, ReadOnlyMode $readOnlyMode, ?array $localJobClasses, array $jobTypeConfiguration, array $jobTypesExcludedFromDefaultQueue, IBufferingStatsdDataFactory $statsdDataFactory, WANObjectCache $wanCache, GlobalIdGenerator $globalIdGenerator)
MapCacheLRU $cache
getQueuesWithJobs()
Get the list of job types that have non-empty queues.
getQueueTypes()
Get the list of queue types.
deduplicateRootJob(RunnableJob $job)
Register the "root job" of a given job into the queue for de-duplication.
static factory(array $params)
Get a job queue object of the specified type.
Definition JobQueue.php:152
Store key-value entries in a size-limited in-memory LRU cache.
set( $key, $value, $rank=self::RANK_TOP)
Set a key/value pair.
get( $key, $maxAge=INF, $default=null)
Get the value for a key.
Defer callable updates to run later in the PHP process.
Enqueue lazy-pushed jobs that have accumulated from JobQueueGroup.
Service locator for MediaWiki core services.
Multi-datacenter aware caching interface.
Determine whether a site is currently in read-only mode.
Class for getting statistically unique IDs without a central coordinator.
Interface for serializable objects that describe a job queue task.
Job that has a run() method and metadata accessors for JobQueue::pop() and JobQueue::ack().
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.
if(count( $args)< 1) $job