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 DB domain 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;
47 protected $bufferedJobs = [];
49 const TYPE_DEFAULT
= 1; // integer; jobs popped by default
50 const TYPE_ANY
= 2; // integer; any job
52 const USE_CACHE
= 1; // integer; use process or persistent cache
54 const PROC_CACHE_TTL
= 15; // integer; seconds
56 const CACHE_VERSION
= 1; // integer; cache version
59 * @param string $domain Wiki DB domain ID
60 * @param string|bool $readOnlyReason Read-only reason or false
62 protected function __construct( $domain, $readOnlyReason ) {
63 $this->domain
= $domain;
64 $this->readOnlyReason
= $readOnlyReason;
65 $this->cache
= new ProcessCacheLRU( 10 );
69 * @param bool|string $domain Wiki domain ID
70 * @return JobQueueGroup
72 public static function singleton( $domain = false ) {
73 global $wgLocalDatabases;
75 if ( $domain === false ) {
76 $domain = WikiMap
::getCurrentWikiDbDomain()->getId();
79 if ( !isset( self
::$instances[$domain] ) ) {
80 self
::$instances[$domain] = new self( $domain, wfConfiguredReadOnlyReason() );
81 // Make sure jobs are not getting pushed to bogus wikis. This can confuse
82 // the job runner system into spawning endless RPC requests that fail (T171371).
83 $wikiId = WikiMap
::getWikiIdFromDomain( $domain );
85 !WikiMap
::isCurrentWikiDbDomain( $domain ) &&
86 !in_array( $wikiId, $wgLocalDatabases )
88 self
::$instances[$domain]->invalidWiki
= true;
92 return self
::$instances[$domain];
96 * Destroy the singleton instances
100 public static function destroySingletons() {
101 self
::$instances = [];
105 * Get the job queue object for a given queue type
107 * @param string $type
110 public function get( $type ) {
111 global $wgJobTypeConf;
113 $conf = [ 'wiki' => $this->domain
, 'type' => $type ];
114 if ( isset( $wgJobTypeConf[$type] ) ) {
115 $conf = $conf +
$wgJobTypeConf[$type];
117 $conf = $conf +
$wgJobTypeConf['default'];
119 $conf['aggregator'] = JobQueueAggregator
::singleton();
120 if ( $this->readOnlyReason
!== false ) {
121 $conf['readOnlyReason'] = $this->readOnlyReason
;
124 return JobQueue
::factory( $conf );
128 * Insert jobs into the respective queues of which they belong
130 * This inserts the jobs into the queue specified by $wgJobTypeConf
131 * and updates the aggregate job queue information cache as needed.
133 * @param IJobSpecification|IJobSpecification[] $jobs A single Job or a list of Jobs
134 * @throws InvalidArgumentException
137 public function push( $jobs ) {
138 global $wgJobTypesExcludedFromDefaultQueue;
140 if ( $this->invalidWiki
) {
141 // Do not enqueue job that cannot be run (T171371)
142 $e = new LogicException( "Domain '{$this->domain}' is not recognized." );
143 MWExceptionHandler
::logException( $e );
147 $jobs = is_array( $jobs ) ?
$jobs : [ $jobs ];
148 if ( !count( $jobs ) ) {
152 $this->assertValidJobs( $jobs );
154 $jobsByType = []; // (job type => list of jobs)
155 foreach ( $jobs as $job ) {
156 $jobsByType[$job->getType()][] = $job;
159 foreach ( $jobsByType as $type => $jobs ) {
160 $this->get( $type )->push( $jobs );
163 if ( $this->cache
->has( 'queues-ready', 'list' ) ) {
164 $list = $this->cache
->get( 'queues-ready', 'list' );
165 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
166 $this->cache
->clear( 'queues-ready' );
170 $cache = ObjectCache
::getLocalClusterInstance();
172 $cache->makeGlobalKey( 'jobqueue', $this->domain
, 'hasjobs', self
::TYPE_ANY
),
176 if ( array_diff( array_keys( $jobsByType ), $wgJobTypesExcludedFromDefaultQueue ) ) {
178 $cache->makeGlobalKey( 'jobqueue', $this->domain
, 'hasjobs', self
::TYPE_DEFAULT
),
186 * Buffer jobs for insertion via push() or call it now if in CLI mode
188 * Note that pushLazyJobs() is registered as a deferred update just before
189 * DeferredUpdates::doUpdates() in MediaWiki and JobRunner classes in order
190 * to be executed as the very last deferred update (T100085, T154425).
192 * @param IJobSpecification|IJobSpecification[] $jobs A single Job or a list of Jobs
196 public function lazyPush( $jobs ) {
197 if ( $this->invalidWiki
) {
198 // Do not enqueue job that cannot be run (T171371)
199 throw new LogicException( "Domain '{$this->domain}' is not recognized." );
202 if ( PHP_SAPI
=== 'cli' || PHP_SAPI
=== 'phpdbg' ) {
203 $this->push( $jobs );
207 $jobs = is_array( $jobs ) ?
$jobs : [ $jobs ];
209 // Throw errors now instead of on push(), when other jobs may be buffered
210 $this->assertValidJobs( $jobs );
212 $this->bufferedJobs
= array_merge( $this->bufferedJobs
, $jobs );
216 * Push all jobs buffered via lazyPush() into their respective queues
221 public static function pushLazyJobs() {
222 foreach ( self
::$instances as $group ) {
224 $group->push( $group->bufferedJobs
);
225 $group->bufferedJobs
= [];
226 } catch ( Exception
$e ) {
227 // Get in as many jobs as possible and let other post-send updates happen
228 MWExceptionHandler
::logException( $e );
234 * Pop a job off one of the job queues
236 * This pops a job off a queue as specified by $wgJobTypeConf and
237 * updates the aggregate job queue information cache as needed.
239 * @param int|string $qtype JobQueueGroup::TYPE_* constant or job type string
240 * @param int $flags Bitfield of JobQueueGroup::USE_* constants
241 * @param array $blacklist List of job types to ignore
242 * @return Job|bool Returns false on failure
244 public function pop( $qtype = self
::TYPE_DEFAULT
, $flags = 0, array $blacklist = [] ) {
247 if ( is_string( $qtype ) ) { // specific job type
248 if ( !in_array( $qtype, $blacklist ) ) {
249 $job = $this->get( $qtype )->pop();
251 } else { // any job in the "default" jobs types
252 if ( $flags & self
::USE_CACHE
) {
253 if ( !$this->cache
->has( 'queues-ready', 'list', self
::PROC_CACHE_TTL
) ) {
254 $this->cache
->set( 'queues-ready', 'list', $this->getQueuesWithJobs() );
256 $types = $this->cache
->get( 'queues-ready', 'list' );
258 $types = $this->getQueuesWithJobs();
261 if ( $qtype == self
::TYPE_DEFAULT
) {
262 $types = array_intersect( $types, $this->getDefaultQueueTypes() );
265 $types = array_diff( $types, $blacklist ); // avoid selected types
266 shuffle( $types ); // avoid starvation
268 foreach ( $types as $type ) { // for each queue...
269 $job = $this->get( $type )->pop();
270 if ( $job ) { // found
272 } else { // not found
273 $this->cache
->clear( 'queues-ready' );
282 * Acknowledge that a job was completed
287 public function ack( Job
$job ) {
288 $this->get( $job->getType() )->ack( $job );
292 * Register the "root job" of a given job into the queue for de-duplication.
293 * This should only be called right *after* all the new jobs have been inserted.
298 public function deduplicateRootJob( Job
$job ) {
299 return $this->get( $job->getType() )->deduplicateRootJob( $job );
303 * Wait for any replica DBs or backup queue servers to catch up.
305 * This does nothing for certain queue classes.
309 public function waitForBackups() {
310 global $wgJobTypeConf;
312 // Try to avoid doing this more than once per queue storage medium
313 foreach ( $wgJobTypeConf as $type => $conf ) {
314 $this->get( $type )->waitForBackups();
319 * Get the list of queue types
321 * @return array List of strings
323 public function getQueueTypes() {
324 return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) );
328 * Get the list of default queue types
330 * @return array List of strings
332 public function getDefaultQueueTypes() {
333 global $wgJobTypesExcludedFromDefaultQueue;
335 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
339 * Check if there are any queues with jobs (this is cached)
341 * @param int $type JobQueueGroup::TYPE_* constant
345 public function queuesHaveJobs( $type = self
::TYPE_ANY
) {
346 $cache = ObjectCache
::getLocalClusterInstance();
347 $key = $cache->makeGlobalKey( 'jobqueue', $this->domain
, 'hasjobs', $type );
349 $value = $cache->get( $key );
350 if ( $value === false ) {
351 $queues = $this->getQueuesWithJobs();
352 if ( $type == self
::TYPE_DEFAULT
) {
353 $queues = array_intersect( $queues, $this->getDefaultQueueTypes() );
355 $value = count( $queues ) ?
'true' : 'false';
356 $cache->add( $key, $value, 15 );
359 return ( $value === 'true' );
363 * Get the list of job types that have non-empty queues
365 * @return array List of job types that have non-empty queues
367 public function getQueuesWithJobs() {
369 foreach ( $this->getCoalescedQueues() as $info ) {
370 $nonEmpty = $info['queue']->getSiblingQueuesWithJobs( $this->getQueueTypes() );
371 if ( is_array( $nonEmpty ) ) { // batching features supported
372 $types = array_merge( $types, $nonEmpty );
373 } else { // we have to go through the queues in the bucket one-by-one
374 foreach ( $info['types'] as $type ) {
375 if ( !$this->get( $type )->isEmpty() ) {
386 * Get the size of the queus for a list of job types
388 * @return array Map of (job type => size)
390 public function getQueueSizes() {
392 foreach ( $this->getCoalescedQueues() as $info ) {
393 $sizes = $info['queue']->getSiblingQueueSizes( $this->getQueueTypes() );
394 if ( is_array( $sizes ) ) { // batching features supported
395 $sizeMap = $sizeMap +
$sizes;
396 } else { // we have to go through the queues in the bucket one-by-one
397 foreach ( $info['types'] as $type ) {
398 $sizeMap[$type] = $this->get( $type )->getSize();
409 protected function getCoalescedQueues() {
410 global $wgJobTypeConf;
412 if ( $this->coalescedQueues
=== null ) {
413 $this->coalescedQueues
= [];
414 foreach ( $wgJobTypeConf as $type => $conf ) {
415 $queue = JobQueue
::factory(
416 [ 'wiki' => $this->domain
, 'type' => 'null' ] +
$conf );
417 $loc = $queue->getCoalesceLocationInternal();
418 if ( !isset( $this->coalescedQueues
[$loc] ) ) {
419 $this->coalescedQueues
[$loc]['queue'] = $queue;
420 $this->coalescedQueues
[$loc]['types'] = [];
422 if ( $type === 'default' ) {
423 $this->coalescedQueues
[$loc]['types'] = array_merge(
424 $this->coalescedQueues
[$loc]['types'],
425 array_diff( $this->getQueueTypes(), array_keys( $wgJobTypeConf ) )
428 $this->coalescedQueues
[$loc]['types'][] = $type;
433 return $this->coalescedQueues
;
437 * @param string $name
440 private function getCachedConfigVar( $name ) {
441 // @TODO: cleanup this whole method with a proper config system
442 if ( WikiMap
::isCurrentWikiDbDomain( $this->domain
) ) {
443 return $GLOBALS[$name]; // common case
445 $wiki = WikiMap
::getWikiIdFromDomain( $this->domain
);
446 $cache = ObjectCache
::getMainWANInstance();
447 $value = $cache->getWithSetCallback(
448 $cache->makeGlobalKey( 'jobqueue', 'configvalue', $this->domain
, $name ),
449 $cache::TTL_DAY +
mt_rand( 0, $cache::TTL_DAY
),
450 function () use ( $wiki, $name ) {
452 // @TODO: use the full domain ID here
453 return [ 'v' => $wgConf->getConfig( $wiki, $name ) ];
455 [ 'pcTTL' => WANObjectCache
::TTL_PROC_LONG
]
463 * @throws InvalidArgumentException
465 private function assertValidJobs( array $jobs ) {
466 foreach ( $jobs as $job ) { // sanity checks
467 if ( !( $job instanceof IJobSpecification
) ) {
468 throw new InvalidArgumentException( "Expected IJobSpecification objects" );
473 function __destruct() {
474 $n = count( $this->bufferedJobs
);
476 $type = implode( ', ', array_unique( array_map( 'get_class', $this->bufferedJobs
) ) );
477 trigger_error( __METHOD__
. ": $n buffered job(s) of type(s) $type never inserted." );