43 private $localJobClasses;
45 private $jobTypeConfiguration;
47 private $jobTypesExcludedFromDefaultQueue;
49 private $statsdDataFactory;
53 private $globalIdGenerator;
59 private const TYPE_ANY = 2;
63 private const PROC_CACHE_TTL = 15;
80 ?array $localJobClasses,
81 array $jobTypeConfiguration,
82 array $jobTypesExcludedFromDefaultQueue,
87 $this->domain = $domain;
88 $this->readOnlyMode = $readOnlyMode;
90 $this->localJobClasses = $localJobClasses;
91 $this->jobTypeConfiguration = $jobTypeConfiguration;
92 $this->jobTypesExcludedFromDefaultQueue = $jobTypesExcludedFromDefaultQueue;
93 $this->statsdDataFactory = $statsdDataFactory;
94 $this->wanCache = $wanCache;
95 $this->globalIdGenerator = $globalIdGenerator;
104 public function get( $type ) {
105 $conf = [
'domain' => $this->domain,
'type' => $type ];
106 $conf += $this->jobTypeConfiguration[$type] ?? $this->jobTypeConfiguration[
'default'];
107 if ( !isset( $conf[
'readOnlyReason'] ) ) {
108 $conf[
'readOnlyReason'] = $this->readOnlyMode->getConfiguredReason();
111 $conf[
'stats'] = $this->statsdDataFactory;
112 $conf[
'wanCache'] = $this->wanCache;
113 $conf[
'idGenerator'] = $this->globalIdGenerator;
127 public function push( $jobs ) {
128 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
129 if ( $jobs === [] ) {
133 $this->assertValidJobs( $jobs );
136 foreach ( $jobs as
$job ) {
137 $type =
$job->getType();
138 if ( isset( $this->jobTypeConfiguration[$type] ) ) {
139 $jobsByType[$type][] =
$job;
142 isset( $this->jobTypeConfiguration[
'default'][
'typeAgnostic'] ) &&
143 $this->jobTypeConfiguration[
'default'][
'typeAgnostic']
145 $jobsByType[
'default'][] =
$job;
147 $jobsByType[$type][] =
$job;
152 foreach ( $jobsByType as $type => $jobs ) {
153 $this->
get( $type )->
push( $jobs );
156 if ( $this->cache->hasField(
'queues-ready',
'list' ) ) {
157 $list = $this->cache->getField(
'queues-ready',
'list' );
158 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
159 $this->cache->clear(
'queues-ready' );
163 $cache = ObjectCache::getLocalClusterInstance();
165 $cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs', self::TYPE_ANY ),
169 if ( array_diff( array_keys( $jobsByType ), $this->jobTypesExcludedFromDefaultQueue ) ) {
171 $cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs', self::TYPE_DEFAULT ),
186 if ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' ) {
187 $this->
push( $jobs );
191 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
194 $this->assertValidJobs( $jobs );
213 public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $ignored = [] ) {
216 if ( !$this->localJobClasses ) {
218 "Cannot pop '{$qtype}' job off foreign '{$this->domain}' wiki queue." );
220 if ( is_string( $qtype ) && !isset( $this->localJobClasses[$qtype] ) ) {
222 throw new JobQueueError(
"Unrecognized job type '$qtype'." );
225 if ( is_string( $qtype ) ) {
226 if ( !in_array( $qtype, $ignored ) ) {
227 $job = $this->
get( $qtype )->
pop();
230 if ( $flags & self::USE_CACHE ) {
231 if ( !$this->cache->hasField(
'queues-ready',
'list', self::PROC_CACHE_TTL ) ) {
234 $types = $this->cache->getField(
'queues-ready',
'list' );
239 if ( $qtype == self::TYPE_DEFAULT ) {
243 $types = array_diff( $types, $ignored );
246 foreach ( $types as $type ) {
247 $job = $this->
get( $type )->
pop();
251 $this->cache->clear(
'queues-ready' );
294 foreach ( $this->jobTypeConfiguration as $type => $conf ) {
307 if ( !$this->localJobClasses ) {
308 throw new JobQueueError(
'Cannot inspect job queue from foreign wiki' );
310 return array_keys( $this->localJobClasses );
321 return array_diff( $this->
getQueueTypes(), $this->jobTypesExcludedFromDefaultQueue );
334 $cache = ObjectCache::getLocalClusterInstance();
335 $key = $cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs', $type );
337 $value = $cache->
get( $key );
338 if ( $value ===
false ) {
340 if ( $type == self::TYPE_DEFAULT ) {
343 $value = count( $queues ) ?
'true' :
'false';
344 $cache->add( $key, $value, 15 );
347 return ( $value ===
'true' );
361 $queue = $info[
'queue'];
362 $nonEmpty = $queue->getSiblingQueuesWithJobs( $this->
getQueueTypes() );
363 if ( is_array( $nonEmpty ) ) {
364 $types = array_merge( $types, $nonEmpty );
366 foreach ( $info[
'types'] as $type ) {
367 if ( !$this->
get( $type )->isEmpty() ) {
388 $queue = $info[
'queue'];
389 $sizes = $queue->getSiblingQueueSizes( $this->
getQueueTypes() );
390 if ( is_array( $sizes ) ) {
393 foreach ( $info[
'types'] as $type ) {
394 $sizeMap[$type] = $this->
get( $type )->getSize();
407 if ( $this->coalescedQueues ===
null ) {
408 $this->coalescedQueues = [];
409 foreach ( $this->jobTypeConfiguration as $type => $conf ) {
410 $conf[
'domain'] = $this->domain;
411 $conf[
'type'] =
'null';
412 $conf[
'stats'] = $this->statsdDataFactory;
413 $conf[
'wanCache'] = $this->wanCache;
414 $conf[
'idGenerator'] = $this->globalIdGenerator;
417 $loc = $queue->getCoalesceLocationInternal();
418 if ( !isset( $this->coalescedQueues[$loc] ) ) {
419 $this->coalescedQueues[$loc][
'queue'] = $queue;
420 $this->coalescedQueues[$loc][
'types'] = [];
422 if ( $type ===
'default' ) {
423 $this->coalescedQueues[$loc][
'types'] = array_merge(
424 $this->coalescedQueues[$loc][
'types'],
425 array_diff( $this->
getQueueTypes(), array_keys( $this->jobTypeConfiguration ) )
428 $this->coalescedQueues[$loc][
'types'][] = $type;
433 return $this->coalescedQueues;
440 private function assertValidJobs( array $jobs ) {
441 foreach ( $jobs as
$job ) {
443 $type = is_object(
$job ) ? get_class(
$job ) : gettype(
$job );
444 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.
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.
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)
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.
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.
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