MediaWiki  REL1_21
JobQueueDB.php
Go to the documentation of this file.
00001 <?php
00030 class JobQueueDB extends JobQueue {
00031         const ROOTJOB_TTL = 1209600; // integer; seconds to remember root jobs (14 days)
00032         const CACHE_TTL_SHORT = 30; // integer; seconds to cache info without re-validating
00033         const CACHE_TTL_LONG = 300; // integer; seconds to cache info that is kept up to date
00034         const MAX_AGE_PRUNE = 604800; // integer; seconds a job can live once claimed
00035         const MAX_JOB_RANDOM = 2147483647; // integer; 2^31 - 1, used for job_random
00036         const MAX_OFFSET = 255; // integer; maximum number of rows to skip
00037 
00038         protected $cluster = false; // string; name of an external DB cluster
00039 
00048         protected function __construct( array $params ) {
00049                 parent::__construct( $params );
00050                 $this->cluster = isset( $params['cluster'] ) ? $params['cluster'] : false;
00051         }
00052 
00053         protected function supportedOrders() {
00054                 return array( 'random', 'timestamp', 'fifo' );
00055         }
00056 
00057         protected function optimalOrder() {
00058                 return 'random';
00059         }
00060 
00065         protected function doIsEmpty() {
00066                 global $wgMemc;
00067 
00068                 $key = $this->getCacheKey( 'empty' );
00069 
00070                 $isEmpty = $wgMemc->get( $key );
00071                 if ( $isEmpty === 'true' ) {
00072                         return true;
00073                 } elseif ( $isEmpty === 'false' ) {
00074                         return false;
00075                 }
00076 
00077                 list( $dbr, $scope ) = $this->getSlaveDB();
00078                 $found = $dbr->selectField( // unclaimed job
00079                         'job', '1', array( 'job_cmd' => $this->type, 'job_token' => '' ), __METHOD__
00080                 );
00081                 $wgMemc->add( $key, $found ? 'false' : 'true', self::CACHE_TTL_LONG );
00082 
00083                 return !$found;
00084         }
00085 
00090         protected function doGetSize() {
00091                 global $wgMemc;
00092 
00093                 $key = $this->getCacheKey( 'size' );
00094 
00095                 $size = $wgMemc->get( $key );
00096                 if ( is_int( $size ) ) {
00097                         return $size;
00098                 }
00099 
00100                 list( $dbr, $scope ) = $this->getSlaveDB();
00101                 $size = (int)$dbr->selectField( 'job', 'COUNT(*)',
00102                         array( 'job_cmd' => $this->type, 'job_token' => '' ),
00103                         __METHOD__
00104                 );
00105                 $wgMemc->set( $key, $size, self::CACHE_TTL_SHORT );
00106 
00107                 return $size;
00108         }
00109 
00114         protected function doGetAcquiredCount() {
00115                 global $wgMemc;
00116 
00117                 if ( $this->claimTTL <= 0 ) {
00118                         return 0; // no acknowledgements
00119                 }
00120 
00121                 $key = $this->getCacheKey( 'acquiredcount' );
00122 
00123                 $count = $wgMemc->get( $key );
00124                 if ( is_int( $count ) ) {
00125                         return $count;
00126                 }
00127 
00128                 list( $dbr, $scope ) = $this->getSlaveDB();
00129                 $count = (int)$dbr->selectField( 'job', 'COUNT(*)',
00130                         array( 'job_cmd' => $this->type, "job_token != {$dbr->addQuotes( '' )}" ),
00131                         __METHOD__
00132                 );
00133                 $wgMemc->set( $key, $count, self::CACHE_TTL_SHORT );
00134 
00135                 return $count;
00136         }
00137 
00145         protected function doBatchPush( array $jobs, $flags ) {
00146                 if ( count( $jobs ) ) {
00147                         list( $dbw, $scope ) = $this->getMasterDB();
00148 
00149                         $rowSet = array(); // (sha1 => job) map for jobs that are de-duplicated
00150                         $rowList = array(); // list of jobs for jobs that are are not de-duplicated
00151 
00152                         foreach ( $jobs as $job ) {
00153                                 $row = $this->insertFields( $job );
00154                                 if ( $job->ignoreDuplicates() ) {
00155                                         $rowSet[$row['job_sha1']] = $row;
00156                                 } else {
00157                                         $rowList[] = $row;
00158                                 }
00159                         }
00160 
00161                         $key = $this->getCacheKey( 'empty' );
00162                         $atomic = ( $flags & self::QoS_Atomic );
00163 
00164                         $dbw->onTransactionIdle(
00165                                 function() use ( $dbw, $rowSet, $rowList, $atomic, $key, $scope
00166                         ) {
00167                                 global $wgMemc;
00168 
00169                                 if ( $atomic ) {
00170                                         $dbw->begin( __METHOD__ ); // wrap all the job additions in one transaction
00171                                 }
00172                                 try {
00173                                         // Strip out any duplicate jobs that are already in the queue...
00174                                         if ( count( $rowSet ) ) {
00175                                                 $res = $dbw->select( 'job', 'job_sha1',
00176                                                         array(
00177                                                                 // No job_type condition since it's part of the job_sha1 hash
00178                                                                 'job_sha1'  => array_keys( $rowSet ),
00179                                                                 'job_token' => '' // unclaimed
00180                                                         ),
00181                                                         __METHOD__
00182                                                 );
00183                                                 foreach ( $res as $row ) {
00184                                                         wfDebug( "Job with hash '{$row->job_sha1}' is a duplicate." );
00185                                                         unset( $rowSet[$row->job_sha1] ); // already enqueued
00186                                                 }
00187                                         }
00188                                         // Build the full list of job rows to insert
00189                                         $rows = array_merge( $rowList, array_values( $rowSet ) );
00190                                         // Insert the job rows in chunks to avoid slave lag...
00191                                         foreach ( array_chunk( $rows, 50 ) as $rowBatch ) {
00192                                                 $dbw->insert( 'job', $rowBatch, __METHOD__ );
00193                                         }
00194                                         wfIncrStats( 'job-insert', count( $rows ) );
00195                                         wfIncrStats( 'job-insert-duplicate',
00196                                                 count( $rowSet ) + count( $rowList ) - count( $rows ) );
00197                                 } catch ( DBError $e ) {
00198                                         if ( $atomic ) {
00199                                                 $dbw->rollback( __METHOD__ );
00200                                         }
00201                                         throw $e;
00202                                 }
00203                                 if ( $atomic ) {
00204                                         $dbw->commit( __METHOD__ );
00205                                 }
00206 
00207                                 $wgMemc->set( $key, 'false', JobQueueDB::CACHE_TTL_LONG );
00208                         } );
00209                 }
00210 
00211                 return true;
00212         }
00213 
00218         protected function doPop() {
00219                 global $wgMemc;
00220 
00221                 if ( $wgMemc->get( $this->getCacheKey( 'empty' ) ) === 'true' ) {
00222                         return false; // queue is empty
00223                 }
00224 
00225                 list( $dbw, $scope ) = $this->getMasterDB();
00226                 $dbw->commit( __METHOD__, 'flush' ); // flush existing transaction
00227                 $autoTrx = $dbw->getFlag( DBO_TRX ); // get current setting
00228                 $dbw->clearFlag( DBO_TRX ); // make each query its own transaction
00229                 $scopedReset = new ScopedCallback( function() use ( $dbw, $autoTrx ) {
00230                         $dbw->setFlag( $autoTrx ? DBO_TRX : 0 ); // restore old setting
00231                 } );
00232 
00233                 $uuid = wfRandomString( 32 ); // pop attempt
00234                 $job = false; // job popped off
00235                 do { // retry when our row is invalid or deleted as a duplicate
00236                         // Try to reserve a row in the DB...
00237                         if ( in_array( $this->order, array( 'fifo', 'timestamp' ) ) ) {
00238                                 $row = $this->claimOldest( $uuid );
00239                         } else { // random first
00240                                 $rand = mt_rand( 0, self::MAX_JOB_RANDOM ); // encourage concurrent UPDATEs
00241                                 $gte = (bool)mt_rand( 0, 1 ); // find rows with rand before/after $rand
00242                                 $row = $this->claimRandom( $uuid, $rand, $gte );
00243                         }
00244                         // Check if we found a row to reserve...
00245                         if ( !$row ) {
00246                                 $wgMemc->set( $this->getCacheKey( 'empty' ), 'true', self::CACHE_TTL_LONG );
00247                                 break; // nothing to do
00248                         }
00249                         wfIncrStats( 'job-pop' );
00250                         // Get the job object from the row...
00251                         $title = Title::makeTitleSafe( $row->job_namespace, $row->job_title );
00252                         if ( !$title ) {
00253                                 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
00254                                 wfDebugLog( 'JobQueueDB', "Row has invalid title '{$row->job_title}'." );
00255                                 continue; // try again
00256                         }
00257                         $job = Job::factory( $row->job_cmd, $title,
00258                                 self::extractBlob( $row->job_params ), $row->job_id );
00259                         $job->id = $row->job_id; // XXX: work around broken subclasses
00260                         // Flag this job as an old duplicate based on its "root" job...
00261                         if ( $this->isRootJobOldDuplicate( $job ) ) {
00262                                 wfIncrStats( 'job-pop-duplicate' );
00263                                 $job = DuplicateJob::newFromJob( $job ); // convert to a no-op
00264                         }
00265                         break; // done
00266                 } while( true );
00267 
00268                 return $job;
00269         }
00270 
00279         protected function claimRandom( $uuid, $rand, $gte ) {
00280                 global $wgMemc;
00281 
00282                 list( $dbw, $scope ) = $this->getMasterDB();
00283                 // Check cache to see if the queue has <= OFFSET items
00284                 $tinyQueue = $wgMemc->get( $this->getCacheKey( 'small' ) );
00285 
00286                 $row = false; // the row acquired
00287                 $invertedDirection = false; // whether one job_random direction was already scanned
00288                 // This uses a replication safe method for acquiring jobs. One could use UPDATE+LIMIT
00289                 // instead, but that either uses ORDER BY (in which case it deadlocks in MySQL) or is
00290                 // not replication safe. Due to http://bugs.mysql.com/bug.php?id=6980, subqueries cannot
00291                 // be used here with MySQL.
00292                 do {
00293                         if ( $tinyQueue ) { // queue has <= MAX_OFFSET rows
00294                                 // For small queues, using OFFSET will overshoot and return no rows more often.
00295                                 // Instead, this uses job_random to pick a row (possibly checking both directions).
00296                                 $ineq = $gte ? '>=' : '<=';
00297                                 $dir = $gte ? 'ASC' : 'DESC';
00298                                 $row = $dbw->selectRow( 'job', '*', // find a random job
00299                                         array(
00300                                                 'job_cmd'   => $this->type,
00301                                                 'job_token' => '', // unclaimed
00302                                                 "job_random {$ineq} {$dbw->addQuotes( $rand )}" ),
00303                                         __METHOD__,
00304                                         array( 'ORDER BY' => "job_random {$dir}" )
00305                                 );
00306                                 if ( !$row && !$invertedDirection ) {
00307                                         $gte = !$gte;
00308                                         $invertedDirection = true;
00309                                         continue; // try the other direction
00310                                 }
00311                         } else { // table *may* have >= MAX_OFFSET rows
00312                                 // Bug 42614: "ORDER BY job_random" with a job_random inequality causes high CPU
00313                                 // in MySQL if there are many rows for some reason. This uses a small OFFSET
00314                                 // instead of job_random for reducing excess claim retries.
00315                                 $row = $dbw->selectRow( 'job', '*', // find a random job
00316                                         array(
00317                                                 'job_cmd'   => $this->type,
00318                                                 'job_token' => '', // unclaimed
00319                                         ),
00320                                         __METHOD__,
00321                                         array( 'OFFSET' => mt_rand( 0, self::MAX_OFFSET ) )
00322                                 );
00323                                 if ( !$row ) {
00324                                         $tinyQueue = true; // we know the queue must have <= MAX_OFFSET rows
00325                                         $wgMemc->set( $this->getCacheKey( 'small' ), 1, 30 );
00326                                         continue; // use job_random
00327                                 }
00328                         }
00329                         if ( $row ) { // claim the job
00330                                 $dbw->update( 'job', // update by PK
00331                                         array(
00332                                                 'job_token'           => $uuid,
00333                                                 'job_token_timestamp' => $dbw->timestamp(),
00334                                                 'job_attempts = job_attempts+1' ),
00335                                         array( 'job_cmd' => $this->type, 'job_id' => $row->job_id, 'job_token' => '' ),
00336                                         __METHOD__
00337                                 );
00338                                 // This might get raced out by another runner when claiming the previously
00339                                 // selected row. The use of job_random should minimize this problem, however.
00340                                 if ( !$dbw->affectedRows() ) {
00341                                         $row = false; // raced out
00342                                 }
00343                         } else {
00344                                 break; // nothing to do
00345                         }
00346                 } while ( !$row );
00347 
00348                 return $row;
00349         }
00350 
00357         protected function claimOldest( $uuid ) {
00358                 list( $dbw, $scope ) = $this->getMasterDB();
00359 
00360                 $row = false; // the row acquired
00361                 do {
00362                         if ( $dbw->getType() === 'mysql' ) {
00363                                 // Per http://bugs.mysql.com/bug.php?id=6980, we can't use subqueries on the
00364                                 // same table being changed in an UPDATE query in MySQL (gives Error: 1093).
00365                                 // Oracle and Postgre have no such limitation. However, MySQL offers an
00366                                 // alternative here by supporting ORDER BY + LIMIT for UPDATE queries.
00367                                 $dbw->query( "UPDATE {$dbw->tableName( 'job' )} " .
00368                                         "SET " .
00369                                                 "job_token = {$dbw->addQuotes( $uuid ) }, " .
00370                                                 "job_token_timestamp = {$dbw->addQuotes( $dbw->timestamp() )}, " .
00371                                                 "job_attempts = job_attempts+1 " .
00372                                         "WHERE ( " .
00373                                                 "job_cmd = {$dbw->addQuotes( $this->type )} " .
00374                                                 "AND job_token = {$dbw->addQuotes( '' )} " .
00375                                         ") ORDER BY job_id ASC LIMIT 1",
00376                                         __METHOD__
00377                                 );
00378                         } else {
00379                                 // Use a subquery to find the job, within an UPDATE to claim it.
00380                                 // This uses as much of the DB wrapper functions as possible.
00381                                 $dbw->update( 'job',
00382                                         array(
00383                                                 'job_token'           => $uuid,
00384                                                 'job_token_timestamp' => $dbw->timestamp(),
00385                                                 'job_attempts = job_attempts+1' ),
00386                                         array( 'job_id = (' .
00387                                                 $dbw->selectSQLText( 'job', 'job_id',
00388                                                         array( 'job_cmd' => $this->type, 'job_token' => '' ),
00389                                                         __METHOD__,
00390                                                         array( 'ORDER BY' => 'job_id ASC', 'LIMIT' => 1 ) ) .
00391                                                 ')'
00392                                         ),
00393                                         __METHOD__
00394                                 );
00395                         }
00396                         // Fetch any row that we just reserved...
00397                         if ( $dbw->affectedRows() ) {
00398                                 $row = $dbw->selectRow( 'job', '*',
00399                                         array( 'job_cmd' => $this->type, 'job_token' => $uuid ), __METHOD__
00400                                 );
00401                                 if ( !$row ) { // raced out by duplicate job removal
00402                                         wfDebugLog( 'JobQueueDB', "Row deleted as duplicate by another process." );
00403                                 }
00404                         } else {
00405                                 break; // nothing to do
00406                         }
00407                 } while ( !$row );
00408 
00409                 return $row;
00410         }
00411 
00417         public function recycleAndDeleteStaleJobs() {
00418                 global $wgMemc;
00419 
00420                 $now = time();
00421                 list( $dbw, $scope ) = $this->getMasterDB();
00422                 $count = 0; // affected rows
00423 
00424                 if ( !$dbw->lock( "jobqueue-recycle-{$this->type}", __METHOD__, 1 ) ) {
00425                         return $count; // already in progress
00426                 }
00427 
00428                 // Remove claims on jobs acquired for too long if enabled...
00429                 if ( $this->claimTTL > 0 ) {
00430                         $claimCutoff = $dbw->timestamp( $now - $this->claimTTL );
00431                         // Get the IDs of jobs that have be claimed but not finished after too long.
00432                         // These jobs can be recycled into the queue by expiring the claim. Selecting
00433                         // the IDs first means that the UPDATE can be done by primary key (less deadlocks).
00434                         $res = $dbw->select( 'job', 'job_id',
00435                                 array(
00436                                         'job_cmd' => $this->type,
00437                                         "job_token != {$dbw->addQuotes( '' )}", // was acquired
00438                                         "job_token_timestamp < {$dbw->addQuotes( $claimCutoff )}", // stale
00439                                         "job_attempts < {$dbw->addQuotes( $this->maxTries )}" ), // retries left
00440                                 __METHOD__
00441                         );
00442                         $ids = array_map( function( $o ) { return $o->job_id; }, iterator_to_array( $res ) );
00443                         if ( count( $ids ) ) {
00444                                 // Reset job_token for these jobs so that other runners will pick them up.
00445                                 // Set the timestamp to the current time, as it is useful to now that the job
00446                                 // was already tried before (the timestamp becomes the "released" time).
00447                                 $dbw->update( 'job',
00448                                         array(
00449                                                 'job_token' => '',
00450                                                 'job_token_timestamp' => $dbw->timestamp( $now ) ), // time of release
00451                                         array(
00452                                                 'job_id' => $ids ),
00453                                         __METHOD__
00454                                 );
00455                                 $count += $dbw->affectedRows();
00456                                 wfIncrStats( 'job-recycle', $dbw->affectedRows() );
00457                                 $wgMemc->set( $this->getCacheKey( 'empty' ), 'false', self::CACHE_TTL_LONG );
00458                         }
00459                 }
00460 
00461                 // Just destroy any stale jobs...
00462                 $pruneCutoff = $dbw->timestamp( $now - self::MAX_AGE_PRUNE );
00463                 $conds = array(
00464                         'job_cmd' => $this->type,
00465                         "job_token != {$dbw->addQuotes( '' )}", // was acquired
00466                         "job_token_timestamp < {$dbw->addQuotes( $pruneCutoff )}" // stale
00467                 );
00468                 if ( $this->claimTTL > 0 ) { // only prune jobs attempted too many times...
00469                         $conds[] = "job_attempts >= {$dbw->addQuotes( $this->maxTries )}";
00470                 }
00471                 // Get the IDs of jobs that are considered stale and should be removed. Selecting
00472                 // the IDs first means that the UPDATE can be done by primary key (less deadlocks).
00473                 $res = $dbw->select( 'job', 'job_id', $conds, __METHOD__ );
00474                 $ids = array_map( function( $o ) { return $o->job_id; }, iterator_to_array( $res ) );
00475                 if ( count( $ids ) ) {
00476                         $dbw->delete( 'job', array( 'job_id' => $ids ), __METHOD__ );
00477                         $count += $dbw->affectedRows();
00478                 }
00479 
00480                 $dbw->unlock( "jobqueue-recycle-{$this->type}", __METHOD__ );
00481 
00482                 return $count;
00483         }
00484 
00491         protected function doAck( Job $job ) {
00492                 if ( !$job->getId() ) {
00493                         throw new MWException( "Job of type '{$job->getType()}' has no ID." );
00494                 }
00495 
00496                 list( $dbw, $scope ) = $this->getMasterDB();
00497                 $dbw->commit( __METHOD__, 'flush' ); // flush existing transaction
00498                 $autoTrx = $dbw->getFlag( DBO_TRX ); // get current setting
00499                 $dbw->clearFlag( DBO_TRX ); // make each query its own transaction
00500                 $scopedReset = new ScopedCallback( function() use ( $dbw, $autoTrx ) {
00501                         $dbw->setFlag( $autoTrx ? DBO_TRX : 0 ); // restore old setting
00502                 } );
00503 
00504                 // Delete a row with a single DELETE without holding row locks over RTTs...
00505                 $dbw->delete( 'job',
00506                         array( 'job_cmd' => $this->type, 'job_id' => $job->getId() ), __METHOD__ );
00507 
00508                 return true;
00509         }
00510 
00517         protected function doDeduplicateRootJob( Job $job ) {
00518                 $params = $job->getParams();
00519                 if ( !isset( $params['rootJobSignature'] ) ) {
00520                         throw new MWException( "Cannot register root job; missing 'rootJobSignature'." );
00521                 } elseif ( !isset( $params['rootJobTimestamp'] ) ) {
00522                         throw new MWException( "Cannot register root job; missing 'rootJobTimestamp'." );
00523                 }
00524                 $key = $this->getRootJobCacheKey( $params['rootJobSignature'] );
00525                 // Callers should call batchInsert() and then this function so that if the insert
00526                 // fails, the de-duplication registration will be aborted. Since the insert is
00527                 // deferred till "transaction idle", do the same here, so that the ordering is
00528                 // maintained. Having only the de-duplication registration succeed would cause
00529                 // jobs to become no-ops without any actual jobs that made them redundant.
00530                 list( $dbw, $scope ) = $this->getMasterDB();
00531                 $dbw->onTransactionIdle( function() use ( $params, $key, $scope ) {
00532                         global $wgMemc;
00533 
00534                         $timestamp = $wgMemc->get( $key ); // current last timestamp of this job
00535                         if ( $timestamp && $timestamp >= $params['rootJobTimestamp'] ) {
00536                                 return true; // a newer version of this root job was enqueued
00537                         }
00538 
00539                         // Update the timestamp of the last root job started at the location...
00540                         return $wgMemc->set( $key, $params['rootJobTimestamp'], JobQueueDB::ROOTJOB_TTL );
00541                 } );
00542 
00543                 return true;
00544         }
00545 
00552         protected function isRootJobOldDuplicate( Job $job ) {
00553                 global $wgMemc;
00554 
00555                 $params = $job->getParams();
00556                 if ( !isset( $params['rootJobSignature'] ) ) {
00557                         return false; // job has no de-deplication info
00558                 } elseif ( !isset( $params['rootJobTimestamp'] ) ) {
00559                         trigger_error( "Cannot check root job; missing 'rootJobTimestamp'." );
00560                         return false;
00561                 }
00562 
00563                 // Get the last time this root job was enqueued
00564                 $timestamp = $wgMemc->get( $this->getRootJobCacheKey( $params['rootJobSignature'] ) );
00565 
00566                 // Check if a new root job was started at the location after this one's...
00567                 return ( $timestamp && $timestamp > $params['rootJobTimestamp'] );
00568         }
00569 
00574         protected function doWaitForBackups() {
00575                 wfWaitForSlaves();
00576         }
00577 
00581         protected function doGetPeriodicTasks() {
00582                 return array(
00583                         'recycleAndDeleteStaleJobs' => array(
00584                                 'callback' => array( $this, 'recycleAndDeleteStaleJobs' ),
00585                                 'period'   => ceil( $this->claimTTL / 2 )
00586                         )
00587                 );
00588         }
00589 
00593         protected function doFlushCaches() {
00594                 global $wgMemc;
00595 
00596                 foreach ( array( 'empty', 'size', 'acquiredcount' ) as $type ) {
00597                         $wgMemc->delete( $this->getCacheKey( $type ) );
00598                 }
00599         }
00600 
00605         public function getAllQueuedJobs() {
00606                 list( $dbr, $scope ) = $this->getSlaveDB();
00607                 return new MappedIterator(
00608                         $dbr->select( 'job', '*', array( 'job_cmd' => $this->getType(), 'job_token' => '' ) ),
00609                         function( $row ) use ( $scope ) {
00610                                 $job = Job::factory(
00611                                         $row->job_cmd,
00612                                         Title::makeTitle( $row->job_namespace, $row->job_title ),
00613                                         strlen( $row->job_params ) ? unserialize( $row->job_params ) : false,
00614                                         $row->job_id
00615                                 );
00616                                 $job->id = $row->job_id; // XXX: work around broken subclasses
00617                                 return $job;
00618                         }
00619                 );
00620         }
00621 
00625         protected function getSlaveDB() {
00626                 return $this->getDB( DB_SLAVE );
00627         }
00628 
00632         protected function getMasterDB() {
00633                 return $this->getDB( DB_MASTER );
00634         }
00635 
00640         protected function getDB( $index ) {
00641                 $lb = ( $this->cluster !== false )
00642                         ? wfGetLBFactory()->getExternalLB( $this->cluster, $this->wiki )
00643                         : wfGetLB( $this->wiki );
00644                 $conn = $lb->getConnection( $index, array(), $this->wiki );
00645                 return array(
00646                         $conn,
00647                         new ScopedCallback( function() use ( $lb, $conn ) {
00648                                 $lb->reuseConnection( $conn );
00649                         } )
00650                 );
00651         }
00652 
00657         protected function insertFields( Job $job ) {
00658                 list( $dbw, $scope ) = $this->getMasterDB();
00659                 return array(
00660                         // Fields that describe the nature of the job
00661                         'job_cmd'       => $job->getType(),
00662                         'job_namespace' => $job->getTitle()->getNamespace(),
00663                         'job_title'     => $job->getTitle()->getDBkey(),
00664                         'job_params'    => self::makeBlob( $job->getParams() ),
00665                         // Additional job metadata
00666                         'job_id'        => $dbw->nextSequenceValue( 'job_job_id_seq' ),
00667                         'job_timestamp' => $dbw->timestamp(),
00668                         'job_sha1'      => wfBaseConvert(
00669                                 sha1( serialize( $job->getDeduplicationInfo() ) ),
00670                                 16, 36, 31
00671                         ),
00672                         'job_random'    => mt_rand( 0, self::MAX_JOB_RANDOM )
00673                 );
00674         }
00675 
00679         private function getCacheKey( $property ) {
00680                 list( $db, $prefix ) = wfSplitWikiID( $this->wiki );
00681                 return wfForeignMemcKey( $db, $prefix, 'jobqueue', $this->type, $property );
00682         }
00683 
00688         private function getRootJobCacheKey( $signature ) {
00689                 list( $db, $prefix ) = wfSplitWikiID( $this->wiki );
00690                 return wfForeignMemcKey( $db, $prefix, 'jobqueue', $this->type, 'rootjob', $signature );
00691         }
00692 
00697         protected static function makeBlob( $params ) {
00698                 if ( $params !== false ) {
00699                         return serialize( $params );
00700                 } else {
00701                         return '';
00702                 }
00703         }
00704 
00709         protected static function extractBlob( $blob ) {
00710                 if ( (string)$blob !== '' ) {
00711                         return unserialize( $blob );
00712                 } else {
00713                         return false;
00714                 }
00715         }
00716 }