- $autoTrx = $dbw->getFlag( DBO_TRX ); // automatic begin() enabled?
- $dbw->clearFlag( DBO_TRX ); // make each query its own transaction
- try {
- // Occasionally recycle jobs back into the queue that have been claimed too long
- if ( mt_rand( 0, 99 ) == 0 ) {
- $this->recycleStaleJobs();
- }
- 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' ) ) ) {
- $row = $this->claimOldest( $uuid );
- } else { // random first
- $rand = mt_rand( 0, self::MAX_JOB_RANDOM ); // encourage concurrent UPDATEs
- $gte = (bool)mt_rand( 0, 1 ); // find rows with rand before/after $rand
- $row = $this->claimRandom( $uuid, $rand, $gte );
- if ( !$row ) { // need to try the other direction
- $row = $this->claimRandom( $uuid, $rand, !$gte );
- }
- }
- // Check if we found a row to reserve...
- if ( !$row ) {
- $wgMemc->set( $this->getEmptinessCacheKey(), 'true', self::CACHE_TTL );
- break; // nothing to do
- }
- // Get the job object from the row...
- $title = Title::makeTitleSafe( $row->job_namespace, $row->job_title );
- if ( !$title ) {
- $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
- wfIncrStats( 'job-pop' );
- wfDebugLog( 'JobQueueDB', "Row has invalid title '{$row->job_title}'." );
- continue; // try again
- }
- $job = Job::factory( $row->job_cmd, $title,
- self::extractBlob( $row->job_params ), $row->job_id );
- // Delete any *other* duplicate jobs in the queue...
- if ( $job->ignoreDuplicates() && strlen( $row->job_sha1 ) ) {
- $dbw->delete( 'job',
- array( 'job_sha1' => $row->job_sha1,
- "job_id != {$dbw->addQuotes( $row->job_id )}" ),
- __METHOD__
- );
- wfIncrStats( 'job-pop', $dbw->affectedRows() );
- }
- break; // done
- } while( true );
- } catch ( DBError $e ) {
- $dbw->setFlag( $autoTrx ? DBO_TRX : 0 ); // restore automatic begin()
- throw $e;