27use Wikimedia\ScopedCallback;
58 $this->cluster = isset(
$params[
'cluster'] ) ?
$params[
'cluster'] :
false;
59 $this->
cache = ObjectCache::getMainWANInstance();
63 return [
'random',
'timestamp',
'fifo' ];
77 $found =
$dbr->selectField(
78 'job',
'1', [
'job_cmd' => $this->
type,
'job_token' =>
'' ], __METHOD__
94 $size = $this->
cache->get( $key );
95 if ( is_int( $size ) ) {
101 $size = (int)
$dbr->selectField(
'job',
'COUNT(*)',
102 [
'job_cmd' => $this->type,
'job_token' =>
'' ],
108 $this->
cache->set( $key, $size, self::CACHE_TTL_SHORT );
118 if ( $this->claimTTL <= 0 ) {
124 $count = $this->
cache->get( $key );
125 if ( is_int( $count ) ) {
131 $count = (int)
$dbr->selectField(
'job',
'COUNT(*)',
132 [
'job_cmd' => $this->type,
"job_token != {$dbr->addQuotes( '' )}" ],
138 $this->
cache->set( $key, $count, self::CACHE_TTL_SHORT );
149 if ( $this->claimTTL <= 0 ) {
155 $count = $this->
cache->get( $key );
156 if ( is_int( $count ) ) {
162 $count = (int)
$dbr->selectField(
'job',
'COUNT(*)',
164 'job_cmd' => $this->type,
165 "job_token != {$dbr->addQuotes( '' )}",
166 "job_attempts >= " .
$dbr->addQuotes( $this->maxTries )
174 $this->
cache->set( $key, $count, self::CACHE_TTL_SHORT );
198 $dbw->onTransactionPreCommitOrIdle(
199 function () use ( $dbw, $jobs, $flags,
$fname ) {
217 if ( !count( $jobs ) ) {
223 foreach ( $jobs as
$job ) {
225 if (
$job->ignoreDuplicates() ) {
226 $rowSet[$row[
'job_sha1']] = $row;
232 if ( $flags & self::QOS_ATOMIC ) {
237 if ( count( $rowSet ) ) {
241 'job_sha1' => array_keys( $rowSet ),
246 foreach (
$res as $row ) {
247 wfDebug(
"Job with hash '{$row->job_sha1}' is a duplicate.\n" );
248 unset( $rowSet[$row->job_sha1] );
252 $rows = array_merge( $rowList, array_values( $rowSet ) );
254 foreach ( array_chunk(
$rows, 50 ) as $rowBatch ) {
255 $dbw->
insert(
'job', $rowBatch, $method );
259 count( $rowSet ) + count( $rowList ) - count(
$rows )
264 if ( $flags & self::QOS_ATOMIC ) {
278 $autoTrx = $dbw->getFlag(
DBO_TRX );
280 $scopedReset =
new ScopedCallback(
function () use ( $dbw, $autoTrx ) {
281 $dbw->setFlag( $autoTrx ?
DBO_TRX : 0 );
288 if ( in_array( $this->
order, [
'fifo',
'timestamp' ] ) ) {
291 $rand = mt_rand( 0, self::MAX_JOB_RANDOM );
292 $gte = (bool)mt_rand( 0, 1 );
301 $title = Title::makeTitle( $row->job_namespace, $row->job_title );
303 self::extractBlob( $row->job_params ), $row->job_id );
304 $job->metadata[
'id'] = $row->job_id;
305 $job->metadata[
'timestamp'] = $row->job_timestamp;
309 if ( !
$job || mt_rand( 0, 9 ) == 0 ) {
335 $invertedDirection =
false;
344 $ineq = $gte ?
'>=' :
'<=';
345 $dir = $gte ?
'ASC' :
'DESC';
346 $row = $dbw->selectRow(
'job', self::selectFields(),
348 'job_cmd' => $this->
type,
350 "job_random {$ineq} {$dbw->addQuotes( $rand )}" ],
352 [
'ORDER BY' =>
"job_random {$dir}" ]
354 if ( !$row && !$invertedDirection ) {
356 $invertedDirection =
true;
363 $row = $dbw->selectRow(
'job', self::selectFields(),
365 'job_cmd' => $this->
type,
369 [
'OFFSET' => mt_rand( 0, self::MAX_OFFSET ) ]
381 'job_token' => $uuid,
382 'job_token_timestamp' => $dbw->timestamp(),
383 'job_attempts = job_attempts+1' ],
384 [
'job_cmd' => $this->type,
'job_id' => $row->job_id,
'job_token' =>
'' ],
389 if ( !$dbw->affectedRows() ) {
411 if ( $dbw->getType() ===
'mysql' ) {
416 $dbw->query(
"UPDATE {$dbw->tableName( 'job' )} " .
418 "job_token = {$dbw->addQuotes( $uuid ) }, " .
419 "job_token_timestamp = {$dbw->addQuotes( $dbw->timestamp() )}, " .
420 "job_attempts = job_attempts+1 " .
422 "job_cmd = {$dbw->addQuotes( $this->type )} " .
423 "AND job_token = {$dbw->addQuotes( '' )} " .
424 ") ORDER BY job_id ASC LIMIT 1",
432 'job_token' => $uuid,
433 'job_token_timestamp' => $dbw->timestamp(),
434 'job_attempts = job_attempts+1' ],
436 $dbw->selectSQLText(
'job',
'job_id',
437 [
'job_cmd' => $this->type,
'job_token' =>
'' ],
439 [
'ORDER BY' =>
'job_id ASC',
'LIMIT' => 1 ] ) .
446 if ( $dbw->affectedRows() ) {
447 $row = $dbw->selectRow(
'job', self::selectFields(),
448 [
'job_cmd' => $this->
type,
'job_token' => $uuid ], __METHOD__
451 wfDebug(
"Row deleted as duplicate by another process.\n" );
467 if ( !isset(
$job->metadata[
'id'] ) ) {
468 throw new MWException(
"Job of type '{$job->getType()}' has no ID." );
473 $autoTrx = $dbw->getFlag(
DBO_TRX );
475 $scopedReset =
new ScopedCallback(
function () use ( $dbw, $autoTrx ) {
476 $dbw->setFlag( $autoTrx ?
DBO_TRX : 0 );
481 [
'job_cmd' => $this->
type,
'job_id' => $job->metadata[
'id'] ], __METHOD__ );
497 if ( !isset(
$params[
'rootJobSignature'] ) ) {
498 throw new MWException(
"Cannot register root job; missing 'rootJobSignature'." );
499 } elseif ( !isset(
$params[
'rootJobTimestamp'] ) ) {
500 throw new MWException(
"Cannot register root job; missing 'rootJobTimestamp'." );
510 $dbw->onTransactionIdle(
513 if ( $timestamp && $timestamp >=
$params[
'rootJobTimestamp'] ) {
533 $dbw->delete(
'job', [
'job_cmd' => $this->
type ] );
546 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
547 $lbFactory->waitForReplication( [
'wiki' => $this->wiki,
'cluster' => $this->cluster ] );
554 foreach ( [
'size',
'acquiredcount' ] as
$type ) {
583 "job_attempts >= " . intval( $this->maxTries )
595 $dbr->select(
'job', self::selectFields(), $conds ),
599 Title::makeTitle( $row->job_namespace, $row->job_title ),
600 strlen( $row->job_params ) ?
unserialize( $row->job_params ) : []
602 $job->metadata[
'id'] = $row->job_id;
603 $job->metadata[
'timestamp'] = $row->job_timestamp;
614 return $this->cluster
615 ?
"DBCluster:{$this->cluster}:{$this->wiki}"
616 :
"LBFactory:{$this->wiki}";
625 $res =
$dbr->select(
'job',
'DISTINCT job_cmd',
626 [
'job_cmd' => $types ], __METHOD__ );
629 foreach (
$res as $row ) {
630 $types[] = $row->job_cmd;
638 $res =
$dbr->select(
'job', [
'job_cmd',
'COUNT(*) AS count' ],
639 [
'job_cmd' => $types ], __METHOD__, [
'GROUP BY' =>
'job_cmd' ] );
642 foreach (
$res as $row ) {
643 $sizes[$row->job_cmd] = (int)$row->count;
660 if ( !$dbw->lock(
"jobqueue-recycle-{$this->type}", __METHOD__, 1 ) ) {
665 if ( $this->claimTTL > 0 ) {
666 $claimCutoff = $dbw->timestamp( $now - $this->claimTTL );
670 $res = $dbw->select(
'job',
'job_id',
672 'job_cmd' => $this->
type,
673 "job_token != {$dbw->addQuotes( '' )}",
674 "job_token_timestamp < {$dbw->addQuotes( $claimCutoff )}",
675 "job_attempts < {$dbw->addQuotes( $this->maxTries )}" ],
681 }, iterator_to_array(
$res )
683 if ( count( $ids ) ) {
690 'job_token_timestamp' => $dbw->timestamp( $now ) ],
695 $affected = $dbw->affectedRows();
698 $this->aggr->notifyQueueNonEmpty( $this->wiki, $this->
type );
703 $pruneCutoff = $dbw->timestamp( $now - self::MAX_AGE_PRUNE );
706 "job_token != {$dbw->addQuotes( '' )}",
707 "job_token_timestamp < {$dbw->addQuotes( $pruneCutoff )}"
709 if ( $this->claimTTL > 0 ) {
710 $conds[] =
"job_attempts >= {$dbw->addQuotes( $this->maxTries )}";
714 $res = $dbw->select(
'job',
'job_id', $conds, __METHOD__ );
718 }, iterator_to_array(
$res )
720 if ( count( $ids ) ) {
721 $dbw->delete(
'job', [
'job_id' => $ids ], __METHOD__ );
722 $affected = $dbw->affectedRows();
727 $dbw->unlock(
"jobqueue-recycle-{$this->type}", __METHOD__ );
745 'job_namespace' =>
$job->getTitle()->getNamespace(),
746 'job_title' =>
$job->getTitle()->getDBkey(),
749 'job_timestamp' => $dbw->timestamp(),
750 'job_sha1' => Wikimedia\base_convert(
754 'job_random' => mt_rand( 0, self::MAX_JOB_RANDOM )
786 protected function getDB( $index ) {
787 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
788 $lb = ( $this->cluster !==
false )
789 ? $lbFactory->getExternalLB( $this->cluster )
790 : $lbFactory->getMainLB( $this->wiki );
792 return ( $lb->getServerType( $lb->getWriterIndex() ) !==
'sqlite' )
795 ? $lb->getConnectionRef( $index, [], $this->wiki, $lb::CONN_TRX_AUTOCOMMIT )
797 : $lb->getConnectionRef( $index, [], $this->wiki );
806 $cluster = is_string( $this->cluster ) ? $this->cluster :
'main';
828 if ( (
string)
$blob !==
'' ) {
859 'job_token_timestamp',
unserialize( $serialized)
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
wfSplitWikiID( $wiki)
Split a wiki ID into DB name and table prefix.
wfForeignMemcKey( $db, $prefix)
Make a cache key for a foreign DB.
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Class to handle job queues stored in the DB.
claimOldest( $uuid)
Reserve a row with a single UPDATE without holding row locks over RTTs...
supportedOrders()
Get the allowed queue orders for configuration validation.
insertFields(IJobSpecification $job)
doGetSiblingQueueSizes(array $types)
bool string $cluster
Name of an external DB cluster.
__construct(array $params)
Additional parameters include:
doBatchPush(array $jobs, $flags)
throwDBException(DBError $e)
static makeBlob( $params)
claimRandom( $uuid, $rand, $gte)
Reserve a row with a single UPDATE without holding row locks over RTTs...
doGetSiblingQueuesWithJobs(array $types)
recycleAndDeleteStaleJobs()
Recycle or destroy any jobs that have been claimed for too long.
doBatchPushInternal(IDatabase $dbw, array $jobs, $flags, $method)
This function should not be called outside of JobQueueDB.
optimalOrder()
Get the default queue order to use if configuration does not specify one.
getCoalesceLocationInternal()
Do not use this function outside of JobQueue/JobQueueGroup.
static extractBlob( $blob)
doDeduplicateRootJob(IJobSpecification $job)
static selectFields()
Return the list of job fields that should be selected.
getJobIterator(array $conds)
Class to handle enqueueing and running of background jobs.
static incrStats( $key, $type, $delta=1)
Call wfIncrStats() for the queue overall and for the queue type.
getRootJobCacheKey( $signature)
Class to both describe a background job and handle jobs.
static factory( $command, Title $title, $params=[])
Create the appropriate object to handle a specific job.
Convenience class for generating iterators from iterators.
Multi-datacenter aware caching interface.
get( $key, &$curTTL=null, array $checkKeys=[], &$asOf=null)
Fetch the value of a key from cache.
set( $key, $value, $ttl=0, array $opts=[])
Set the value of a key in cache.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
when a variable name is used in a function
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any order
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
processing should stop and the error should be shown to the user * false
returning false will NOT prevent logging $e
Job queue task description interface.
you have access to all of the normal MediaWiki so you can get a DB use the cache
This document describes the state of Postgres support in and is fairly well maintained The main code is very well while extensions are very hit and miss it is probably the most supported database after MySQL Much of the work in making MediaWiki database agnostic came about through the work of creating Postgres but without copying over all the usage comments General notes on the but these can almost always be programmed around *Although Postgres has a true BOOLEAN type
if(count( $args)< 1) $job