2012-08-29 00:01:31 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Job queue base code.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2021-03-17 12:37:06 +00:00
|
|
|
|
2018-10-13 07:29:23 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-03-17 12:37:06 +00:00
|
|
|
use Wikimedia\UUID\GlobalIdGenerator;
|
2012-08-29 00:01:31 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class to handle enqueueing of background jobs
|
|
|
|
|
*
|
|
|
|
|
* @ingroup JobQueue
|
2012-11-06 00:43:44 +00:00
|
|
|
* @since 1.21
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
|
|
|
|
class JobQueueGroup {
|
2021-03-17 12:37:06 +00:00
|
|
|
/**
|
|
|
|
|
* @var JobQueueGroup[]
|
2022-03-06 02:55:50 +00:00
|
|
|
* @deprecated since 1.37
|
2021-03-17 12:37:06 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
protected static $instances = [];
|
2012-08-29 00:01:31 +00:00
|
|
|
|
2019-06-05 18:32:05 +00:00
|
|
|
/** @var MapCacheLRU */
|
2013-01-14 22:06:11 +00:00
|
|
|
protected $cache;
|
|
|
|
|
|
2018-10-13 07:29:23 +00:00
|
|
|
/** @var string Wiki domain ID */
|
|
|
|
|
protected $domain;
|
2021-03-17 12:37:06 +00:00
|
|
|
/** @var ConfiguredReadOnlyMode Read only mode */
|
|
|
|
|
protected $readOnlyMode;
|
2017-08-10 00:13:49 +00:00
|
|
|
/** @var bool Whether the wiki is not recognized in configuration */
|
2018-10-13 07:29:23 +00:00
|
|
|
protected $invalidDomain = false;
|
2021-03-17 12:37:06 +00:00
|
|
|
/** @var array */
|
|
|
|
|
private $jobClasses;
|
|
|
|
|
/** @var array */
|
|
|
|
|
private $jobTypeConfiguration;
|
|
|
|
|
/** @var array */
|
|
|
|
|
private $jobTypesExcludedFromDefaultQueue;
|
|
|
|
|
/** @var IBufferingStatsdDataFactory */
|
|
|
|
|
private $statsdDataFactory;
|
|
|
|
|
/** @var WANObjectCache */
|
|
|
|
|
private $wanCache;
|
|
|
|
|
/** @var GlobalIdGenerator */
|
|
|
|
|
private $globalIdGenerator;
|
2012-08-29 00:01:31 +00:00
|
|
|
|
2013-07-04 07:05:19 +00:00
|
|
|
/** @var array Map of (bucket => (queue => JobQueue, types => list of types) */
|
|
|
|
|
protected $coalescedQueues;
|
|
|
|
|
|
2020-05-12 22:37:45 +00:00
|
|
|
public const TYPE_DEFAULT = 1; // integer; jobs popped by default
|
|
|
|
|
private const TYPE_ANY = 2; // integer; any job
|
2012-08-29 00:01:31 +00:00
|
|
|
|
2020-05-12 22:37:45 +00:00
|
|
|
public const USE_CACHE = 1; // integer; use process or persistent cache
|
2013-01-14 22:06:11 +00:00
|
|
|
|
2020-05-12 22:37:45 +00:00
|
|
|
private const PROC_CACHE_TTL = 15; // integer; seconds
|
2013-01-14 22:06:11 +00:00
|
|
|
|
2020-05-12 22:37:45 +00:00
|
|
|
private const CACHE_VERSION = 1; // integer; cache version
|
2013-02-05 20:00:24 +00:00
|
|
|
|
2012-08-29 00:01:31 +00:00
|
|
|
/**
|
2021-03-17 12:37:06 +00:00
|
|
|
* @internal Use MediaWikiServices::getJobQueueGroupFactory
|
|
|
|
|
*
|
2018-10-13 07:29:23 +00:00
|
|
|
* @param string $domain Wiki domain ID
|
2021-03-17 12:37:06 +00:00
|
|
|
* @param ConfiguredReadOnlyMode $readOnlyMode Read-only mode
|
|
|
|
|
* @param bool $invalidDomain Whether the wiki is not recognized in configuration
|
|
|
|
|
* @param array $jobClasses
|
|
|
|
|
* @param array $jobTypeConfiguration
|
|
|
|
|
* @param array $jobTypesExcludedFromDefaultQueue
|
|
|
|
|
* @param IBufferingStatsdDataFactory $statsdDataFactory
|
|
|
|
|
* @param WANObjectCache $wanCache
|
|
|
|
|
* @param GlobalIdGenerator $globalIdGenerator
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
2021-03-17 12:37:06 +00:00
|
|
|
public function __construct(
|
|
|
|
|
$domain,
|
|
|
|
|
ConfiguredReadOnlyMode $readOnlyMode,
|
|
|
|
|
bool $invalidDomain,
|
|
|
|
|
array $jobClasses,
|
|
|
|
|
array $jobTypeConfiguration,
|
|
|
|
|
array $jobTypesExcludedFromDefaultQueue,
|
|
|
|
|
IBufferingStatsdDataFactory $statsdDataFactory,
|
|
|
|
|
WANObjectCache $wanCache,
|
|
|
|
|
GlobalIdGenerator $globalIdGenerator
|
|
|
|
|
) {
|
2018-10-13 07:29:23 +00:00
|
|
|
$this->domain = $domain;
|
2021-03-17 12:37:06 +00:00
|
|
|
$this->readOnlyMode = $readOnlyMode;
|
2018-07-11 12:54:51 +00:00
|
|
|
$this->cache = new MapCacheLRU( 10 );
|
2021-03-17 12:37:06 +00:00
|
|
|
$this->invalidDomain = $invalidDomain;
|
|
|
|
|
$this->jobClasses = $jobClasses;
|
|
|
|
|
$this->jobTypeConfiguration = $jobTypeConfiguration;
|
|
|
|
|
$this->jobTypesExcludedFromDefaultQueue = $jobTypesExcludedFromDefaultQueue;
|
|
|
|
|
$this->statsdDataFactory = $statsdDataFactory;
|
|
|
|
|
$this->wanCache = $wanCache;
|
|
|
|
|
$this->globalIdGenerator = $globalIdGenerator;
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-06-24 16:48:31 +00:00
|
|
|
* @deprecated since 1.37 Use JobQueueGroupFactory::makeJobQueueGroup (hard deprecated since 1.39)
|
2018-10-13 07:29:23 +00:00
|
|
|
* @param bool|string $domain Wiki domain ID
|
2012-08-29 00:01:31 +00:00
|
|
|
* @return JobQueueGroup
|
|
|
|
|
*/
|
2018-10-13 07:29:23 +00:00
|
|
|
public static function singleton( $domain = false ) {
|
2022-06-24 16:48:31 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.37' );
|
2021-03-17 12:37:06 +00:00
|
|
|
return MediaWikiServices::getInstance()->getJobQueueGroupFactory()->makeJobQueueGroup( $domain );
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
2013-01-14 22:06:11 +00:00
|
|
|
/**
|
|
|
|
|
* Destroy the singleton instances
|
|
|
|
|
*
|
2022-06-24 16:48:31 +00:00
|
|
|
* @deprecated since 1.37 (hard deprecated since 1.39)
|
2013-01-14 22:06:11 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function destroySingletons() {
|
2022-06-24 16:48:31 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.37' );
|
2013-01-14 22:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
2012-08-29 00:01:31 +00:00
|
|
|
/**
|
2013-02-21 19:45:51 +00:00
|
|
|
* Get the job queue object for a given queue type
|
|
|
|
|
*
|
2013-11-25 17:12:42 +00:00
|
|
|
* @param string $type
|
2013-02-21 19:45:51 +00:00
|
|
|
* @return JobQueue
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
|
|
|
|
public function get( $type ) {
|
2018-10-13 07:29:23 +00:00
|
|
|
$conf = [ 'domain' => $this->domain, 'type' => $type ];
|
2022-04-04 09:57:04 +00:00
|
|
|
$conf += $this->jobTypeConfiguration[$type] ?? $this->jobTypeConfiguration['default'];
|
2018-07-19 16:13:38 +00:00
|
|
|
if ( !isset( $conf['readOnlyReason'] ) ) {
|
2021-03-17 12:37:06 +00:00
|
|
|
$conf['readOnlyReason'] = $this->readOnlyMode->getReason();
|
2016-03-25 19:52:39 +00:00
|
|
|
}
|
2012-08-29 00:01:31 +00:00
|
|
|
|
2021-03-17 12:37:06 +00:00
|
|
|
$conf['stats'] = $this->statsdDataFactory;
|
|
|
|
|
$conf['wanCache'] = $this->wanCache;
|
|
|
|
|
$conf['idGenerator'] = $this->globalIdGenerator;
|
2019-03-30 04:41:34 +00:00
|
|
|
|
2012-10-24 17:14:54 +00:00
|
|
|
return JobQueue::factory( $conf );
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-05-15 23:23:51 +00:00
|
|
|
* Insert jobs into the respective queues of which they belong
|
2013-02-21 19:45:51 +00:00
|
|
|
*
|
|
|
|
|
* This inserts the jobs into the queue specified by $wgJobTypeConf
|
|
|
|
|
* and updates the aggregate job queue information cache as needed.
|
2012-08-29 00:01:31 +00:00
|
|
|
*
|
2015-05-15 23:23:51 +00:00
|
|
|
* @param IJobSpecification|IJobSpecification[] $jobs A single Job or a list of Jobs
|
|
|
|
|
* @throws InvalidArgumentException
|
2014-04-16 18:07:26 +00:00
|
|
|
* @return void
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
|
|
|
|
public function push( $jobs ) {
|
2018-10-13 07:29:23 +00:00
|
|
|
if ( $this->invalidDomain ) {
|
2017-08-10 00:13:49 +00:00
|
|
|
// Do not enqueue job that cannot be run (T171371)
|
2018-10-13 07:29:23 +00:00
|
|
|
$e = new LogicException( "Domain '{$this->domain}' is not recognized." );
|
2017-08-10 00:13:49 +00:00
|
|
|
MWExceptionHandler::logException( $e );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
|
2019-01-09 16:24:36 +00:00
|
|
|
if ( $jobs === [] ) {
|
2014-04-16 18:07:26 +00:00
|
|
|
return;
|
2013-11-19 00:12:12 +00:00
|
|
|
}
|
2012-08-29 00:01:31 +00:00
|
|
|
|
2015-05-15 23:23:51 +00:00
|
|
|
$this->assertValidJobs( $jobs );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$jobsByType = []; // (job type => list of jobs)
|
2012-08-29 00:01:31 +00:00
|
|
|
foreach ( $jobs as $job ) {
|
2021-09-29 21:03:50 +00:00
|
|
|
$type = $job->getType();
|
|
|
|
|
if ( isset( $this->jobTypeConfiguration[$type] ) ) {
|
|
|
|
|
$jobsByType[$type][] = $job;
|
|
|
|
|
} else {
|
|
|
|
|
if (
|
|
|
|
|
isset( $this->jobTypeConfiguration['default']['typeAgnostic'] ) &&
|
|
|
|
|
$this->jobTypeConfiguration['default']['typeAgnostic']
|
|
|
|
|
) {
|
|
|
|
|
$jobsByType['default'][] = $job;
|
|
|
|
|
} else {
|
|
|
|
|
$jobsByType[$type][] = $job;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( $jobsByType as $type => $jobs ) {
|
2014-04-16 17:51:11 +00:00
|
|
|
$this->get( $type )->push( $jobs );
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
2018-07-11 12:54:51 +00:00
|
|
|
if ( $this->cache->hasField( 'queues-ready', 'list' ) ) {
|
|
|
|
|
$list = $this->cache->getField( 'queues-ready', 'list' );
|
2013-01-14 22:06:11 +00:00
|
|
|
if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
|
|
|
|
|
$this->cache->clear( 'queues-ready' );
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-11 11:57:12 +00:00
|
|
|
|
|
|
|
|
$cache = ObjectCache::getLocalClusterInstance();
|
|
|
|
|
$cache->set(
|
2018-10-13 07:29:23 +00:00
|
|
|
$cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', self::TYPE_ANY ),
|
2016-09-11 11:57:12 +00:00
|
|
|
'true',
|
|
|
|
|
15
|
|
|
|
|
);
|
2021-03-17 12:37:06 +00:00
|
|
|
if ( array_diff( array_keys( $jobsByType ), $this->jobTypesExcludedFromDefaultQueue ) ) {
|
2016-09-11 11:57:12 +00:00
|
|
|
$cache->set(
|
2018-10-13 07:29:23 +00:00
|
|
|
$cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', self::TYPE_DEFAULT ),
|
2016-09-11 11:57:12 +00:00
|
|
|
'true',
|
|
|
|
|
15
|
|
|
|
|
);
|
|
|
|
|
}
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
2015-05-15 23:23:51 +00:00
|
|
|
/**
|
|
|
|
|
* Buffer jobs for insertion via push() or call it now if in CLI mode
|
|
|
|
|
*
|
|
|
|
|
* @param IJobSpecification|IJobSpecification[] $jobs A single Job or a list of Jobs
|
|
|
|
|
* @return void
|
|
|
|
|
* @since 1.26
|
|
|
|
|
*/
|
|
|
|
|
public function lazyPush( $jobs ) {
|
2018-10-13 07:29:23 +00:00
|
|
|
if ( $this->invalidDomain ) {
|
2017-08-10 00:13:49 +00:00
|
|
|
// Do not enqueue job that cannot be run (T171371)
|
2018-10-13 07:29:23 +00:00
|
|
|
throw new LogicException( "Domain '{$this->domain}' is not recognized." );
|
2017-08-10 00:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
2017-04-10 11:32:15 +00:00
|
|
|
if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
|
2015-05-15 23:23:51 +00:00
|
|
|
$this->push( $jobs );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
|
2015-05-15 23:23:51 +00:00
|
|
|
|
|
|
|
|
// Throw errors now instead of on push(), when other jobs may be buffered
|
|
|
|
|
$this->assertValidJobs( $jobs );
|
|
|
|
|
|
2018-10-13 07:29:23 +00:00
|
|
|
DeferredUpdates::addUpdate( new JobQueueEnqueueUpdate( $this->domain, $jobs ) );
|
2015-05-15 23:23:51 +00:00
|
|
|
}
|
|
|
|
|
|
2012-08-29 00:01:31 +00:00
|
|
|
/**
|
|
|
|
|
* Pop a job off one of the job queues
|
|
|
|
|
*
|
2013-02-21 19:45:51 +00:00
|
|
|
* This pops a job off a queue as specified by $wgJobTypeConf and
|
|
|
|
|
* updates the aggregate job queue information cache as needed.
|
|
|
|
|
*
|
2013-12-21 20:57:45 +00:00
|
|
|
* @param int|string $qtype JobQueueGroup::TYPE_* constant or job type string
|
2013-11-25 17:12:42 +00:00
|
|
|
* @param int $flags Bitfield of JobQueueGroup::USE_* constants
|
2021-03-19 16:12:50 +00:00
|
|
|
* @param array $ignored List of job types to ignore
|
2019-03-30 06:07:48 +00:00
|
|
|
* @return RunnableJob|bool Returns false on failure
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
2021-03-19 16:12:50 +00:00
|
|
|
public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $ignored = [] ) {
|
2013-12-21 20:57:45 +00:00
|
|
|
$job = false;
|
|
|
|
|
|
2019-03-30 04:41:34 +00:00
|
|
|
if ( !WikiMap::isCurrentWikiDbDomain( $this->domain ) ) {
|
|
|
|
|
throw new JobQueueError(
|
|
|
|
|
"Cannot pop '{$qtype}' job off foreign '{$this->domain}' wiki queue." );
|
2021-03-17 12:37:06 +00:00
|
|
|
} elseif ( is_string( $qtype ) && !isset( $this->jobClasses[$qtype] ) ) {
|
2019-03-30 04:41:34 +00:00
|
|
|
// Do not pop jobs if there is no class for the queue type
|
|
|
|
|
throw new JobQueueError( "Unrecognized job type '$qtype'." );
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-05 20:00:24 +00:00
|
|
|
if ( is_string( $qtype ) ) { // specific job type
|
2021-03-19 16:12:50 +00:00
|
|
|
if ( !in_array( $qtype, $ignored ) ) {
|
2013-12-21 20:57:45 +00:00
|
|
|
$job = $this->get( $qtype )->pop();
|
2013-02-05 20:00:24 +00:00
|
|
|
}
|
|
|
|
|
} else { // any job in the "default" jobs types
|
|
|
|
|
if ( $flags & self::USE_CACHE ) {
|
2018-07-11 12:54:51 +00:00
|
|
|
if ( !$this->cache->hasField( 'queues-ready', 'list', self::PROC_CACHE_TTL ) ) {
|
|
|
|
|
$this->cache->setField( 'queues-ready', 'list', $this->getQueuesWithJobs() );
|
2013-02-05 20:00:24 +00:00
|
|
|
}
|
2018-07-11 12:54:51 +00:00
|
|
|
$types = $this->cache->getField( 'queues-ready', 'list' );
|
2013-02-05 20:00:24 +00:00
|
|
|
} else {
|
|
|
|
|
$types = $this->getQueuesWithJobs();
|
2013-01-14 22:06:11 +00:00
|
|
|
}
|
2012-08-29 00:01:31 +00:00
|
|
|
|
2013-02-05 20:00:24 +00:00
|
|
|
if ( $qtype == self::TYPE_DEFAULT ) {
|
|
|
|
|
$types = array_intersect( $types, $this->getDefaultQueueTypes() );
|
|
|
|
|
}
|
2013-12-21 20:57:45 +00:00
|
|
|
|
2021-03-19 16:12:50 +00:00
|
|
|
$types = array_diff( $types, $ignored ); // avoid selected types
|
2013-02-05 20:00:24 +00:00
|
|
|
shuffle( $types ); // avoid starvation
|
|
|
|
|
|
|
|
|
|
foreach ( $types as $type ) { // for each queue...
|
|
|
|
|
$job = $this->get( $type )->pop();
|
|
|
|
|
if ( $job ) { // found
|
2013-12-21 20:57:45 +00:00
|
|
|
break;
|
2013-02-05 20:00:24 +00:00
|
|
|
} else { // not found
|
|
|
|
|
$this->cache->clear( 'queues-ready' );
|
|
|
|
|
}
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|
2013-02-05 20:00:24 +00:00
|
|
|
}
|
2013-12-21 20:57:45 +00:00
|
|
|
|
|
|
|
|
return $job;
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Acknowledge that a job was completed
|
|
|
|
|
*
|
2019-07-05 20:20:56 +00:00
|
|
|
* @param RunnableJob $job
|
2015-05-15 23:23:51 +00:00
|
|
|
* @return void
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
2019-07-05 20:20:56 +00:00
|
|
|
public function ack( RunnableJob $job ) {
|
2015-05-15 23:23:51 +00:00
|
|
|
$this->get( $job->getType() )->ack( $job );
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
2012-11-08 22:01:40 +00:00
|
|
|
/**
|
|
|
|
|
* Register the "root job" of a given job into the queue for de-duplication.
|
|
|
|
|
* This should only be called right *after* all the new jobs have been inserted.
|
|
|
|
|
*
|
2019-07-05 20:20:56 +00:00
|
|
|
* @param RunnableJob $job
|
2012-11-08 22:01:40 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2019-07-05 20:20:56 +00:00
|
|
|
public function deduplicateRootJob( RunnableJob $job ) {
|
2012-11-08 22:01:40 +00:00
|
|
|
return $this->get( $job->getType() )->deduplicateRootJob( $job );
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-12 01:56:16 +00:00
|
|
|
/**
|
2016-09-05 20:21:26 +00:00
|
|
|
* Wait for any replica DBs or backup queue servers to catch up.
|
2013-03-12 01:56:16 +00:00
|
|
|
*
|
|
|
|
|
* This does nothing for certain queue classes.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function waitForBackups() {
|
|
|
|
|
// Try to avoid doing this more than once per queue storage medium
|
2021-03-17 12:37:06 +00:00
|
|
|
foreach ( $this->jobTypeConfiguration as $type => $conf ) {
|
2013-03-12 01:56:16 +00:00
|
|
|
$this->get( $type )->waitForBackups();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-29 00:01:31 +00:00
|
|
|
/**
|
|
|
|
|
* Get the list of queue types
|
|
|
|
|
*
|
2020-10-28 10:01:33 +00:00
|
|
|
* @return string[]
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
|
|
|
|
public function getQueueTypes() {
|
2013-02-04 00:28:52 +00:00
|
|
|
return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) );
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the list of default queue types
|
|
|
|
|
*
|
2020-10-28 10:01:33 +00:00
|
|
|
* @return string[]
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
|
|
|
|
public function getDefaultQueueTypes() {
|
2021-03-17 12:37:06 +00:00
|
|
|
return array_diff( $this->getQueueTypes(), $this->jobTypesExcludedFromDefaultQueue );
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|
2012-11-03 00:31:25 +00:00
|
|
|
|
2014-04-08 16:34:49 +00:00
|
|
|
/**
|
|
|
|
|
* Check if there are any queues with jobs (this is cached)
|
|
|
|
|
*
|
2014-04-20 21:33:05 +00:00
|
|
|
* @param int $type JobQueueGroup::TYPE_* constant
|
2014-04-08 16:34:49 +00:00
|
|
|
* @return bool
|
|
|
|
|
* @since 1.23
|
|
|
|
|
*/
|
|
|
|
|
public function queuesHaveJobs( $type = self::TYPE_ANY ) {
|
2015-10-26 07:41:05 +00:00
|
|
|
$cache = ObjectCache::getLocalClusterInstance();
|
2018-10-13 07:29:23 +00:00
|
|
|
$key = $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', $type );
|
2014-04-08 16:34:49 +00:00
|
|
|
|
2015-10-26 07:41:05 +00:00
|
|
|
$value = $cache->get( $key );
|
2014-04-08 16:34:49 +00:00
|
|
|
if ( $value === false ) {
|
|
|
|
|
$queues = $this->getQueuesWithJobs();
|
|
|
|
|
if ( $type == self::TYPE_DEFAULT ) {
|
|
|
|
|
$queues = array_intersect( $queues, $this->getDefaultQueueTypes() );
|
|
|
|
|
}
|
|
|
|
|
$value = count( $queues ) ? 'true' : 'false';
|
2015-10-26 07:41:05 +00:00
|
|
|
$cache->add( $key, $value, 15 );
|
2014-04-08 16:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ( $value === 'true' );
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-03 00:31:25 +00:00
|
|
|
/**
|
2013-02-05 20:00:24 +00:00
|
|
|
* Get the list of job types that have non-empty queues
|
|
|
|
|
*
|
2019-03-30 04:11:35 +00:00
|
|
|
* @return string[] List of job types that have non-empty queues
|
2012-11-03 00:31:25 +00:00
|
|
|
*/
|
|
|
|
|
public function getQueuesWithJobs() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$types = [];
|
2013-07-04 07:05:19 +00:00
|
|
|
foreach ( $this->getCoalescedQueues() as $info ) {
|
2019-03-30 04:11:35 +00:00
|
|
|
/** @var JobQueue $queue */
|
|
|
|
|
$queue = $info['queue'];
|
|
|
|
|
$nonEmpty = $queue->getSiblingQueuesWithJobs( $this->getQueueTypes() );
|
2013-07-04 07:05:19 +00:00
|
|
|
if ( is_array( $nonEmpty ) ) { // batching features supported
|
|
|
|
|
$types = array_merge( $types, $nonEmpty );
|
|
|
|
|
} else { // we have to go through the queues in the bucket one-by-one
|
|
|
|
|
foreach ( $info['types'] as $type ) {
|
|
|
|
|
if ( !$this->get( $type )->isEmpty() ) {
|
|
|
|
|
$types[] = $type;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-11-03 00:31:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2012-11-03 00:31:25 +00:00
|
|
|
return $types;
|
|
|
|
|
}
|
2013-01-14 22:06:11 +00:00
|
|
|
|
2013-07-04 07:05:19 +00:00
|
|
|
/**
|
2020-04-09 06:53:40 +00:00
|
|
|
* Get the size of the queues for a list of job types
|
2013-07-04 07:05:19 +00:00
|
|
|
*
|
2019-03-30 04:11:35 +00:00
|
|
|
* @return int[] Map of (job type => size)
|
2013-07-04 07:05:19 +00:00
|
|
|
*/
|
|
|
|
|
public function getQueueSizes() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$sizeMap = [];
|
2013-07-04 07:05:19 +00:00
|
|
|
foreach ( $this->getCoalescedQueues() as $info ) {
|
2019-03-30 04:11:35 +00:00
|
|
|
/** @var JobQueue $queue */
|
|
|
|
|
$queue = $info['queue'];
|
|
|
|
|
$sizes = $queue->getSiblingQueueSizes( $this->getQueueTypes() );
|
2013-07-04 07:05:19 +00:00
|
|
|
if ( is_array( $sizes ) ) { // batching features supported
|
2020-07-31 19:41:28 +00:00
|
|
|
$sizeMap += $sizes;
|
2013-07-04 07:05:19 +00:00
|
|
|
} else { // we have to go through the queues in the bucket one-by-one
|
|
|
|
|
foreach ( $info['types'] as $type ) {
|
|
|
|
|
$sizeMap[$type] = $this->get( $type )->getSize();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2013-07-04 07:05:19 +00:00
|
|
|
return $sizeMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-08-30 17:56:27 +00:00
|
|
|
* @return array[]
|
|
|
|
|
* @phan-return array<string,array{queue:JobQueue,types:array<string,class-string>}>
|
2013-07-04 07:05:19 +00:00
|
|
|
*/
|
|
|
|
|
protected function getCoalescedQueues() {
|
|
|
|
|
if ( $this->coalescedQueues === null ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->coalescedQueues = [];
|
2021-03-17 12:37:06 +00:00
|
|
|
foreach ( $this->jobTypeConfiguration as $type => $conf ) {
|
|
|
|
|
$conf['domain'] = $this->domain;
|
|
|
|
|
$conf['type'] = 'null';
|
|
|
|
|
$conf['stats'] = $this->statsdDataFactory;
|
|
|
|
|
$conf['wanCache'] = $this->wanCache;
|
|
|
|
|
$conf['idGenerator'] = $this->globalIdGenerator;
|
|
|
|
|
|
|
|
|
|
$queue = JobQueue::factory( $conf );
|
2013-07-04 07:05:19 +00:00
|
|
|
$loc = $queue->getCoalesceLocationInternal();
|
|
|
|
|
if ( !isset( $this->coalescedQueues[$loc] ) ) {
|
|
|
|
|
$this->coalescedQueues[$loc]['queue'] = $queue;
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->coalescedQueues[$loc]['types'] = [];
|
2013-07-04 07:05:19 +00:00
|
|
|
}
|
|
|
|
|
if ( $type === 'default' ) {
|
|
|
|
|
$this->coalescedQueues[$loc]['types'] = array_merge(
|
|
|
|
|
$this->coalescedQueues[$loc]['types'],
|
2021-03-17 12:37:06 +00:00
|
|
|
array_diff( $this->getQueueTypes(), array_keys( $this->jobTypeConfiguration ) )
|
2013-07-04 07:05:19 +00:00
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$this->coalescedQueues[$loc]['types'][] = $type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->coalescedQueues;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 19:45:51 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @param string $name
|
2013-02-21 19:45:51 +00:00
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2013-02-04 00:28:52 +00:00
|
|
|
private function getCachedConfigVar( $name ) {
|
2015-11-03 13:09:57 +00:00
|
|
|
// @TODO: cleanup this whole method with a proper config system
|
2019-02-06 20:28:45 +00:00
|
|
|
if ( WikiMap::isCurrentWikiDbDomain( $this->domain ) ) {
|
2013-02-04 00:28:52 +00:00
|
|
|
return $GLOBALS[$name]; // common case
|
|
|
|
|
} else {
|
2019-02-06 20:28:45 +00:00
|
|
|
$wiki = WikiMap::getWikiIdFromDbDomain( $this->domain );
|
2018-10-13 07:29:23 +00:00
|
|
|
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
|
2015-11-03 13:09:57 +00:00
|
|
|
$value = $cache->getWithSetCallback(
|
2018-10-13 07:29:23 +00:00
|
|
|
$cache->makeGlobalKey( 'jobqueue', 'configvalue', $this->domain, $name ),
|
2015-11-03 13:09:57 +00:00
|
|
|
$cache::TTL_DAY + mt_rand( 0, $cache::TTL_DAY ),
|
2021-02-10 22:31:02 +00:00
|
|
|
static function () use ( $wiki, $name ) {
|
2022-01-12 22:14:34 +00:00
|
|
|
global $wgConf;
|
2018-10-13 07:29:23 +00:00
|
|
|
// @TODO: use the full domain ID here
|
2022-01-12 22:14:34 +00:00
|
|
|
return [ 'v' => $wgConf->getConfig( $wiki, $name ) ];
|
2015-11-03 13:09:57 +00:00
|
|
|
},
|
2016-05-05 23:40:26 +00:00
|
|
|
[ 'pcTTL' => WANObjectCache::TTL_PROC_LONG ]
|
2015-11-03 13:09:57 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $value['v'];
|
2013-02-04 00:28:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
2015-05-15 23:23:51 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $jobs
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
|
|
|
|
private function assertValidJobs( array $jobs ) {
|
2021-11-19 23:19:42 +00:00
|
|
|
foreach ( $jobs as $job ) {
|
2015-05-15 23:23:51 +00:00
|
|
|
if ( !( $job instanceof IJobSpecification ) ) {
|
2021-08-19 20:00:32 +00:00
|
|
|
$type = is_object( $job ) ? get_class( $job ) : gettype( $job );
|
|
|
|
|
throw new InvalidArgumentException( "Expected IJobSpecification objects, got " . $type );
|
2015-05-15 23:23:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|