70 public static function singleton( $domain =
false ) {
74 $domain = WikiMap::getCurrentWikiDbDomain()->getId();
77 if ( !isset( self::$instances[
$domain] ) ) {
81 $wikiId = WikiMap::getWikiIdFromDbDomain( $domain );
83 !WikiMap::isCurrentWikiDbDomain( $domain ) &&
86 self::$instances[$domain]->invalidDomain =
true;
90 return self::$instances[$domain];
99 self::$instances = [];
111 $conf = [
'domain' => $this->domain,
'type' =>
$type ];
117 if ( !isset( $conf[
'readOnlyReason'] ) ) {
118 $conf[
'readOnlyReason'] = $this->readOnlyReason;
130 $services = MediaWikiServices::getInstance();
131 $conf[
'stats'] = $services->getStatsdDataFactory();
132 $conf[
'wanCache'] = $services->getMainWANObjectCache();
133 $conf[
'idGenerator'] = $services->getGlobalIdGenerator();
148 public function push( $jobs ) {
151 if ( $this->invalidDomain ) {
153 $e =
new LogicException(
"Domain '{$this->domain}' is not recognized." );
154 MWExceptionHandler::logException( $e );
158 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
159 if ( $jobs === [] ) {
166 foreach ( $jobs as
$job ) {
167 $jobsByType[
$job->getType()][] =
$job;
170 foreach ( $jobsByType as
$type => $jobs ) {
174 if ( $this->cache->hasField(
'queues-ready',
'list' ) ) {
175 $list = $this->cache->getField(
'queues-ready',
'list' );
176 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
177 $this->cache->clear(
'queues-ready' );
181 $cache = ObjectCache::getLocalClusterInstance();
183 $cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs', self::TYPE_ANY ),
189 $cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs', self::TYPE_DEFAULT ),
204 if ( $this->invalidDomain ) {
206 throw new LogicException(
"Domain '{$this->domain}' is not recognized." );
209 if ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' ) {
210 $this->
push( $jobs );
214 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
233 public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $blacklist = [] ) {
238 if ( !WikiMap::isCurrentWikiDbDomain( $this->domain ) ) {
240 "Cannot pop '{$qtype}' job off foreign '{$this->domain}' wiki queue." );
241 } elseif ( is_string( $qtype ) && !isset(
$wgJobClasses[$qtype] ) ) {
243 throw new JobQueueError(
"Unrecognized job type '$qtype'." );
246 if ( is_string( $qtype ) ) {
247 if ( !in_array( $qtype, $blacklist ) ) {
248 $job = $this->
get( $qtype )->
pop();
251 if ( $flags & self::USE_CACHE ) {
252 if ( !$this->cache->hasField(
'queues-ready',
'list', self::PROC_CACHE_TTL ) ) {
255 $types = $this->cache->getField(
'queues-ready',
'list' );
260 if ( $qtype == self::TYPE_DEFAULT ) {
264 $types = array_diff( $types, $blacklist );
267 foreach ( $types as
$type ) {
272 $this->cache->clear(
'queues-ready' );
345 $cache = ObjectCache::getLocalClusterInstance();
346 $key =
$cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs',
$type );
348 $value =
$cache->get( $key );
349 if ( $value ===
false ) {
351 if (
$type == self::TYPE_DEFAULT ) {
354 $value = count( $queues ) ?
'true' :
'false';
355 $cache->add( $key, $value, 15 );
358 return ( $value ===
'true' );
372 if ( is_array( $nonEmpty ) ) {
373 $types = array_merge( $types, $nonEmpty );
375 foreach ( $info[
'types'] as
$type ) {
376 if ( !$this->
get( $type )->isEmpty() ) {
397 if ( is_array( $sizes ) ) {
400 foreach ( $info[
'types'] as
$type ) {
401 $sizeMap[
$type] = $this->
get(
$type )->getSize();
416 if ( $this->coalescedQueues ===
null ) {
417 $this->coalescedQueues = [];
420 [
'domain' => $this->domain,
'type' =>
'null' ] + $conf );
421 $loc =
$queue->getCoalesceLocationInternal();
422 if ( !isset( $this->coalescedQueues[$loc] ) ) {
423 $this->coalescedQueues[$loc][
'queue'] =
$queue;
424 $this->coalescedQueues[$loc][
'types'] = [];
426 if (
$type ===
'default' ) {
427 $this->coalescedQueues[$loc][
'types'] = array_merge(
428 $this->coalescedQueues[$loc][
'types'],
432 $this->coalescedQueues[$loc][
'types'][] =
$type;
437 return $this->coalescedQueues;
446 if ( WikiMap::isCurrentWikiDbDomain( $this->domain ) ) {
449 $wiki = WikiMap::getWikiIdFromDbDomain( $this->domain );
450 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
451 $value =
$cache->getWithSetCallback(
452 $cache->makeGlobalKey(
'jobqueue',
'configvalue', $this->domain, $name ),
453 $cache::TTL_DAY + mt_rand( 0, $cache::TTL_DAY ),
454 function () use ( $wiki, $name ) {
457 return [
'v' =>
$wgConf->getConfig( $wiki, $name ) ];
459 [
'pcTTL' => WANObjectCache::TTL_PROC_LONG ]
471 foreach ( $jobs as
$job ) {
473 throw new InvalidArgumentException(
"Expected IJobSpecification objects" );
$wgJobTypeConf
Map of job types to configuration arrays.
$wgJobTypesExcludedFromDefaultQueue
Jobs that must be explicitly requested, i.e.
$wgConf
$wgConf hold the site configuration.
$wgJobClasses
Maps jobs to their handlers; extensions can add to this to provide custom jobs.
string[] $wgLocalDatabases
Other wikis on this site, can be administered from a single developer account.
wfConfiguredReadOnlyReason()
Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
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.
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)
string bool $readOnlyReason
Read only rationale (or false if r/w)
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 $blacklist=[])
Pop a job off one of the job queues.
ack(RunnableJob $job)
Acknowledge that a job was completed.
static destroySingletons()
Destroy the singleton instances.
factoryJobQueue(array $conf)
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.
__construct( $domain, $readOnlyReason)
static JobQueueGroup[] $instances
getQueuesWithJobs()
Get the list of job types that have non-empty queues.
assertValidJobs(array $jobs)
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.
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