MediaWiki
REL1_22
|
00001 <?php 00030 class JobQueueDB extends JobQueue { 00031 const CACHE_TTL_SHORT = 30; // integer; seconds to cache info without re-validating 00032 const CACHE_TTL_LONG = 300; // integer; seconds to cache info that is kept up to date 00033 const MAX_AGE_PRUNE = 604800; // integer; seconds a job can live once claimed 00034 const MAX_JOB_RANDOM = 2147483647; // integer; 2^31 - 1, used for job_random 00035 const MAX_OFFSET = 255; // integer; maximum number of rows to skip 00036 00038 protected $cache; 00039 00040 protected $cluster = false; // string; name of an external DB cluster 00041 00050 protected function __construct( array $params ) { 00051 global $wgMemc; 00052 00053 parent::__construct( $params ); 00054 00055 $this->cluster = isset( $params['cluster'] ) ? $params['cluster'] : false; 00056 // Make sure that we don't use the SQL cache, which would be harmful 00057 $this->cache = ( $wgMemc instanceof SqlBagOStuff ) ? new EmptyBagOStuff() : $wgMemc; 00058 } 00059 00060 protected function supportedOrders() { 00061 return array( 'random', 'timestamp', 'fifo' ); 00062 } 00063 00064 protected function optimalOrder() { 00065 return 'random'; 00066 } 00067 00072 protected function doIsEmpty() { 00073 $key = $this->getCacheKey( 'empty' ); 00074 00075 $isEmpty = $this->cache->get( $key ); 00076 if ( $isEmpty === 'true' ) { 00077 return true; 00078 } elseif ( $isEmpty === 'false' ) { 00079 return false; 00080 } 00081 00082 $dbr = $this->getSlaveDB(); 00083 try { 00084 $found = $dbr->selectField( // unclaimed job 00085 'job', '1', array( 'job_cmd' => $this->type, 'job_token' => '' ), __METHOD__ 00086 ); 00087 } catch ( DBError $e ) { 00088 $this->throwDBException( $e ); 00089 } 00090 $this->cache->add( $key, $found ? 'false' : 'true', self::CACHE_TTL_LONG ); 00091 00092 return !$found; 00093 } 00094 00099 protected function doGetSize() { 00100 $key = $this->getCacheKey( 'size' ); 00101 00102 $size = $this->cache->get( $key ); 00103 if ( is_int( $size ) ) { 00104 return $size; 00105 } 00106 00107 try { 00108 $dbr = $this->getSlaveDB(); 00109 $size = (int)$dbr->selectField( 'job', 'COUNT(*)', 00110 array( 'job_cmd' => $this->type, 'job_token' => '' ), 00111 __METHOD__ 00112 ); 00113 } catch ( DBError $e ) { 00114 $this->throwDBException( $e ); 00115 } 00116 $this->cache->set( $key, $size, self::CACHE_TTL_SHORT ); 00117 00118 return $size; 00119 } 00120 00125 protected function doGetAcquiredCount() { 00126 if ( $this->claimTTL <= 0 ) { 00127 return 0; // no acknowledgements 00128 } 00129 00130 $key = $this->getCacheKey( 'acquiredcount' ); 00131 00132 $count = $this->cache->get( $key ); 00133 if ( is_int( $count ) ) { 00134 return $count; 00135 } 00136 00137 $dbr = $this->getSlaveDB(); 00138 try { 00139 $count = (int)$dbr->selectField( 'job', 'COUNT(*)', 00140 array( 'job_cmd' => $this->type, "job_token != {$dbr->addQuotes( '' )}" ), 00141 __METHOD__ 00142 ); 00143 } catch ( DBError $e ) { 00144 $this->throwDBException( $e ); 00145 } 00146 $this->cache->set( $key, $count, self::CACHE_TTL_SHORT ); 00147 00148 return $count; 00149 } 00150 00156 protected function doGetAbandonedCount() { 00157 global $wgMemc; 00158 00159 if ( $this->claimTTL <= 0 ) { 00160 return 0; // no acknowledgements 00161 } 00162 00163 $key = $this->getCacheKey( 'abandonedcount' ); 00164 00165 $count = $wgMemc->get( $key ); 00166 if ( is_int( $count ) ) { 00167 return $count; 00168 } 00169 00170 $dbr = $this->getSlaveDB(); 00171 try { 00172 $count = (int)$dbr->selectField( 'job', 'COUNT(*)', 00173 array( 00174 'job_cmd' => $this->type, 00175 "job_token != {$dbr->addQuotes( '' )}", 00176 "job_attempts >= " . $dbr->addQuotes( $this->maxTries ) 00177 ), 00178 __METHOD__ 00179 ); 00180 } catch ( DBError $e ) { 00181 $this->throwDBException( $e ); 00182 } 00183 $wgMemc->set( $key, $count, self::CACHE_TTL_SHORT ); 00184 00185 return $count; 00186 } 00187 00195 protected function doBatchPush( array $jobs, $flags ) { 00196 $dbw = $this->getMasterDB(); 00197 00198 $that = $this; 00199 $method = __METHOD__; 00200 $dbw->onTransactionIdle( 00201 function() use ( $dbw, $that, $jobs, $flags, $method ) { 00202 $that->doBatchPushInternal( $dbw, $jobs, $flags, $method ); 00203 } 00204 ); 00205 00206 return true; 00207 } 00208 00219 public function doBatchPushInternal( IDatabase $dbw, array $jobs, $flags, $method ) { 00220 if ( !count( $jobs ) ) { 00221 return true; 00222 } 00223 00224 $rowSet = array(); // (sha1 => job) map for jobs that are de-duplicated 00225 $rowList = array(); // list of jobs for jobs that are are not de-duplicated 00226 foreach ( $jobs as $job ) { 00227 $row = $this->insertFields( $job ); 00228 if ( $job->ignoreDuplicates() ) { 00229 $rowSet[$row['job_sha1']] = $row; 00230 } else { 00231 $rowList[] = $row; 00232 } 00233 } 00234 00235 if ( $flags & self::QOS_ATOMIC ) { 00236 $dbw->begin( $method ); // wrap all the job additions in one transaction 00237 } 00238 try { 00239 // Strip out any duplicate jobs that are already in the queue... 00240 if ( count( $rowSet ) ) { 00241 $res = $dbw->select( 'job', 'job_sha1', 00242 array( 00243 // No job_type condition since it's part of the job_sha1 hash 00244 'job_sha1' => array_keys( $rowSet ), 00245 'job_token' => '' // unclaimed 00246 ), 00247 $method 00248 ); 00249 foreach ( $res as $row ) { 00250 wfDebug( "Job with hash '{$row->job_sha1}' is a duplicate.\n" ); 00251 unset( $rowSet[$row->job_sha1] ); // already enqueued 00252 } 00253 } 00254 // Build the full list of job rows to insert 00255 $rows = array_merge( $rowList, array_values( $rowSet ) ); 00256 // Insert the job rows in chunks to avoid slave lag... 00257 foreach ( array_chunk( $rows, 50 ) as $rowBatch ) { 00258 $dbw->insert( 'job', $rowBatch, $method ); 00259 } 00260 JobQueue::incrStats( 'job-insert', $this->type, count( $rows ) ); 00261 JobQueue::incrStats( 'job-insert-duplicate', $this->type, 00262 count( $rowSet ) + count( $rowList ) - count( $rows ) ); 00263 } catch ( DBError $e ) { 00264 if ( $flags & self::QOS_ATOMIC ) { 00265 $dbw->rollback( $method ); 00266 } 00267 throw $e; 00268 } 00269 if ( $flags & self::QOS_ATOMIC ) { 00270 $dbw->commit( $method ); 00271 } 00272 00273 $this->cache->set( $this->getCacheKey( 'empty' ), 'false', JobQueueDB::CACHE_TTL_LONG ); 00274 00275 return true; 00276 } 00277 00282 protected function doPop() { 00283 if ( $this->cache->get( $this->getCacheKey( 'empty' ) ) === 'true' ) { 00284 return false; // queue is empty 00285 } 00286 00287 $dbw = $this->getMasterDB(); 00288 try { 00289 $dbw->commit( __METHOD__, 'flush' ); // flush existing transaction 00290 $autoTrx = $dbw->getFlag( DBO_TRX ); // get current setting 00291 $dbw->clearFlag( DBO_TRX ); // make each query its own transaction 00292 $scopedReset = new ScopedCallback( function() use ( $dbw, $autoTrx ) { 00293 $dbw->setFlag( $autoTrx ? DBO_TRX : 0 ); // restore old setting 00294 } ); 00295 00296 $uuid = wfRandomString( 32 ); // pop attempt 00297 $job = false; // job popped off 00298 do { // retry when our row is invalid or deleted as a duplicate 00299 // Try to reserve a row in the DB... 00300 if ( in_array( $this->order, array( 'fifo', 'timestamp' ) ) ) { 00301 $row = $this->claimOldest( $uuid ); 00302 } else { // random first 00303 $rand = mt_rand( 0, self::MAX_JOB_RANDOM ); // encourage concurrent UPDATEs 00304 $gte = (bool)mt_rand( 0, 1 ); // find rows with rand before/after $rand 00305 $row = $this->claimRandom( $uuid, $rand, $gte ); 00306 } 00307 // Check if we found a row to reserve... 00308 if ( !$row ) { 00309 $this->cache->set( $this->getCacheKey( 'empty' ), 'true', self::CACHE_TTL_LONG ); 00310 break; // nothing to do 00311 } 00312 JobQueue::incrStats( 'job-pop', $this->type ); 00313 // Get the job object from the row... 00314 $title = Title::makeTitleSafe( $row->job_namespace, $row->job_title ); 00315 if ( !$title ) { 00316 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ ); 00317 wfDebug( "Row has invalid title '{$row->job_title}'." ); 00318 continue; // try again 00319 } 00320 $job = Job::factory( $row->job_cmd, $title, 00321 self::extractBlob( $row->job_params ), $row->job_id ); 00322 $job->metadata['id'] = $row->job_id; 00323 $job->id = $row->job_id; // XXX: work around broken subclasses 00324 break; // done 00325 } while ( true ); 00326 } catch ( DBError $e ) { 00327 $this->throwDBException( $e ); 00328 } 00329 00330 return $job; 00331 } 00332 00341 protected function claimRandom( $uuid, $rand, $gte ) { 00342 $dbw = $this->getMasterDB(); 00343 // Check cache to see if the queue has <= OFFSET items 00344 $tinyQueue = $this->cache->get( $this->getCacheKey( 'small' ) ); 00345 00346 $row = false; // the row acquired 00347 $invertedDirection = false; // whether one job_random direction was already scanned 00348 // This uses a replication safe method for acquiring jobs. One could use UPDATE+LIMIT 00349 // instead, but that either uses ORDER BY (in which case it deadlocks in MySQL) or is 00350 // not replication safe. Due to http://bugs.mysql.com/bug.php?id=6980, subqueries cannot 00351 // be used here with MySQL. 00352 do { 00353 if ( $tinyQueue ) { // queue has <= MAX_OFFSET rows 00354 // For small queues, using OFFSET will overshoot and return no rows more often. 00355 // Instead, this uses job_random to pick a row (possibly checking both directions). 00356 $ineq = $gte ? '>=' : '<='; 00357 $dir = $gte ? 'ASC' : 'DESC'; 00358 $row = $dbw->selectRow( 'job', '*', // find a random job 00359 array( 00360 'job_cmd' => $this->type, 00361 'job_token' => '', // unclaimed 00362 "job_random {$ineq} {$dbw->addQuotes( $rand )}" ), 00363 __METHOD__, 00364 array( 'ORDER BY' => "job_random {$dir}" ) 00365 ); 00366 if ( !$row && !$invertedDirection ) { 00367 $gte = !$gte; 00368 $invertedDirection = true; 00369 continue; // try the other direction 00370 } 00371 } else { // table *may* have >= MAX_OFFSET rows 00372 // Bug 42614: "ORDER BY job_random" with a job_random inequality causes high CPU 00373 // in MySQL if there are many rows for some reason. This uses a small OFFSET 00374 // instead of job_random for reducing excess claim retries. 00375 $row = $dbw->selectRow( 'job', '*', // find a random job 00376 array( 00377 'job_cmd' => $this->type, 00378 'job_token' => '', // unclaimed 00379 ), 00380 __METHOD__, 00381 array( 'OFFSET' => mt_rand( 0, self::MAX_OFFSET ) ) 00382 ); 00383 if ( !$row ) { 00384 $tinyQueue = true; // we know the queue must have <= MAX_OFFSET rows 00385 $this->cache->set( $this->getCacheKey( 'small' ), 1, 30 ); 00386 continue; // use job_random 00387 } 00388 } 00389 if ( $row ) { // claim the job 00390 $dbw->update( 'job', // update by PK 00391 array( 00392 'job_token' => $uuid, 00393 'job_token_timestamp' => $dbw->timestamp(), 00394 'job_attempts = job_attempts+1' ), 00395 array( 'job_cmd' => $this->type, 'job_id' => $row->job_id, 'job_token' => '' ), 00396 __METHOD__ 00397 ); 00398 // This might get raced out by another runner when claiming the previously 00399 // selected row. The use of job_random should minimize this problem, however. 00400 if ( !$dbw->affectedRows() ) { 00401 $row = false; // raced out 00402 } 00403 } else { 00404 break; // nothing to do 00405 } 00406 } while ( !$row ); 00407 00408 return $row; 00409 } 00410 00417 protected function claimOldest( $uuid ) { 00418 $dbw = $this->getMasterDB(); 00419 00420 $row = false; // the row acquired 00421 do { 00422 if ( $dbw->getType() === 'mysql' ) { 00423 // Per http://bugs.mysql.com/bug.php?id=6980, we can't use subqueries on the 00424 // same table being changed in an UPDATE query in MySQL (gives Error: 1093). 00425 // Oracle and Postgre have no such limitation. However, MySQL offers an 00426 // alternative here by supporting ORDER BY + LIMIT for UPDATE queries. 00427 $dbw->query( "UPDATE {$dbw->tableName( 'job' )} " . 00428 "SET " . 00429 "job_token = {$dbw->addQuotes( $uuid ) }, " . 00430 "job_token_timestamp = {$dbw->addQuotes( $dbw->timestamp() )}, " . 00431 "job_attempts = job_attempts+1 " . 00432 "WHERE ( " . 00433 "job_cmd = {$dbw->addQuotes( $this->type )} " . 00434 "AND job_token = {$dbw->addQuotes( '' )} " . 00435 ") ORDER BY job_id ASC LIMIT 1", 00436 __METHOD__ 00437 ); 00438 } else { 00439 // Use a subquery to find the job, within an UPDATE to claim it. 00440 // This uses as much of the DB wrapper functions as possible. 00441 $dbw->update( 'job', 00442 array( 00443 'job_token' => $uuid, 00444 'job_token_timestamp' => $dbw->timestamp(), 00445 'job_attempts = job_attempts+1' ), 00446 array( 'job_id = (' . 00447 $dbw->selectSQLText( 'job', 'job_id', 00448 array( 'job_cmd' => $this->type, 'job_token' => '' ), 00449 __METHOD__, 00450 array( 'ORDER BY' => 'job_id ASC', 'LIMIT' => 1 ) ) . 00451 ')' 00452 ), 00453 __METHOD__ 00454 ); 00455 } 00456 // Fetch any row that we just reserved... 00457 if ( $dbw->affectedRows() ) { 00458 $row = $dbw->selectRow( 'job', '*', 00459 array( 'job_cmd' => $this->type, 'job_token' => $uuid ), __METHOD__ 00460 ); 00461 if ( !$row ) { // raced out by duplicate job removal 00462 wfDebug( "Row deleted as duplicate by another process." ); 00463 } 00464 } else { 00465 break; // nothing to do 00466 } 00467 } while ( !$row ); 00468 00469 return $row; 00470 } 00471 00478 protected function doAck( Job $job ) { 00479 if ( !isset( $job->metadata['id'] ) ) { 00480 throw new MWException( "Job of type '{$job->getType()}' has no ID." ); 00481 } 00482 00483 $dbw = $this->getMasterDB(); 00484 try { 00485 $dbw->commit( __METHOD__, 'flush' ); // flush existing transaction 00486 $autoTrx = $dbw->getFlag( DBO_TRX ); // get current setting 00487 $dbw->clearFlag( DBO_TRX ); // make each query its own transaction 00488 $scopedReset = new ScopedCallback( function() use ( $dbw, $autoTrx ) { 00489 $dbw->setFlag( $autoTrx ? DBO_TRX : 0 ); // restore old setting 00490 } ); 00491 00492 // Delete a row with a single DELETE without holding row locks over RTTs... 00493 $dbw->delete( 'job', 00494 array( 'job_cmd' => $this->type, 'job_id' => $job->metadata['id'] ), __METHOD__ ); 00495 } catch ( DBError $e ) { 00496 $this->throwDBException( $e ); 00497 } 00498 00499 return true; 00500 } 00501 00508 protected function doDeduplicateRootJob( Job $job ) { 00509 $params = $job->getParams(); 00510 if ( !isset( $params['rootJobSignature'] ) ) { 00511 throw new MWException( "Cannot register root job; missing 'rootJobSignature'." ); 00512 } elseif ( !isset( $params['rootJobTimestamp'] ) ) { 00513 throw new MWException( "Cannot register root job; missing 'rootJobTimestamp'." ); 00514 } 00515 $key = $this->getRootJobCacheKey( $params['rootJobSignature'] ); 00516 // Callers should call batchInsert() and then this function so that if the insert 00517 // fails, the de-duplication registration will be aborted. Since the insert is 00518 // deferred till "transaction idle", do the same here, so that the ordering is 00519 // maintained. Having only the de-duplication registration succeed would cause 00520 // jobs to become no-ops without any actual jobs that made them redundant. 00521 $dbw = $this->getMasterDB(); 00522 $cache = $this->dupCache; 00523 $dbw->onTransactionIdle( function() use ( $cache, $params, $key, $dbw ) { 00524 $timestamp = $cache->get( $key ); // current last timestamp of this job 00525 if ( $timestamp && $timestamp >= $params['rootJobTimestamp'] ) { 00526 return true; // a newer version of this root job was enqueued 00527 } 00528 00529 // Update the timestamp of the last root job started at the location... 00530 return $cache->set( $key, $params['rootJobTimestamp'], JobQueueDB::ROOTJOB_TTL ); 00531 } ); 00532 00533 return true; 00534 } 00535 00540 protected function doDelete() { 00541 $dbw = $this->getMasterDB(); 00542 try { 00543 $dbw->delete( 'job', array( 'job_cmd' => $this->type ) ); 00544 } catch ( DBError $e ) { 00545 $this->throwDBException( $e ); 00546 } 00547 return true; 00548 } 00549 00554 protected function doWaitForBackups() { 00555 wfWaitForSlaves(); 00556 } 00557 00561 protected function doGetPeriodicTasks() { 00562 return array( 00563 'recycleAndDeleteStaleJobs' => array( 00564 'callback' => array( $this, 'recycleAndDeleteStaleJobs' ), 00565 'period' => ceil( $this->claimTTL / 2 ) 00566 ) 00567 ); 00568 } 00569 00573 protected function doFlushCaches() { 00574 foreach ( array( 'empty', 'size', 'acquiredcount' ) as $type ) { 00575 $this->cache->delete( $this->getCacheKey( $type ) ); 00576 } 00577 } 00578 00583 public function getAllQueuedJobs() { 00584 $dbr = $this->getSlaveDB(); 00585 try { 00586 return new MappedIterator( 00587 $dbr->select( 'job', '*', 00588 array( 'job_cmd' => $this->getType(), 'job_token' => '' ) ), 00589 function( $row ) use ( $dbr ) { 00590 $job = Job::factory( 00591 $row->job_cmd, 00592 Title::makeTitle( $row->job_namespace, $row->job_title ), 00593 strlen( $row->job_params ) ? unserialize( $row->job_params ) : false, 00594 $row->job_id 00595 ); 00596 $job->metadata['id'] = $row->job_id; 00597 $job->id = $row->job_id; // XXX: work around broken subclasses 00598 return $job; 00599 } 00600 ); 00601 } catch ( DBError $e ) { 00602 $this->throwDBException( $e ); 00603 } 00604 } 00605 00606 public function getCoalesceLocationInternal() { 00607 return $this->cluster 00608 ? "DBCluster:{$this->cluster}:{$this->wiki}" 00609 : "LBFactory:{$this->wiki}"; 00610 } 00611 00612 protected function doGetSiblingQueuesWithJobs( array $types ) { 00613 $dbr = $this->getSlaveDB(); 00614 $res = $dbr->select( 'job', 'DISTINCT job_cmd', 00615 array( 'job_cmd' => $types ), __METHOD__ ); 00616 00617 $types = array(); 00618 foreach ( $res as $row ) { 00619 $types[] = $row->job_cmd; 00620 } 00621 return $types; 00622 } 00623 00624 protected function doGetSiblingQueueSizes( array $types ) { 00625 $dbr = $this->getSlaveDB(); 00626 $res = $dbr->select( 'job', array( 'job_cmd', 'COUNT(*) AS count' ), 00627 array( 'job_cmd' => $types ), __METHOD__, array( 'GROUP BY' => 'job_cmd' ) ); 00628 00629 $sizes = array(); 00630 foreach ( $res as $row ) { 00631 $sizes[$row->job_cmd] = (int)$row->count; 00632 } 00633 return $sizes; 00634 } 00635 00641 public function recycleAndDeleteStaleJobs() { 00642 $now = time(); 00643 $count = 0; // affected rows 00644 $dbw = $this->getMasterDB(); 00645 00646 try { 00647 if ( !$dbw->lock( "jobqueue-recycle-{$this->type}", __METHOD__, 1 ) ) { 00648 return $count; // already in progress 00649 } 00650 00651 // Remove claims on jobs acquired for too long if enabled... 00652 if ( $this->claimTTL > 0 ) { 00653 $claimCutoff = $dbw->timestamp( $now - $this->claimTTL ); 00654 // Get the IDs of jobs that have be claimed but not finished after too long. 00655 // These jobs can be recycled into the queue by expiring the claim. Selecting 00656 // the IDs first means that the UPDATE can be done by primary key (less deadlocks). 00657 $res = $dbw->select( 'job', 'job_id', 00658 array( 00659 'job_cmd' => $this->type, 00660 "job_token != {$dbw->addQuotes( '' )}", // was acquired 00661 "job_token_timestamp < {$dbw->addQuotes( $claimCutoff )}", // stale 00662 "job_attempts < {$dbw->addQuotes( $this->maxTries )}" ), // retries left 00663 __METHOD__ 00664 ); 00665 $ids = array_map( 00666 function( $o ) { 00667 return $o->job_id; 00668 }, iterator_to_array( $res ) 00669 ); 00670 if ( count( $ids ) ) { 00671 // Reset job_token for these jobs so that other runners will pick them up. 00672 // Set the timestamp to the current time, as it is useful to now that the job 00673 // was already tried before (the timestamp becomes the "released" time). 00674 $dbw->update( 'job', 00675 array( 00676 'job_token' => '', 00677 'job_token_timestamp' => $dbw->timestamp( $now ) ), // time of release 00678 array( 00679 'job_id' => $ids ), 00680 __METHOD__ 00681 ); 00682 $count += $dbw->affectedRows(); 00683 JobQueue::incrStats( 'job-recycle', $this->type, $dbw->affectedRows() ); 00684 $this->cache->set( $this->getCacheKey( 'empty' ), 'false', self::CACHE_TTL_LONG ); 00685 } 00686 } 00687 00688 // Just destroy any stale jobs... 00689 $pruneCutoff = $dbw->timestamp( $now - self::MAX_AGE_PRUNE ); 00690 $conds = array( 00691 'job_cmd' => $this->type, 00692 "job_token != {$dbw->addQuotes( '' )}", // was acquired 00693 "job_token_timestamp < {$dbw->addQuotes( $pruneCutoff )}" // stale 00694 ); 00695 if ( $this->claimTTL > 0 ) { // only prune jobs attempted too many times... 00696 $conds[] = "job_attempts >= {$dbw->addQuotes( $this->maxTries )}"; 00697 } 00698 // Get the IDs of jobs that are considered stale and should be removed. Selecting 00699 // the IDs first means that the UPDATE can be done by primary key (less deadlocks). 00700 $res = $dbw->select( 'job', 'job_id', $conds, __METHOD__ ); 00701 $ids = array_map( 00702 function( $o ) { 00703 return $o->job_id; 00704 }, iterator_to_array( $res ) 00705 ); 00706 if ( count( $ids ) ) { 00707 $dbw->delete( 'job', array( 'job_id' => $ids ), __METHOD__ ); 00708 $count += $dbw->affectedRows(); 00709 JobQueue::incrStats( 'job-abandon', $this->type, $dbw->affectedRows() ); 00710 } 00711 00712 $dbw->unlock( "jobqueue-recycle-{$this->type}", __METHOD__ ); 00713 } catch ( DBError $e ) { 00714 $this->throwDBException( $e ); 00715 } 00716 00717 return $count; 00718 } 00719 00724 protected function insertFields( Job $job ) { 00725 $dbw = $this->getMasterDB(); 00726 return array( 00727 // Fields that describe the nature of the job 00728 'job_cmd' => $job->getType(), 00729 'job_namespace' => $job->getTitle()->getNamespace(), 00730 'job_title' => $job->getTitle()->getDBkey(), 00731 'job_params' => self::makeBlob( $job->getParams() ), 00732 // Additional job metadata 00733 'job_id' => $dbw->nextSequenceValue( 'job_job_id_seq' ), 00734 'job_timestamp' => $dbw->timestamp(), 00735 'job_sha1' => wfBaseConvert( 00736 sha1( serialize( $job->getDeduplicationInfo() ) ), 00737 16, 36, 31 00738 ), 00739 'job_random' => mt_rand( 0, self::MAX_JOB_RANDOM ) 00740 ); 00741 } 00742 00746 protected function getSlaveDB() { 00747 try { 00748 return $this->getDB( DB_SLAVE ); 00749 } catch ( DBConnectionError $e ) { 00750 throw new JobQueueConnectionError( "DBConnectionError:" . $e->getMessage() ); 00751 } 00752 } 00753 00757 protected function getMasterDB() { 00758 try { 00759 return $this->getDB( DB_MASTER ); 00760 } catch ( DBConnectionError $e ) { 00761 throw new JobQueueConnectionError( "DBConnectionError:" . $e->getMessage() ); 00762 } 00763 } 00764 00769 protected function getDB( $index ) { 00770 $lb = ( $this->cluster !== false ) 00771 ? wfGetLBFactory()->getExternalLB( $this->cluster, $this->wiki ) 00772 : wfGetLB( $this->wiki ); 00773 return $lb->getConnectionRef( $index, array(), $this->wiki ); 00774 } 00775 00779 private function getCacheKey( $property ) { 00780 list( $db, $prefix ) = wfSplitWikiID( $this->wiki ); 00781 $cluster = is_string( $this->cluster ) ? $this->cluster : 'main'; 00782 return wfForeignMemcKey( $db, $prefix, 'jobqueue', $cluster, $this->type, $property ); 00783 } 00784 00789 protected static function makeBlob( $params ) { 00790 if ( $params !== false ) { 00791 return serialize( $params ); 00792 } else { 00793 return ''; 00794 } 00795 } 00796 00801 protected static function extractBlob( $blob ) { 00802 if ( (string)$blob !== '' ) { 00803 return unserialize( $blob ); 00804 } else { 00805 return false; 00806 } 00807 } 00808 00813 protected function throwDBException( DBError $e ) { 00814 throw new JobQueueError( get_class( $e ) . ": " . $e->getMessage() ); 00815 } 00816 }