91 array $jobTypeConfiguration,
92 array $jobTypesExcludedFromDefaultQueue,
98 $this->readOnlyMode = $readOnlyMode;
100 $this->invalidDomain = $invalidDomain;
101 $this->jobClasses = $jobClasses;
102 $this->jobTypeConfiguration = $jobTypeConfiguration;
103 $this->jobTypesExcludedFromDefaultQueue = $jobTypesExcludedFromDefaultQueue;
104 $this->statsdDataFactory = $statsdDataFactory;
105 $this->wanCache = $wanCache;
106 $this->globalIdGenerator = $globalIdGenerator;
115 return MediaWikiServices::getInstance()->getJobQueueGroupFactory()->makeJobQueueGroup(
$domain );
134 $conf = [
'domain' => $this->domain,
'type' =>
$type ];
135 if ( isset( $this->jobTypeConfiguration[
$type] ) ) {
136 $conf += $this->jobTypeConfiguration[
$type];
138 $conf += $this->jobTypeConfiguration[
'default'];
140 if ( !isset( $conf[
'readOnlyReason'] ) ) {
141 $conf[
'readOnlyReason'] = $this->readOnlyMode->getReason();
144 $conf[
'stats'] = $this->statsdDataFactory;
145 $conf[
'wanCache'] = $this->wanCache;
146 $conf[
'idGenerator'] = $this->globalIdGenerator;
161 public function push( $jobs ) {
162 if ( $this->invalidDomain ) {
164 $e =
new LogicException(
"Domain '{$this->domain}' is not recognized." );
165 MWExceptionHandler::logException( $e );
169 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
170 if ( $jobs === [] ) {
177 foreach ( $jobs as
$job ) {
178 $jobsByType[
$job->getType()][] =
$job;
181 foreach ( $jobsByType as
$type => $jobs ) {
185 if ( $this->cache->hasField(
'queues-ready',
'list' ) ) {
186 $list = $this->cache->getField(
'queues-ready',
'list' );
187 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
188 $this->cache->clear(
'queues-ready' );
192 $cache = ObjectCache::getLocalClusterInstance();
194 $cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs', self::TYPE_ANY ),
198 if ( array_diff( array_keys( $jobsByType ), $this->jobTypesExcludedFromDefaultQueue ) ) {
200 $cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs', self::TYPE_DEFAULT ),
215 if ( $this->invalidDomain ) {
217 throw new LogicException(
"Domain '{$this->domain}' is not recognized." );
220 if ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' ) {
221 $this->
push( $jobs );
225 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
244 public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $ignored = [] ) {
247 if ( !WikiMap::isCurrentWikiDbDomain( $this->domain ) ) {
249 "Cannot pop '{$qtype}' job off foreign '{$this->domain}' wiki queue." );
250 } elseif ( is_string( $qtype ) && !isset( $this->jobClasses[$qtype] ) ) {
252 throw new JobQueueError(
"Unrecognized job type '$qtype'." );
255 if ( is_string( $qtype ) ) {
256 if ( !in_array( $qtype, $ignored ) ) {
257 $job = $this->
get( $qtype )->
pop();
260 if ( $flags & self::USE_CACHE ) {
261 if ( !$this->cache->hasField(
'queues-ready',
'list', self::PROC_CACHE_TTL ) ) {
264 $types = $this->cache->getField(
'queues-ready',
'list' );
269 if ( $qtype == self::TYPE_DEFAULT ) {
273 $types = array_diff( $types, $ignored );
276 foreach ( $types as
$type ) {
281 $this->cache->clear(
'queues-ready' );
319 foreach ( $this->jobTypeConfiguration as
$type => $conf ) {
339 return array_diff( $this->
getQueueTypes(), $this->jobTypesExcludedFromDefaultQueue );
350 $cache = ObjectCache::getLocalClusterInstance();
351 $key =
$cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs',
$type );
353 $value =
$cache->get( $key );
354 if ( $value ===
false ) {
356 if (
$type == self::TYPE_DEFAULT ) {
359 $value = count( $queues ) ?
'true' :
'false';
360 $cache->add( $key, $value, 15 );
363 return ( $value ===
'true' );
377 if ( is_array( $nonEmpty ) ) {
378 $types = array_merge( $types, $nonEmpty );
380 foreach ( $info[
'types'] as
$type ) {
381 if ( !$this->
get( $type )->isEmpty() ) {
402 if ( is_array( $sizes ) ) {
405 foreach ( $info[
'types'] as
$type ) {
406 $sizeMap[
$type] = $this->
get(
$type )->getSize();
419 if ( $this->coalescedQueues ===
null ) {
420 $this->coalescedQueues = [];
421 foreach ( $this->jobTypeConfiguration as
$type => $conf ) {
422 $conf[
'domain'] = $this->domain;
423 $conf[
'type'] =
'null';
424 $conf[
'stats'] = $this->statsdDataFactory;
425 $conf[
'wanCache'] = $this->wanCache;
426 $conf[
'idGenerator'] = $this->globalIdGenerator;
429 $loc =
$queue->getCoalesceLocationInternal();
430 if ( !isset( $this->coalescedQueues[$loc] ) ) {
431 $this->coalescedQueues[$loc][
'queue'] =
$queue;
432 $this->coalescedQueues[$loc][
'types'] = [];
434 if (
$type ===
'default' ) {
435 $this->coalescedQueues[$loc][
'types'] = array_merge(
436 $this->coalescedQueues[$loc][
'types'],
437 array_diff( $this->
getQueueTypes(), array_keys( $this->jobTypeConfiguration ) )
440 $this->coalescedQueues[$loc][
'types'][] =
$type;
445 return $this->coalescedQueues;
454 if ( WikiMap::isCurrentWikiDbDomain( $this->domain ) ) {
455 return $GLOBALS[$name];
457 $wiki = WikiMap::getWikiIdFromDbDomain( $this->domain );
458 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
459 $value =
$cache->getWithSetCallback(
460 $cache->makeGlobalKey(
'jobqueue',
'configvalue', $this->domain, $name ),
461 $cache::TTL_DAY + mt_rand( 0, $cache::TTL_DAY ),
462 static function () use ( $wiki, $name ) {
465 return [
'v' =>
$wgConf->getConfig( $wiki, $name ) ];
467 [
'pcTTL' => WANObjectCache::TTL_PROC_LONG ]
479 foreach ( $jobs as
$job ) {
482 throw new InvalidArgumentException(
"Expected IJobSpecification objects, got " .
$type );
$wgConf
$wgConf hold the site configuration.
Enqueue lazy-pushed jobs that have accumulated from JobQueueGroup.
Class to handle enqueueing of background jobs.
getCachedConfigVar( $name)
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.
__construct( $domain, ConfiguredReadOnlyMode $readOnlyMode, bool $invalidDomain, array $jobClasses, array $jobTypeConfiguration, array $jobTypesExcludedFromDefaultQueue, IBufferingStatsdDataFactory $statsdDataFactory, WANObjectCache $wanCache, GlobalIdGenerator $globalIdGenerator)
waitForBackups()
Wait for any replica DBs or backup queue servers to catch up.
array $coalescedQueues
Map of (bucket => (queue => JobQueue, types => list of types)
static singleton( $domain=false)
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 a job off one of the job queues.
ack(RunnableJob $job)
Acknowledge that a job was completed.
static destroySingletons()
Destroy the singleton instances.
ConfiguredReadOnlyMode $readOnlyMode
Read only mode.
queuesHaveJobs( $type=self::TYPE_ANY)
Check if there are any queues with jobs (this is cached)
bool $invalidDomain
Whether the wiki is not recognized in configuration.
GlobalIdGenerator $globalIdGenerator
static JobQueueGroup[] $instances
array $jobTypesExcludedFromDefaultQueue
getQueuesWithJobs()
Get the list of job types that have non-empty queues.
assertValidJobs(array $jobs)
getQueueTypes()
Get the list of queue types.
IBufferingStatsdDataFactory $statsdDataFactory
deduplicateRootJob(RunnableJob $job)
Register the "root job" of a given job into the queue for de-duplication.
array $jobTypeConfiguration
static factory(array $params)
Get a job queue object of the specified type.
Handles a simple LRU key/value map with a maximum number of entries.
Multi-datacenter aware caching interface.
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.
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()
if(count( $args)< 1) $job