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