48 private $localJobClasses;
50 private $jobTypeConfiguration;
52 private $jobTypesExcludedFromDefaultQueue;
54 private $statsdDataFactory;
58 private $globalIdGenerator;
64 private const TYPE_ANY = 2;
68 private const PROC_CACHE_TTL = 15;
86 ?array $localJobClasses,
87 array $jobTypeConfiguration,
88 array $jobTypesExcludedFromDefaultQueue,
93 $this->domain = $domain;
94 $this->readOnlyMode = $readOnlyMode;
96 $this->localJobClasses = $localJobClasses;
97 $this->jobTypeConfiguration = $jobTypeConfiguration;
98 $this->jobTypesExcludedFromDefaultQueue = $jobTypesExcludedFromDefaultQueue;
99 $this->statsdDataFactory = $statsdDataFactory;
100 $this->wanCache = $wanCache;
101 $this->globalIdGenerator = $globalIdGenerator;
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();
117 $conf[
'stats'] = $this->statsdDataFactory;
118 $conf[
'wanCache'] = $this->wanCache;
119 $conf[
'idGenerator'] = $this->globalIdGenerator;
133 public function push( $jobs ) {
134 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
135 if ( $jobs === [] ) {
139 $this->assertValidJobs( $jobs );
142 foreach ( $jobs as
$job ) {
143 $type =
$job->getType();
144 if ( isset( $this->jobTypeConfiguration[$type] ) ) {
145 $jobsByType[$type][] =
$job;
148 isset( $this->jobTypeConfiguration[
'default'][
'typeAgnostic'] ) &&
149 $this->jobTypeConfiguration[
'default'][
'typeAgnostic']
151 $jobsByType[
'default'][] =
$job;
153 $jobsByType[$type][] =
$job;
158 foreach ( $jobsByType as $type => $jobs ) {
159 $this->
get( $type )->
push( $jobs );
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' );
169 $cache = MediaWikiServices::getInstance()->getObjectCacheFactory()->getLocalClusterInstance();
171 $cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs', self::TYPE_ANY ),
175 if ( array_diff( array_keys( $jobsByType ), $this->jobTypesExcludedFromDefaultQueue ) ) {
177 $cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs', self::TYPE_DEFAULT ),
192 if ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' ) {
193 $this->
push( $jobs );
197 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
200 $this->assertValidJobs( $jobs );
219 public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $ignored = [] ) {
222 if ( !$this->localJobClasses ) {
224 "Cannot pop '{$qtype}' job off foreign '{$this->domain}' wiki queue." );
226 if ( is_string( $qtype ) && !isset( $this->localJobClasses[$qtype] ) ) {
228 throw new JobQueueError(
"Unrecognized job type '$qtype'." );
231 if ( is_string( $qtype ) ) {
232 if ( !in_array( $qtype, $ignored ) ) {
233 $job = $this->
get( $qtype )->
pop();
236 if ( $flags & self::USE_CACHE ) {
237 if ( !$this->cache->hasField(
'queues-ready',
'list', self::PROC_CACHE_TTL ) ) {
240 $types = $this->cache->getField(
'queues-ready',
'list' );
245 if ( $qtype == self::TYPE_DEFAULT ) {
249 $types = array_diff( $types, $ignored );
252 foreach ( $types as $type ) {
253 $job = $this->
get( $type )->
pop();
257 $this->cache->clear(
'queues-ready' );
300 foreach ( $this->jobTypeConfiguration as $type => $conf ) {
313 if ( !$this->localJobClasses ) {
314 throw new JobQueueError(
'Cannot inspect job queue from foreign wiki' );
316 return array_keys( $this->localJobClasses );
327 return array_diff( $this->
getQueueTypes(), $this->jobTypesExcludedFromDefaultQueue );
340 $cache = MediaWikiServices::getInstance()->getObjectCacheFactory()->getLocalClusterInstance();
341 $key = $cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs', $type );
343 $value = $cache->
get( $key );
344 if ( $value ===
false ) {
346 if ( $type == self::TYPE_DEFAULT ) {
349 $value = count( $queues ) ?
'true' :
'false';
350 $cache->add( $key, $value, 15 );
353 return ( $value ===
'true' );
367 $queue = $info[
'queue'];
368 $nonEmpty = $queue->getSiblingQueuesWithJobs( $this->
getQueueTypes() );
369 if ( is_array( $nonEmpty ) ) {
370 $types = array_merge( $types, $nonEmpty );
372 foreach ( $info[
'types'] as $type ) {
373 if ( !$this->
get( $type )->isEmpty() ) {
394 $queue = $info[
'queue'];
395 $sizes = $queue->getSiblingQueueSizes( $this->
getQueueTypes() );
396 if ( is_array( $sizes ) ) {
399 foreach ( $info[
'types'] as $type ) {
400 $sizeMap[$type] = $this->
get( $type )->getSize();
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;
423 $loc = $queue->getCoalesceLocationInternal();
424 if ( !isset( $this->coalescedQueues[$loc] ) ) {
425 $this->coalescedQueues[$loc][
'queue'] = $queue;
426 $this->coalescedQueues[$loc][
'types'] = [];
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 ) )
434 $this->coalescedQueues[$loc][
'types'][] = $type;
439 return $this->coalescedQueues;
445 private function assertValidJobs( array $jobs ) {
446 foreach ( $jobs as
$job ) {
448 $type = get_debug_type(
$job );
449 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.
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.
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