5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 use MediaWiki\MediaWikiServices
;
25 * Class to handle enqueueing of background jobs
31 /** @var JobQueueGroup[] */
32 protected static $instances = [];
34 /** @var ProcessCacheLRU */
37 /** @var string Wiki domain ID */
39 /** @var string|bool Read only rationale (or false if r/w) */
40 protected $readOnlyReason;
41 /** @var bool Whether the wiki is not recognized in configuration */
42 protected $invalidDomain = false;
44 /** @var array Map of (bucket => (queue => JobQueue, types => list of types) */
45 protected $coalescedQueues;
47 const TYPE_DEFAULT
= 1; // integer; jobs popped by default
48 const TYPE_ANY
= 2; // integer; any job
50 const USE_CACHE
= 1; // integer; use process or persistent cache
52 const PROC_CACHE_TTL
= 15; // integer; seconds
54 const CACHE_VERSION
= 1; // integer; cache version
57 * @param string $domain Wiki domain ID
58 * @param string|bool $readOnlyReason Read-only reason or false
60 protected function __construct( $domain, $readOnlyReason ) {
61 $this->domain
= $domain;
62 $this->readOnlyReason
= $readOnlyReason;
63 $this->cache
= new MapCacheLRU( 10 );
67 * @param bool|string $domain Wiki domain ID
68 * @return JobQueueGroup
70 public static function singleton( $domain = false ) {
71 global $wgLocalDatabases;
73 if ( $domain === false ) {
74 $domain = WikiMap
::getCurrentWikiDbDomain()->getId();
77 if ( !isset( self
::$instances[$domain] ) ) {
78 self
::$instances[$domain] = new self( $domain, wfConfiguredReadOnlyReason() );
79 // Make sure jobs are not getting pushed to bogus wikis. This can confuse
80 // the job runner system into spawning endless RPC requests that fail (T171371).
81 $wikiId = WikiMap
::getWikiIdFromDbDomain( $domain );
83 !WikiMap
::isCurrentWikiDbDomain( $domain ) &&
84 !in_array( $wikiId, $wgLocalDatabases )
86 self
::$instances[$domain]->invalidDomain
= true;
90 return self
::$instances[$domain];
94 * Destroy the singleton instances
98 public static function destroySingletons() {
99 self
::$instances = [];
103 * Get the job queue object for a given queue type
105 * @param string $type
108 public function get( $type ) {
109 global $wgJobTypeConf;
111 $conf = [ 'domain' => $this->domain
, 'type' => $type ];
112 if ( isset( $wgJobTypeConf[$type] ) ) {
113 $conf = $conf +
$wgJobTypeConf[$type];
115 $conf = $conf +
$wgJobTypeConf['default'];
117 if ( !isset( $conf['readOnlyReason'] ) ) {
118 $conf['readOnlyReason'] = $this->readOnlyReason
;
121 $services = MediaWikiServices
::getInstance();
122 $conf['stats'] = $services->getStatsdDataFactory();
123 $conf['wanCache'] = $services->getMainWANObjectCache();
124 $conf['stash'] = $services->getMainObjectStash();
126 return JobQueue
::factory( $conf );
130 * Insert jobs into the respective queues of which they belong
132 * This inserts the jobs into the queue specified by $wgJobTypeConf
133 * and updates the aggregate job queue information cache as needed.
135 * @param IJobSpecification|IJobSpecification[] $jobs A single Job or a list of Jobs
136 * @throws InvalidArgumentException
139 public function push( $jobs ) {
140 global $wgJobTypesExcludedFromDefaultQueue;
142 if ( $this->invalidDomain
) {
143 // Do not enqueue job that cannot be run (T171371)
144 $e = new LogicException( "Domain '{$this->domain}' is not recognized." );
145 MWExceptionHandler
::logException( $e );
149 $jobs = is_array( $jobs ) ?
$jobs : [ $jobs ];
150 if ( $jobs === [] ) {
154 $this->assertValidJobs( $jobs );
156 $jobsByType = []; // (job type => list of jobs)
157 foreach ( $jobs as $job ) {
158 $jobsByType[$job->getType()][] = $job;
161 foreach ( $jobsByType as $type => $jobs ) {
162 $this->get( $type )->push( $jobs );
165 if ( $this->cache
->hasField( 'queues-ready', 'list' ) ) {
166 $list = $this->cache
->getField( 'queues-ready', 'list' );
167 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
168 $this->cache
->clear( 'queues-ready' );
172 $cache = ObjectCache
::getLocalClusterInstance();
174 $cache->makeGlobalKey( 'jobqueue', $this->domain
, 'hasjobs', self
::TYPE_ANY
),
178 if ( array_diff( array_keys( $jobsByType ), $wgJobTypesExcludedFromDefaultQueue ) ) {
180 $cache->makeGlobalKey( 'jobqueue', $this->domain
, 'hasjobs', self
::TYPE_DEFAULT
),
188 * Buffer jobs for insertion via push() or call it now if in CLI mode
190 * Note that pushLazyJobs() is registered as a deferred update just before
191 * DeferredUpdates::doUpdates() in MediaWiki and JobRunner classes in order
192 * to be executed as the very last deferred update (T100085, T154425).
194 * @param IJobSpecification|IJobSpecification[] $jobs A single Job or a list of Jobs
198 public function lazyPush( $jobs ) {
199 if ( $this->invalidDomain
) {
200 // Do not enqueue job that cannot be run (T171371)
201 throw new LogicException( "Domain '{$this->domain}' is not recognized." );
204 if ( PHP_SAPI
=== 'cli' || PHP_SAPI
=== 'phpdbg' ) {
205 $this->push( $jobs );
209 $jobs = is_array( $jobs ) ?
$jobs : [ $jobs ];
211 // Throw errors now instead of on push(), when other jobs may be buffered
212 $this->assertValidJobs( $jobs );
214 DeferredUpdates
::addUpdate( new JobQueueEnqueueUpdate( $this->domain
, $jobs ) );
218 * Push all jobs buffered via lazyPush() into their respective queues
222 * @deprecated Since 1.33 Not needed anymore
224 public static function pushLazyJobs() {
225 wfDeprecated( __METHOD__
, '1.33' );
229 * Pop a job off one of the job queues
231 * This pops a job off a queue as specified by $wgJobTypeConf and
232 * updates the aggregate job queue information cache as needed.
234 * @param int|string $qtype JobQueueGroup::TYPE_* constant or job type string
235 * @param int $flags Bitfield of JobQueueGroup::USE_* constants
236 * @param array $blacklist List of job types to ignore
237 * @return Job|bool Returns false on failure
239 public function pop( $qtype = self
::TYPE_DEFAULT
, $flags = 0, array $blacklist = [] ) {
240 global $wgJobClasses;
244 if ( !WikiMap
::isCurrentWikiDbDomain( $this->domain
) ) {
245 throw new JobQueueError(
246 "Cannot pop '{$qtype}' job off foreign '{$this->domain}' wiki queue." );
247 } elseif ( is_string( $qtype ) && !isset( $wgJobClasses[$qtype] ) ) {
248 // Do not pop jobs if there is no class for the queue type
249 throw new JobQueueError( "Unrecognized job type '$qtype'." );
252 if ( is_string( $qtype ) ) { // specific job type
253 if ( !in_array( $qtype, $blacklist ) ) {
254 $job = $this->get( $qtype )->pop();
256 } else { // any job in the "default" jobs types
257 if ( $flags & self
::USE_CACHE
) {
258 if ( !$this->cache
->hasField( 'queues-ready', 'list', self
::PROC_CACHE_TTL
) ) {
259 $this->cache
->setField( 'queues-ready', 'list', $this->getQueuesWithJobs() );
261 $types = $this->cache
->getField( 'queues-ready', 'list' );
263 $types = $this->getQueuesWithJobs();
266 if ( $qtype == self
::TYPE_DEFAULT
) {
267 $types = array_intersect( $types, $this->getDefaultQueueTypes() );
270 $types = array_diff( $types, $blacklist ); // avoid selected types
271 shuffle( $types ); // avoid starvation
273 foreach ( $types as $type ) { // for each queue...
274 $job = $this->get( $type )->pop();
275 if ( $job ) { // found
277 } else { // not found
278 $this->cache
->clear( 'queues-ready' );
287 * Acknowledge that a job was completed
292 public function ack( Job
$job ) {
293 $this->get( $job->getType() )->ack( $job );
297 * Register the "root job" of a given job into the queue for de-duplication.
298 * This should only be called right *after* all the new jobs have been inserted.
303 public function deduplicateRootJob( Job
$job ) {
304 return $this->get( $job->getType() )->deduplicateRootJob( $job );
308 * Wait for any replica DBs or backup queue servers to catch up.
310 * This does nothing for certain queue classes.
314 public function waitForBackups() {
315 global $wgJobTypeConf;
317 // Try to avoid doing this more than once per queue storage medium
318 foreach ( $wgJobTypeConf as $type => $conf ) {
319 $this->get( $type )->waitForBackups();
324 * Get the list of queue types
326 * @return array List of strings
328 public function getQueueTypes() {
329 return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) );
333 * Get the list of default queue types
335 * @return array List of strings
337 public function getDefaultQueueTypes() {
338 global $wgJobTypesExcludedFromDefaultQueue;
340 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
344 * Check if there are any queues with jobs (this is cached)
346 * @param int $type JobQueueGroup::TYPE_* constant
350 public function queuesHaveJobs( $type = self
::TYPE_ANY
) {
351 $cache = ObjectCache
::getLocalClusterInstance();
352 $key = $cache->makeGlobalKey( 'jobqueue', $this->domain
, 'hasjobs', $type );
354 $value = $cache->get( $key );
355 if ( $value === false ) {
356 $queues = $this->getQueuesWithJobs();
357 if ( $type == self
::TYPE_DEFAULT
) {
358 $queues = array_intersect( $queues, $this->getDefaultQueueTypes() );
360 $value = count( $queues ) ?
'true' : 'false';
361 $cache->add( $key, $value, 15 );
364 return ( $value === 'true' );
368 * Get the list of job types that have non-empty queues
370 * @return string[] List of job types that have non-empty queues
372 public function getQueuesWithJobs() {
374 foreach ( $this->getCoalescedQueues() as $info ) {
375 /** @var JobQueue $queue */
376 $queue = $info['queue'];
377 $nonEmpty = $queue->getSiblingQueuesWithJobs( $this->getQueueTypes() );
378 if ( is_array( $nonEmpty ) ) { // batching features supported
379 $types = array_merge( $types, $nonEmpty );
380 } else { // we have to go through the queues in the bucket one-by-one
381 foreach ( $info['types'] as $type ) {
382 if ( !$this->get( $type )->isEmpty() ) {
393 * Get the size of the queus for a list of job types
395 * @return int[] Map of (job type => size)
397 public function getQueueSizes() {
399 foreach ( $this->getCoalescedQueues() as $info ) {
400 /** @var JobQueue $queue */
401 $queue = $info['queue'];
402 $sizes = $queue->getSiblingQueueSizes( $this->getQueueTypes() );
403 if ( is_array( $sizes ) ) { // batching features supported
404 $sizeMap = $sizeMap +
$sizes;
405 } else { // we have to go through the queues in the bucket one-by-one
406 foreach ( $info['types'] as $type ) {
407 $sizeMap[$type] = $this->get( $type )->getSize();
418 protected function getCoalescedQueues() {
419 global $wgJobTypeConf;
421 if ( $this->coalescedQueues
=== null ) {
422 $this->coalescedQueues
= [];
423 foreach ( $wgJobTypeConf as $type => $conf ) {
424 $queue = JobQueue
::factory(
425 [ 'domain' => $this->domain
, 'type' => 'null' ] +
$conf );
426 $loc = $queue->getCoalesceLocationInternal();
427 if ( !isset( $this->coalescedQueues
[$loc] ) ) {
428 $this->coalescedQueues
[$loc]['queue'] = $queue;
429 $this->coalescedQueues
[$loc]['types'] = [];
431 if ( $type === 'default' ) {
432 $this->coalescedQueues
[$loc]['types'] = array_merge(
433 $this->coalescedQueues
[$loc]['types'],
434 array_diff( $this->getQueueTypes(), array_keys( $wgJobTypeConf ) )
437 $this->coalescedQueues
[$loc]['types'][] = $type;
442 return $this->coalescedQueues
;
446 * @param string $name
449 private function getCachedConfigVar( $name ) {
450 // @TODO: cleanup this whole method with a proper config system
451 if ( WikiMap
::isCurrentWikiDbDomain( $this->domain
) ) {
452 return $GLOBALS[$name]; // common case
454 $wiki = WikiMap
::getWikiIdFromDbDomain( $this->domain
);
455 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
456 $value = $cache->getWithSetCallback(
457 $cache->makeGlobalKey( 'jobqueue', 'configvalue', $this->domain
, $name ),
458 $cache::TTL_DAY +
mt_rand( 0, $cache::TTL_DAY
),
459 function () use ( $wiki, $name ) {
461 // @TODO: use the full domain ID here
462 return [ 'v' => $wgConf->getConfig( $wiki, $name ) ];
464 [ 'pcTTL' => WANObjectCache
::TTL_PROC_LONG
]
473 * @throws InvalidArgumentException
475 private function assertValidJobs( array $jobs ) {
476 foreach ( $jobs as $job ) { // sanity checks
477 if ( !( $job instanceof IJobSpecification
) ) {
478 throw new InvalidArgumentException( "Expected IJobSpecification objects" );