X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fjobqueue%2FJobQueueDB.php;h=479ec3289cb7f488d071453fafa84df81e8ff14b;hb=b563ca96845c935d2ca734381c71e4dfb5cd9370;hp=51dec65d1b75a856516b440f227170f420e5663d;hpb=592637225a4d5db5abcdc288d838c160284eef08;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/jobqueue/JobQueueDB.php b/includes/jobqueue/JobQueueDB.php index 51dec65d1b..479ec3289c 100644 --- a/includes/jobqueue/JobQueueDB.php +++ b/includes/jobqueue/JobQueueDB.php @@ -55,7 +55,7 @@ class JobQueueDB extends JobQueue { } protected function supportedOrders() { - return array( 'random', 'timestamp', 'fifo' ); + return [ 'random', 'timestamp', 'fifo' ]; } protected function optimalOrder() { @@ -70,7 +70,7 @@ class JobQueueDB extends JobQueue { $dbr = $this->getSlaveDB(); try { $found = $dbr->selectField( // unclaimed job - 'job', '1', array( 'job_cmd' => $this->type, 'job_token' => '' ), __METHOD__ + 'job', '1', [ 'job_cmd' => $this->type, 'job_token' => '' ], __METHOD__ ); } catch ( DBError $e ) { $this->throwDBException( $e ); @@ -94,7 +94,7 @@ class JobQueueDB extends JobQueue { try { $dbr = $this->getSlaveDB(); $size = (int)$dbr->selectField( 'job', 'COUNT(*)', - array( 'job_cmd' => $this->type, 'job_token' => '' ), + [ 'job_cmd' => $this->type, 'job_token' => '' ], __METHOD__ ); } catch ( DBError $e ) { @@ -124,7 +124,7 @@ class JobQueueDB extends JobQueue { $dbr = $this->getSlaveDB(); try { $count = (int)$dbr->selectField( 'job', 'COUNT(*)', - array( 'job_cmd' => $this->type, "job_token != {$dbr->addQuotes( '' )}" ), + [ 'job_cmd' => $this->type, "job_token != {$dbr->addQuotes( '' )}" ], __METHOD__ ); } catch ( DBError $e ) { @@ -155,11 +155,11 @@ class JobQueueDB extends JobQueue { $dbr = $this->getSlaveDB(); try { $count = (int)$dbr->selectField( 'job', 'COUNT(*)', - array( + [ 'job_cmd' => $this->type, "job_token != {$dbr->addQuotes( '' )}", "job_attempts >= " . $dbr->addQuotes( $this->maxTries ) - ), + ], __METHOD__ ); } catch ( DBError $e ) { @@ -204,8 +204,8 @@ class JobQueueDB extends JobQueue { return; } - $rowSet = array(); // (sha1 => job) map for jobs that are de-duplicated - $rowList = array(); // list of jobs for jobs that are not de-duplicated + $rowSet = []; // (sha1 => job) map for jobs that are de-duplicated + $rowList = []; // list of jobs for jobs that are not de-duplicated foreach ( $jobs as $job ) { $row = $this->insertFields( $job ); if ( $job->ignoreDuplicates() ) { @@ -222,11 +222,11 @@ class JobQueueDB extends JobQueue { // Strip out any duplicate jobs that are already in the queue... if ( count( $rowSet ) ) { $res = $dbw->select( 'job', 'job_sha1', - array( + [ // No job_type condition since it's part of the job_sha1 hash 'job_sha1' => array_keys( $rowSet ), 'job_token' => '' // unclaimed - ), + ], $method ); foreach ( $res as $row ) { @@ -275,7 +275,7 @@ class JobQueueDB extends JobQueue { $job = false; // job popped off do { // retry when our row is invalid or deleted as a duplicate // Try to reserve a row in the DB... - if ( in_array( $this->order, array( 'fifo', 'timestamp' ) ) ) { + if ( in_array( $this->order, [ 'fifo', 'timestamp' ] ) ) { $row = $this->claimOldest( $uuid ); } else { // random first $rand = mt_rand( 0, self::MAX_JOB_RANDOM ); // encourage concurrent UPDATEs @@ -334,12 +334,12 @@ class JobQueueDB extends JobQueue { $ineq = $gte ? '>=' : '<='; $dir = $gte ? 'ASC' : 'DESC'; $row = $dbw->selectRow( 'job', self::selectFields(), // find a random job - array( + [ 'job_cmd' => $this->type, 'job_token' => '', // unclaimed - "job_random {$ineq} {$dbw->addQuotes( $rand )}" ), + "job_random {$ineq} {$dbw->addQuotes( $rand )}" ], __METHOD__, - array( 'ORDER BY' => "job_random {$dir}" ) + [ 'ORDER BY' => "job_random {$dir}" ] ); if ( !$row && !$invertedDirection ) { $gte = !$gte; @@ -351,12 +351,12 @@ class JobQueueDB extends JobQueue { // in MySQL if there are many rows for some reason. This uses a small OFFSET // instead of job_random for reducing excess claim retries. $row = $dbw->selectRow( 'job', self::selectFields(), // find a random job - array( + [ 'job_cmd' => $this->type, 'job_token' => '', // unclaimed - ), + ], __METHOD__, - array( 'OFFSET' => mt_rand( 0, self::MAX_OFFSET ) ) + [ 'OFFSET' => mt_rand( 0, self::MAX_OFFSET ) ] ); if ( !$row ) { $tinyQueue = true; // we know the queue must have <= MAX_OFFSET rows @@ -367,11 +367,11 @@ class JobQueueDB extends JobQueue { if ( $row ) { // claim the job $dbw->update( 'job', // update by PK - array( + [ 'job_token' => $uuid, 'job_token_timestamp' => $dbw->timestamp(), - 'job_attempts = job_attempts+1' ), - array( 'job_cmd' => $this->type, 'job_id' => $row->job_id, 'job_token' => '' ), + 'job_attempts = job_attempts+1' ], + [ 'job_cmd' => $this->type, 'job_id' => $row->job_id, 'job_token' => '' ], __METHOD__ ); // This might get raced out by another runner when claiming the previously @@ -418,24 +418,24 @@ class JobQueueDB extends JobQueue { // Use a subquery to find the job, within an UPDATE to claim it. // This uses as much of the DB wrapper functions as possible. $dbw->update( 'job', - array( + [ 'job_token' => $uuid, 'job_token_timestamp' => $dbw->timestamp(), - 'job_attempts = job_attempts+1' ), - array( 'job_id = (' . + 'job_attempts = job_attempts+1' ], + [ 'job_id = (' . $dbw->selectSQLText( 'job', 'job_id', - array( 'job_cmd' => $this->type, 'job_token' => '' ), + [ 'job_cmd' => $this->type, 'job_token' => '' ], __METHOD__, - array( 'ORDER BY' => 'job_id ASC', 'LIMIT' => 1 ) ) . + [ 'ORDER BY' => 'job_id ASC', 'LIMIT' => 1 ] ) . ')' - ), + ], __METHOD__ ); } // Fetch any row that we just reserved... if ( $dbw->affectedRows() ) { $row = $dbw->selectRow( 'job', self::selectFields(), - array( 'job_cmd' => $this->type, 'job_token' => $uuid ), __METHOD__ + [ 'job_cmd' => $this->type, 'job_token' => $uuid ], __METHOD__ ); if ( !$row ) { // raced out by duplicate job removal wfDebug( "Row deleted as duplicate by another process.\n" ); @@ -452,7 +452,6 @@ class JobQueueDB extends JobQueue { * @see JobQueue::doAck() * @param Job $job * @throws MWException - * @return Job|bool */ protected function doAck( Job $job ) { if ( !isset( $job->metadata['id'] ) ) { @@ -470,14 +469,12 @@ class JobQueueDB extends JobQueue { // Delete a row with a single DELETE without holding row locks over RTTs... $dbw->delete( 'job', - array( 'job_cmd' => $this->type, 'job_id' => $job->metadata['id'] ), __METHOD__ ); + [ 'job_cmd' => $this->type, 'job_id' => $job->metadata['id'] ], __METHOD__ ); JobQueue::incrStats( 'acks', $this->type ); } catch ( DBError $e ) { $this->throwDBException( $e ); } - - return true; } /** @@ -521,7 +518,7 @@ class JobQueueDB extends JobQueue { protected function doDelete() { $dbw = $this->getMasterDB(); try { - $dbw->delete( 'job', array( 'job_cmd' => $this->type ) ); + $dbw->delete( 'job', [ 'job_cmd' => $this->type ] ); } catch ( DBError $e ) { $this->throwDBException( $e ); } @@ -541,7 +538,7 @@ class JobQueueDB extends JobQueue { * @return void */ protected function doFlushCaches() { - foreach ( array( 'size', 'acquiredcount' ) as $type ) { + foreach ( [ 'size', 'acquiredcount' ] as $type ) { $this->cache->delete( $this->getCacheKey( $type ) ); } } @@ -551,7 +548,7 @@ class JobQueueDB extends JobQueue { * @return Iterator */ public function getAllQueuedJobs() { - return $this->getJobIterator( array( 'job_cmd' => $this->getType(), 'job_token' => '' ) ); + return $this->getJobIterator( [ 'job_cmd' => $this->getType(), 'job_token' => '' ] ); } /** @@ -559,7 +556,7 @@ class JobQueueDB extends JobQueue { * @return Iterator */ public function getAllAcquiredJobs() { - return $this->getJobIterator( array( 'job_cmd' => $this->getType(), "job_token > ''" ) ); + return $this->getJobIterator( [ 'job_cmd' => $this->getType(), "job_token > ''" ] ); } /** @@ -575,7 +572,7 @@ class JobQueueDB extends JobQueue { $job = Job::factory( $row->job_cmd, Title::makeTitle( $row->job_namespace, $row->job_title ), - strlen( $row->job_params ) ? unserialize( $row->job_params ) : array() + strlen( $row->job_params ) ? unserialize( $row->job_params ) : [] ); $job->metadata['id'] = $row->job_id; $job->metadata['timestamp'] = $row->job_timestamp; @@ -601,9 +598,9 @@ class JobQueueDB extends JobQueue { // have stale jobs. This lets recycleAndDeleteStaleJobs() re-enqueue // failed jobs so that they can be popped again for that edge case. $res = $dbr->select( 'job', 'DISTINCT job_cmd', - array( 'job_cmd' => $types ), __METHOD__ ); + [ 'job_cmd' => $types ], __METHOD__ ); - $types = array(); + $types = []; foreach ( $res as $row ) { $types[] = $row->job_cmd; } @@ -613,10 +610,10 @@ class JobQueueDB extends JobQueue { protected function doGetSiblingQueueSizes( array $types ) { $dbr = $this->getSlaveDB(); - $res = $dbr->select( 'job', array( 'job_cmd', 'COUNT(*) AS count' ), - array( 'job_cmd' => $types ), __METHOD__, array( 'GROUP BY' => 'job_cmd' ) ); + $res = $dbr->select( 'job', [ 'job_cmd', 'COUNT(*) AS count' ], + [ 'job_cmd' => $types ], __METHOD__, [ 'GROUP BY' => 'job_cmd' ] ); - $sizes = array(); + $sizes = []; foreach ( $res as $row ) { $sizes[$row->job_cmd] = (int)$row->count; } @@ -646,11 +643,11 @@ class JobQueueDB extends JobQueue { // These jobs can be recycled into the queue by expiring the claim. Selecting // the IDs first means that the UPDATE can be done by primary key (less deadlocks). $res = $dbw->select( 'job', 'job_id', - array( + [ 'job_cmd' => $this->type, "job_token != {$dbw->addQuotes( '' )}", // was acquired "job_token_timestamp < {$dbw->addQuotes( $claimCutoff )}", // stale - "job_attempts < {$dbw->addQuotes( $this->maxTries )}" ), // retries left + "job_attempts < {$dbw->addQuotes( $this->maxTries )}" ], // retries left __METHOD__ ); $ids = array_map( @@ -663,11 +660,11 @@ class JobQueueDB extends JobQueue { // Set the timestamp to the current time, as it is useful to now that the job // was already tried before (the timestamp becomes the "released" time). $dbw->update( 'job', - array( + [ 'job_token' => '', - 'job_token_timestamp' => $dbw->timestamp( $now ) ), // time of release - array( - 'job_id' => $ids ), + 'job_token_timestamp' => $dbw->timestamp( $now ) ], // time of release + [ + 'job_id' => $ids ], __METHOD__ ); $affected = $dbw->affectedRows(); @@ -679,11 +676,11 @@ class JobQueueDB extends JobQueue { // Just destroy any stale jobs... $pruneCutoff = $dbw->timestamp( $now - self::MAX_AGE_PRUNE ); - $conds = array( + $conds = [ 'job_cmd' => $this->type, "job_token != {$dbw->addQuotes( '' )}", // was acquired "job_token_timestamp < {$dbw->addQuotes( $pruneCutoff )}" // stale - ); + ]; if ( $this->claimTTL > 0 ) { // only prune jobs attempted too many times... $conds[] = "job_attempts >= {$dbw->addQuotes( $this->maxTries )}"; } @@ -696,7 +693,7 @@ class JobQueueDB extends JobQueue { }, iterator_to_array( $res ) ); if ( count( $ids ) ) { - $dbw->delete( 'job', array( 'job_id' => $ids ), __METHOD__ ); + $dbw->delete( 'job', [ 'job_id' => $ids ], __METHOD__ ); $affected = $dbw->affectedRows(); $count += $affected; JobQueue::incrStats( 'abandons', $this->type, $affected ); @@ -717,7 +714,7 @@ class JobQueueDB extends JobQueue { protected function insertFields( IJobSpecification $job ) { $dbw = $this->getMasterDB(); - return array( + return [ // Fields that describe the nature of the job 'job_cmd' => $job->getType(), 'job_namespace' => $job->getTitle()->getNamespace(), @@ -731,7 +728,7 @@ class JobQueueDB extends JobQueue { 16, 36, 31 ), 'job_random' => mt_rand( 0, self::MAX_JOB_RANDOM ) - ); + ]; } /** @@ -767,7 +764,7 @@ class JobQueueDB extends JobQueue { ? wfGetLBFactory()->getExternalLB( $this->cluster, $this->wiki ) : wfGetLB( $this->wiki ); - return $lb->getConnectionRef( $index, array(), $this->wiki ); + return $lb->getConnectionRef( $index, [], $this->wiki ); } /** @@ -819,7 +816,7 @@ class JobQueueDB extends JobQueue { * @return array */ public static function selectFields() { - return array( + return [ 'job_id', 'job_cmd', 'job_namespace', @@ -831,6 +828,6 @@ class JobQueueDB extends JobQueue { 'job_token', 'job_token_timestamp', 'job_sha1', - ); + ]; } }