40 protected $invalidDomain =
false;
44 private $jobTypeConfiguration;
46 private $jobTypesExcludedFromDefaultQueue;
48 private $statsdDataFactory;
52 private $globalIdGenerator;
58 private const TYPE_ANY = 2;
62 private const PROC_CACHE_TTL = 15;
64 private const CACHE_VERSION = 1;
84 array $jobTypeConfiguration,
85 array $jobTypesExcludedFromDefaultQueue,
90 $this->domain = $domain;
91 $this->readOnlyMode = $readOnlyMode;
93 $this->invalidDomain = $invalidDomain;
94 $this->jobClasses = $jobClasses;
95 $this->jobTypeConfiguration = $jobTypeConfiguration;
96 $this->jobTypesExcludedFromDefaultQueue = $jobTypesExcludedFromDefaultQueue;
97 $this->statsdDataFactory = $statsdDataFactory;
98 $this->wanCache = $wanCache;
99 $this->globalIdGenerator = $globalIdGenerator;
109 $conf = [
'domain' => $this->domain,
'type' =>
$type ];
110 $conf += $this->jobTypeConfiguration[
$type] ?? $this->jobTypeConfiguration[
'default'];
111 if ( !isset( $conf[
'readOnlyReason'] ) ) {
112 $conf[
'readOnlyReason'] = $this->readOnlyMode->getReason();
115 $conf[
'stats'] = $this->statsdDataFactory;
116 $conf[
'wanCache'] = $this->wanCache;
117 $conf[
'idGenerator'] = $this->globalIdGenerator;
132 public function push( $jobs ) {
133 if ( $this->invalidDomain ) {
135 $e =
new LogicException(
"Domain '{$this->domain}' is not recognized." );
136 MWExceptionHandler::logException( $e );
140 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
141 if ( $jobs === [] ) {
145 $this->assertValidJobs( $jobs );
148 foreach ( $jobs as
$job ) {
150 if ( isset( $this->jobTypeConfiguration[
$type] ) ) {
154 isset( $this->jobTypeConfiguration[
'default'][
'typeAgnostic'] ) &&
155 $this->jobTypeConfiguration[
'default'][
'typeAgnostic']
157 $jobsByType[
'default'][] =
$job;
164 foreach ( $jobsByType as
$type => $jobs ) {
168 if ( $this->cache->hasField(
'queues-ready',
'list' ) ) {
169 $list = $this->cache->getField(
'queues-ready',
'list' );
170 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
171 $this->cache->clear(
'queues-ready' );
175 $cache = ObjectCache::getLocalClusterInstance();
177 $cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs', self::TYPE_ANY ),
181 if ( array_diff( array_keys( $jobsByType ), $this->jobTypesExcludedFromDefaultQueue ) ) {
183 $cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs', self::TYPE_DEFAULT ),
198 if ( $this->invalidDomain ) {
200 throw new LogicException(
"Domain '{$this->domain}' is not recognized." );
203 if ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' ) {
204 $this->
push( $jobs );
208 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
211 $this->assertValidJobs( $jobs );
227 public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $ignored = [] ) {
230 if ( !WikiMap::isCurrentWikiDbDomain( $this->domain ) ) {
232 "Cannot pop '{$qtype}' job off foreign '{$this->domain}' wiki queue." );
233 } elseif ( is_string( $qtype ) && !isset( $this->jobClasses[$qtype] ) ) {
235 throw new JobQueueError(
"Unrecognized job type '$qtype'." );
238 if ( is_string( $qtype ) ) {
239 if ( !in_array( $qtype, $ignored ) ) {
240 $job = $this->
get( $qtype )->
pop();
243 if ( $flags & self::USE_CACHE ) {
244 if ( !$this->cache->hasField(
'queues-ready',
'list', self::PROC_CACHE_TTL ) ) {
247 $types = $this->cache->getField(
'queues-ready',
'list' );
252 if ( $qtype == self::TYPE_DEFAULT ) {
256 $types = array_diff( $types, $ignored );
259 foreach ( $types as
$type ) {
264 $this->cache->clear(
'queues-ready' );
304 foreach ( $this->jobTypeConfiguration as
$type => $conf ) {
315 return array_keys( $this->getCachedConfigVar(
'wgJobClasses' ) );
324 return array_diff( $this->
getQueueTypes(), $this->jobTypesExcludedFromDefaultQueue );
335 $cache = ObjectCache::getLocalClusterInstance();
336 $key = $cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs',
$type );
338 $value = $cache->
get( $key );
339 if ( $value ===
false ) {
341 if (
$type == self::TYPE_DEFAULT ) {
344 $value = count( $queues ) ?
'true' :
'false';
345 $cache->add( $key, $value, 15 );
348 return ( $value ===
'true' );
360 $queue = $info[
'queue'];
361 $nonEmpty = $queue->getSiblingQueuesWithJobs( $this->
getQueueTypes() );
362 if ( is_array( $nonEmpty ) ) {
363 $types = array_merge( $types, $nonEmpty );
365 foreach ( $info[
'types'] as
$type ) {
366 if ( !$this->
get( $type )->isEmpty() ) {
385 $queue = $info[
'queue'];
386 $sizes = $queue->getSiblingQueueSizes( $this->
getQueueTypes() );
387 if ( is_array( $sizes ) ) {
390 foreach ( $info[
'types'] as
$type ) {
391 $sizeMap[
$type] = $this->
get(
$type )->getSize();
404 if ( $this->coalescedQueues ===
null ) {
405 $this->coalescedQueues = [];
406 foreach ( $this->jobTypeConfiguration as
$type => $conf ) {
407 $conf[
'domain'] = $this->domain;
408 $conf[
'type'] =
'null';
409 $conf[
'stats'] = $this->statsdDataFactory;
410 $conf[
'wanCache'] = $this->wanCache;
411 $conf[
'idGenerator'] = $this->globalIdGenerator;
414 $loc = $queue->getCoalesceLocationInternal();
415 if ( !isset( $this->coalescedQueues[$loc] ) ) {
416 $this->coalescedQueues[$loc][
'queue'] = $queue;
417 $this->coalescedQueues[$loc][
'types'] = [];
419 if (
$type ===
'default' ) {
420 $this->coalescedQueues[$loc][
'types'] = array_merge(
421 $this->coalescedQueues[$loc][
'types'],
422 array_diff( $this->
getQueueTypes(), array_keys( $this->jobTypeConfiguration ) )
425 $this->coalescedQueues[$loc][
'types'][] =
$type;
430 return $this->coalescedQueues;
437 private function getCachedConfigVar( $name ) {
439 if ( WikiMap::isCurrentWikiDbDomain( $this->domain ) ) {
440 return $GLOBALS[$name];
442 $wiki = WikiMap::getWikiIdFromDbDomain( $this->domain );
443 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
445 $cache->makeGlobalKey(
'jobqueue',
'configvalue', $this->domain, $name ),
446 $cache::TTL_DAY + mt_rand( 0, $cache::TTL_DAY ),
447 static function () use ( $wiki, $name ) {
450 return [
'v' =>
$wgConf->getConfig( $wiki, $name ) ];
452 [
'pcTTL' => WANObjectCache::TTL_PROC_LONG ]
463 private function assertValidJobs( array $jobs ) {
464 foreach ( $jobs as
$job ) {
467 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.
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)
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.
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.
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.
set( $key, $value, $rank=self::RANK_TOP)
Set a key/value pair.
get( $key, $maxAge=INF, $default=null)
Get the value for a key.
getWithSetCallback( $key, callable $callback, $rank=self::RANK_TOP, $maxAge=INF)
Get an item with the given key, producing and setting it if not found.
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