37 protected static $instances = [];
47 protected $invalidDomain =
false;
51 private $jobTypeConfiguration;
53 private $jobTypesExcludedFromDefaultQueue;
55 private $statsdDataFactory;
59 private $globalIdGenerator;
65 private const TYPE_ANY = 2;
69 private const PROC_CACHE_TTL = 15;
71 private const CACHE_VERSION = 1;
91 array $jobTypeConfiguration,
92 array $jobTypesExcludedFromDefaultQueue,
97 $this->domain = $domain;
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;
116 return MediaWikiServices::getInstance()->getJobQueueGroupFactory()->makeJobQueueGroup( $domain );
136 $conf = [
'domain' => $this->domain,
'type' =>
$type ];
137 $conf += $this->jobTypeConfiguration[
$type] ?? $this->jobTypeConfiguration[
'default'];
138 if ( !isset( $conf[
'readOnlyReason'] ) ) {
139 $conf[
'readOnlyReason'] = $this->readOnlyMode->getReason();
142 $conf[
'stats'] = $this->statsdDataFactory;
143 $conf[
'wanCache'] = $this->wanCache;
144 $conf[
'idGenerator'] = $this->globalIdGenerator;
159 public function push( $jobs ) {
160 if ( $this->invalidDomain ) {
162 $e =
new LogicException(
"Domain '{$this->domain}' is not recognized." );
163 MWExceptionHandler::logException( $e );
167 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
168 if ( $jobs === [] ) {
172 $this->assertValidJobs( $jobs );
175 foreach ( $jobs as
$job ) {
177 if ( isset( $this->jobTypeConfiguration[
$type] ) ) {
181 isset( $this->jobTypeConfiguration[
'default'][
'typeAgnostic'] ) &&
182 $this->jobTypeConfiguration[
'default'][
'typeAgnostic']
184 $jobsByType[
'default'][] =
$job;
191 foreach ( $jobsByType as
$type => $jobs ) {
195 if ( $this->cache->hasField(
'queues-ready',
'list' ) ) {
196 $list = $this->cache->getField(
'queues-ready',
'list' );
197 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
198 $this->cache->clear(
'queues-ready' );
202 $cache = ObjectCache::getLocalClusterInstance();
204 $cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs', self::TYPE_ANY ),
208 if ( array_diff( array_keys( $jobsByType ), $this->jobTypesExcludedFromDefaultQueue ) ) {
210 $cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs', self::TYPE_DEFAULT ),
225 if ( $this->invalidDomain ) {
227 throw new LogicException(
"Domain '{$this->domain}' is not recognized." );
230 if ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' ) {
231 $this->
push( $jobs );
235 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
238 $this->assertValidJobs( $jobs );
254 public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $ignored = [] ) {
257 if ( !WikiMap::isCurrentWikiDbDomain( $this->domain ) ) {
259 "Cannot pop '{$qtype}' job off foreign '{$this->domain}' wiki queue." );
260 } elseif ( is_string( $qtype ) && !isset( $this->jobClasses[$qtype] ) ) {
262 throw new JobQueueError(
"Unrecognized job type '$qtype'." );
265 if ( is_string( $qtype ) ) {
266 if ( !in_array( $qtype, $ignored ) ) {
267 $job = $this->
get( $qtype )->
pop();
270 if ( $flags & self::USE_CACHE ) {
271 if ( !$this->cache->hasField(
'queues-ready',
'list', self::PROC_CACHE_TTL ) ) {
274 $types = $this->cache->getField(
'queues-ready',
'list' );
279 if ( $qtype == self::TYPE_DEFAULT ) {
283 $types = array_diff( $types, $ignored );
286 foreach ( $types as
$type ) {
291 $this->cache->clear(
'queues-ready' );
329 foreach ( $this->jobTypeConfiguration as
$type => $conf ) {
340 return array_keys( $this->getCachedConfigVar(
'wgJobClasses' ) );
349 return array_diff( $this->
getQueueTypes(), $this->jobTypesExcludedFromDefaultQueue );
360 $cache = ObjectCache::getLocalClusterInstance();
361 $key =
$cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs',
$type );
363 $value =
$cache->get( $key );
364 if ( $value ===
false ) {
366 if (
$type == self::TYPE_DEFAULT ) {
369 $value = count( $queues ) ?
'true' :
'false';
370 $cache->add( $key, $value, 15 );
373 return ( $value ===
'true' );
387 if ( is_array( $nonEmpty ) ) {
388 $types = array_merge( $types, $nonEmpty );
390 foreach ( $info[
'types'] as
$type ) {
391 if ( !$this->
get( $type )->isEmpty() ) {
412 if ( is_array( $sizes ) ) {
415 foreach ( $info[
'types'] as
$type ) {
416 $sizeMap[
$type] = $this->
get(
$type )->getSize();
429 if ( $this->coalescedQueues ===
null ) {
430 $this->coalescedQueues = [];
431 foreach ( $this->jobTypeConfiguration as
$type => $conf ) {
432 $conf[
'domain'] = $this->domain;
433 $conf[
'type'] =
'null';
434 $conf[
'stats'] = $this->statsdDataFactory;
435 $conf[
'wanCache'] = $this->wanCache;
436 $conf[
'idGenerator'] = $this->globalIdGenerator;
439 $loc =
$queue->getCoalesceLocationInternal();
440 if ( !isset( $this->coalescedQueues[$loc] ) ) {
441 $this->coalescedQueues[$loc][
'queue'] =
$queue;
442 $this->coalescedQueues[$loc][
'types'] = [];
444 if (
$type ===
'default' ) {
445 $this->coalescedQueues[$loc][
'types'] = array_merge(
446 $this->coalescedQueues[$loc][
'types'],
447 array_diff( $this->
getQueueTypes(), array_keys( $this->jobTypeConfiguration ) )
450 $this->coalescedQueues[$loc][
'types'][] =
$type;
455 return $this->coalescedQueues;
462 private function getCachedConfigVar( $name ) {
464 if ( WikiMap::isCurrentWikiDbDomain( $this->domain ) ) {
465 return $GLOBALS[$name];
468 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
469 $value =
$cache->getWithSetCallback(
470 $cache->makeGlobalKey(
'jobqueue',
'configvalue', $this->domain, $name ),
471 $cache::TTL_DAY + mt_rand( 0, $cache::TTL_DAY ),
472 static function () use ( $wiki, $name ) {
475 return [
'v' =>
$wgConf->getConfig( $wiki, $name ) ];
477 [
'pcTTL' => WANObjectCache::TTL_PROC_LONG ]
488 private function assertValidJobs( array $jobs ) {
489 foreach ( $jobs as
$job ) {
492 throw new InvalidArgumentException(
"Expected IJobSpecification objects, got " .
$type );
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
$wgConf
$wgConf hold the site configuration.
Enqueue lazy-pushed jobs that have accumulated from JobQueueGroup.
Class to 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.
__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.
static JobQueueGroup[] $instances
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.
Handles a simple LRU key/value map with a maximum number of entries.
Multi-datacenter aware caching interface.
static getWikiIdFromDbDomain( $domain)
Get the wiki ID of a database domain.
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