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
24 * Class to handle enqueueing of background jobs
30 /** @var JobQueueGroup[] */
31 protected static $instances = [];
33 /** @var ProcessCacheLRU */
36 /** @var string Wiki ID */
38 /** @var string|bool Read only rationale (or false if r/w) */
39 protected $readOnlyReason;
40 /** @var bool Whether the wiki is not recognized in configuration */
41 protected $invalidWiki = false;
43 /** @var array Map of (bucket => (queue => JobQueue, types => list of types) */
44 protected $coalescedQueues;
46 const TYPE_DEFAULT
= 1; // integer; jobs popped by default
47 const TYPE_ANY
= 2; // integer; any job
49 const USE_CACHE
= 1; // integer; use process or persistent cache
51 const PROC_CACHE_TTL
= 15; // integer; seconds
53 const CACHE_VERSION
= 1; // integer; cache version
56 * @param string $wiki Wiki ID
57 * @param string|bool $readOnlyReason Read-only reason or false
59 protected function __construct( $wiki, $readOnlyReason ) {
61 $this->readOnlyReason
= $readOnlyReason;
62 $this->cache
= new MapCacheLRU( 10 );
66 * @param bool|string $wiki Wiki ID
67 * @return JobQueueGroup
69 public static function singleton( $wiki = false ) {
70 global $wgLocalDatabases;
72 $wiki = ( $wiki === false ) ?
wfWikiID() : $wiki;
74 if ( !isset( self
::$instances[$wiki] ) ) {
75 self
::$instances[$wiki] = new self( $wiki, wfConfiguredReadOnlyReason() );
76 // Make sure jobs are not getting pushed to bogus wikis. This can confuse
77 // the job runner system into spawning endless RPC requests that fail (T171371).
78 if ( !WikiMap
::isCurrentWikiId( $wiki ) && !in_array( $wiki, $wgLocalDatabases ) ) {
79 self
::$instances[$wiki]->invalidWiki
= true;
83 return self
::$instances[$wiki];
87 * Destroy the singleton instances
91 public static function destroySingletons() {
92 self
::$instances = [];
96 * Get the job queue object for a given queue type
101 public function get( $type ) {
102 global $wgJobTypeConf;
104 $conf = [ 'wiki' => $this->wiki
, 'type' => $type ];
105 $conf +
= $wgJobTypeConf[$type] ??
$wgJobTypeConf['default'];
106 $conf['aggregator'] = JobQueueAggregator
::singleton();
107 if ( !isset( $conf['readOnlyReason'] ) ) {
108 $conf['readOnlyReason'] = $this->readOnlyReason
;
111 return JobQueue
::factory( $conf );
115 * Insert jobs into the respective queues of which they belong
117 * This inserts the jobs into the queue specified by $wgJobTypeConf
118 * and updates the aggregate job queue information cache as needed.
120 * @param IJobSpecification|IJobSpecification[] $jobs A single Job or a list of Jobs
121 * @throws InvalidArgumentException
124 public function push( $jobs ) {
125 global $wgJobTypesExcludedFromDefaultQueue;
127 if ( $this->invalidWiki
) {
128 // Do not enqueue job that cannot be run (T171371)
129 $e = new LogicException( "Domain '{$this->wiki}' is not recognized." );
130 MWExceptionHandler
::logException( $e );
134 $jobs = is_array( $jobs ) ?
$jobs : [ $jobs ];
135 if ( !count( $jobs ) ) {
139 $this->assertValidJobs( $jobs );
141 $jobsByType = []; // (job type => list of jobs)
142 foreach ( $jobs as $job ) {
143 $jobsByType[$job->getType()][] = $job;
146 foreach ( $jobsByType as $type => $jobs ) {
147 $this->get( $type )->push( $jobs );
150 if ( $this->cache
->hasField( 'queues-ready', 'list' ) ) {
151 $list = $this->cache
->getField( 'queues-ready', 'list' );
152 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
153 $this->cache
->clear( 'queues-ready' );
157 $cache = ObjectCache
::getLocalClusterInstance();
159 $cache->makeGlobalKey( 'jobqueue', $this->wiki
, 'hasjobs', self
::TYPE_ANY
),
163 if ( array_diff( array_keys( $jobsByType ), $wgJobTypesExcludedFromDefaultQueue ) ) {
165 $cache->makeGlobalKey( 'jobqueue', $this->wiki
, 'hasjobs', self
::TYPE_DEFAULT
),
173 * Buffer jobs for insertion via push() or call it now if in CLI mode
175 * Note that pushLazyJobs() is registered as a deferred update just before
176 * DeferredUpdates::doUpdates() in MediaWiki and JobRunner classes in order
177 * to be executed as the very last deferred update (T100085, T154425).
179 * @param IJobSpecification|IJobSpecification[] $jobs A single Job or a list of Jobs
183 public function lazyPush( $jobs ) {
184 if ( $this->invalidWiki
) {
185 // Do not enqueue job that cannot be run (T171371)
186 throw new LogicException( "Domain '{$this->wiki}' is not recognized." );
189 if ( PHP_SAPI
=== 'cli' || PHP_SAPI
=== 'phpdbg' ) {
190 $this->push( $jobs );
194 $jobs = is_array( $jobs ) ?
$jobs : [ $jobs ];
196 // Throw errors now instead of on push(), when other jobs may be buffered
197 $this->assertValidJobs( $jobs );
199 DeferredUpdates
::addUpdate( new JobQueueEnqueueUpdate( $this->wiki
, $jobs ) );
203 * Push all jobs buffered via lazyPush() into their respective queues
207 * @deprecated Since 1.33 Not needed anymore
209 public static function pushLazyJobs() {
210 wfDeprecated( __METHOD__
, '1.33' );
214 * Pop a job off one of the job queues
216 * This pops a job off a queue as specified by $wgJobTypeConf and
217 * updates the aggregate job queue information cache as needed.
219 * @param int|string $qtype JobQueueGroup::TYPE_* constant or job type string
220 * @param int $flags Bitfield of JobQueueGroup::USE_* constants
221 * @param array $blacklist List of job types to ignore
222 * @return Job|bool Returns false on failure
224 public function pop( $qtype = self
::TYPE_DEFAULT
, $flags = 0, array $blacklist = [] ) {
227 if ( is_string( $qtype ) ) { // specific job type
228 if ( !in_array( $qtype, $blacklist ) ) {
229 $job = $this->get( $qtype )->pop();
231 } else { // any job in the "default" jobs types
232 if ( $flags & self
::USE_CACHE
) {
233 if ( !$this->cache
->hasField( 'queues-ready', 'list', self
::PROC_CACHE_TTL
) ) {
234 $this->cache
->setField( 'queues-ready', 'list', $this->getQueuesWithJobs() );
236 $types = $this->cache
->getField( 'queues-ready', 'list' );
238 $types = $this->getQueuesWithJobs();
241 if ( $qtype == self
::TYPE_DEFAULT
) {
242 $types = array_intersect( $types, $this->getDefaultQueueTypes() );
245 $types = array_diff( $types, $blacklist ); // avoid selected types
246 shuffle( $types ); // avoid starvation
248 foreach ( $types as $type ) { // for each queue...
249 $job = $this->get( $type )->pop();
250 if ( $job ) { // found
252 } else { // not found
253 $this->cache
->clear( 'queues-ready' );
262 * Acknowledge that a job was completed
267 public function ack( Job
$job ) {
268 $this->get( $job->getType() )->ack( $job );
272 * Register the "root job" of a given job into the queue for de-duplication.
273 * This should only be called right *after* all the new jobs have been inserted.
278 public function deduplicateRootJob( Job
$job ) {
279 return $this->get( $job->getType() )->deduplicateRootJob( $job );
283 * Wait for any replica DBs or backup queue servers to catch up.
285 * This does nothing for certain queue classes.
289 public function waitForBackups() {
290 global $wgJobTypeConf;
292 // Try to avoid doing this more than once per queue storage medium
293 foreach ( $wgJobTypeConf as $type => $conf ) {
294 $this->get( $type )->waitForBackups();
299 * Get the list of queue types
301 * @return array List of strings
303 public function getQueueTypes() {
304 return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) );
308 * Get the list of default queue types
310 * @return array List of strings
312 public function getDefaultQueueTypes() {
313 global $wgJobTypesExcludedFromDefaultQueue;
315 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
319 * Check if there are any queues with jobs (this is cached)
321 * @param int $type JobQueueGroup::TYPE_* constant
325 public function queuesHaveJobs( $type = self
::TYPE_ANY
) {
326 $cache = ObjectCache
::getLocalClusterInstance();
327 $key = $cache->makeGlobalKey( 'jobqueue', $this->wiki
, 'hasjobs', $type );
329 $value = $cache->get( $key );
330 if ( $value === false ) {
331 $queues = $this->getQueuesWithJobs();
332 if ( $type == self
::TYPE_DEFAULT
) {
333 $queues = array_intersect( $queues, $this->getDefaultQueueTypes() );
335 $value = count( $queues ) ?
'true' : 'false';
336 $cache->add( $key, $value, 15 );
339 return ( $value === 'true' );
343 * Get the list of job types that have non-empty queues
345 * @return array List of job types that have non-empty queues
347 public function getQueuesWithJobs() {
349 foreach ( $this->getCoalescedQueues() as $info ) {
350 $nonEmpty = $info['queue']->getSiblingQueuesWithJobs( $this->getQueueTypes() );
351 if ( is_array( $nonEmpty ) ) { // batching features supported
352 $types = array_merge( $types, $nonEmpty );
353 } else { // we have to go through the queues in the bucket one-by-one
354 foreach ( $info['types'] as $type ) {
355 if ( !$this->get( $type )->isEmpty() ) {
366 * Get the size of the queus for a list of job types
368 * @return array Map of (job type => size)
370 public function getQueueSizes() {
372 foreach ( $this->getCoalescedQueues() as $info ) {
373 $sizes = $info['queue']->getSiblingQueueSizes( $this->getQueueTypes() );
374 if ( is_array( $sizes ) ) { // batching features supported
375 $sizeMap = $sizeMap +
$sizes;
376 } else { // we have to go through the queues in the bucket one-by-one
377 foreach ( $info['types'] as $type ) {
378 $sizeMap[$type] = $this->get( $type )->getSize();
389 protected function getCoalescedQueues() {
390 global $wgJobTypeConf;
392 if ( $this->coalescedQueues
=== null ) {
393 $this->coalescedQueues
= [];
394 foreach ( $wgJobTypeConf as $type => $conf ) {
395 $queue = JobQueue
::factory(
396 [ 'wiki' => $this->wiki
, 'type' => 'null' ] +
$conf );
397 $loc = $queue->getCoalesceLocationInternal();
398 if ( !isset( $this->coalescedQueues
[$loc] ) ) {
399 $this->coalescedQueues
[$loc]['queue'] = $queue;
400 $this->coalescedQueues
[$loc]['types'] = [];
402 if ( $type === 'default' ) {
403 $this->coalescedQueues
[$loc]['types'] = array_merge(
404 $this->coalescedQueues
[$loc]['types'],
405 array_diff( $this->getQueueTypes(), array_keys( $wgJobTypeConf ) )
408 $this->coalescedQueues
[$loc]['types'][] = $type;
413 return $this->coalescedQueues
;
417 * @param string $name
420 private function getCachedConfigVar( $name ) {
421 // @TODO: cleanup this whole method with a proper config system
422 if ( WikiMap
::isCurrentWikiId( $this->wiki
) ) {
423 return $GLOBALS[$name]; // common case
426 $cache = ObjectCache
::getMainWANInstance();
427 $value = $cache->getWithSetCallback(
428 $cache->makeGlobalKey( 'jobqueue', 'configvalue', $wiki, $name ),
429 $cache::TTL_DAY +
mt_rand( 0, $cache::TTL_DAY
),
430 function () use ( $wiki, $name ) {
433 return [ 'v' => $wgConf->getConfig( $wiki, $name ) ];
435 [ 'pcTTL' => WANObjectCache
::TTL_PROC_LONG
]
444 * @throws InvalidArgumentException
446 private function assertValidJobs( array $jobs ) {
447 foreach ( $jobs as $job ) { // sanity checks
448 if ( !( $job instanceof IJobSpecification
) ) {
449 throw new InvalidArgumentException( "Expected IJobSpecification objects" );