Merge "Update formatting and docs"
[lhc/web/wiklou.git] / includes / job / JobQueueDB.php
1 <?php
2 /**
3 * Database-backed job queue code.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Aaron Schulz
22 */
23
24 /**
25 * Class to handle job queues stored in the DB
26 *
27 * @ingroup JobQueue
28 * @since 1.21
29 */
30 class JobQueueDB extends JobQueue {
31 const CACHE_TTL_SHORT = 30; // integer; seconds to cache info without re-validating
32 const CACHE_TTL_LONG = 300; // integer; seconds to cache info that is kept up to date
33 const MAX_AGE_PRUNE = 604800; // integer; seconds a job can live once claimed
34 const MAX_JOB_RANDOM = 2147483647; // integer; 2^31 - 1, used for job_random
35 const MAX_OFFSET = 255; // integer; maximum number of rows to skip
36
37 /** @var BagOStuff */
38 protected $cache;
39
40 protected $cluster = false; // string; name of an external DB cluster
41
42 /**
43 * Additional parameters include:
44 * - cluster : The name of an external cluster registered via LBFactory.
45 * If not specified, the primary DB cluster for the wiki will be used.
46 * This can be overridden with a custom cluster so that DB handles will
47 * be retrieved via LBFactory::getExternalLB() and getConnection().
48 * @param $params array
49 */
50 protected function __construct( array $params ) {
51 global $wgMemc;
52
53 parent::__construct( $params );
54
55 $this->cluster = isset( $params['cluster'] ) ? $params['cluster'] : false;
56 // Make sure that we don't use the SQL cache, which would be harmful
57 $this->cache = ( $wgMemc instanceof SqlBagOStuff ) ? new EmptyBagOStuff() : $wgMemc;
58 }
59
60 protected function supportedOrders() {
61 return array( 'random', 'timestamp', 'fifo' );
62 }
63
64 protected function optimalOrder() {
65 return 'random';
66 }
67
68 /**
69 * @see JobQueue::doIsEmpty()
70 * @return bool
71 */
72 protected function doIsEmpty() {
73 $key = $this->getCacheKey( 'empty' );
74
75 $isEmpty = $this->cache->get( $key );
76 if ( $isEmpty === 'true' ) {
77 return true;
78 } elseif ( $isEmpty === 'false' ) {
79 return false;
80 }
81
82 list( $dbr, $scope ) = $this->getSlaveDB();
83 $found = $dbr->selectField( // unclaimed job
84 'job', '1', array( 'job_cmd' => $this->type, 'job_token' => '' ), __METHOD__
85 );
86 $this->cache->add( $key, $found ? 'false' : 'true', self::CACHE_TTL_LONG );
87
88 return !$found;
89 }
90
91 /**
92 * @see JobQueue::doGetSize()
93 * @return integer
94 */
95 protected function doGetSize() {
96 $key = $this->getCacheKey( 'size' );
97
98 $size = $this->cache->get( $key );
99 if ( is_int( $size ) ) {
100 return $size;
101 }
102
103 list( $dbr, $scope ) = $this->getSlaveDB();
104 $size = (int)$dbr->selectField( 'job', 'COUNT(*)',
105 array( 'job_cmd' => $this->type, 'job_token' => '' ),
106 __METHOD__
107 );
108 $this->cache->set( $key, $size, self::CACHE_TTL_SHORT );
109
110 return $size;
111 }
112
113 /**
114 * @see JobQueue::doGetAcquiredCount()
115 * @return integer
116 */
117 protected function doGetAcquiredCount() {
118 if ( $this->claimTTL <= 0 ) {
119 return 0; // no acknowledgements
120 }
121
122 $key = $this->getCacheKey( 'acquiredcount' );
123
124 $count = $this->cache->get( $key );
125 if ( is_int( $count ) ) {
126 return $count;
127 }
128
129 list( $dbr, $scope ) = $this->getSlaveDB();
130 $count = (int)$dbr->selectField( 'job', 'COUNT(*)',
131 array( 'job_cmd' => $this->type, "job_token != {$dbr->addQuotes( '' )}" ),
132 __METHOD__
133 );
134 $this->cache->set( $key, $count, self::CACHE_TTL_SHORT );
135
136 return $count;
137 }
138
139 /**
140 * @see JobQueue::doGetAbandonedCount()
141 * @return integer
142 * @throws MWException
143 */
144 protected function doGetAbandonedCount() {
145 global $wgMemc;
146
147 if ( $this->claimTTL <= 0 ) {
148 return 0; // no acknowledgements
149 }
150
151 $key = $this->getCacheKey( 'abandonedcount' );
152
153 $count = $wgMemc->get( $key );
154 if ( is_int( $count ) ) {
155 return $count;
156 }
157
158 list( $dbr, $scope ) = $this->getSlaveDB();
159 $count = (int)$dbr->selectField( 'job', 'COUNT(*)',
160 array(
161 'job_cmd' => $this->type,
162 "job_token != {$dbr->addQuotes( '' )}",
163 "job_attempts >= " . $dbr->addQuotes( $this->maxTries )
164 ),
165 __METHOD__
166 );
167 $wgMemc->set( $key, $count, self::CACHE_TTL_SHORT );
168
169 return $count;
170 }
171
172 /**
173 * @see JobQueue::doBatchPush()
174 * @param array $jobs
175 * @param $flags
176 * @throws DBError|Exception
177 * @return bool
178 */
179 protected function doBatchPush( array $jobs, $flags ) {
180 if ( count( $jobs ) ) {
181 list( $dbw, $scope ) = $this->getMasterDB();
182
183 $rowSet = array(); // (sha1 => job) map for jobs that are de-duplicated
184 $rowList = array(); // list of jobs for jobs that are are not de-duplicated
185
186 foreach ( $jobs as $job ) {
187 $row = $this->insertFields( $job );
188 if ( $job->ignoreDuplicates() ) {
189 $rowSet[$row['job_sha1']] = $row;
190 } else {
191 $rowList[] = $row;
192 }
193 }
194
195 $key = $this->getCacheKey( 'empty' );
196 $atomic = ( $flags & self::QOS_ATOMIC );
197 $cache = $this->cache;
198
199 $dbw->onTransactionIdle(
200 function() use ( $dbw, $cache, $rowSet, $rowList, $atomic, $key, $scope
201 ) {
202 if ( $atomic ) {
203 $dbw->begin( __METHOD__ ); // wrap all the job additions in one transaction
204 }
205 try {
206 // Strip out any duplicate jobs that are already in the queue...
207 if ( count( $rowSet ) ) {
208 $res = $dbw->select( 'job', 'job_sha1',
209 array(
210 // No job_type condition since it's part of the job_sha1 hash
211 'job_sha1' => array_keys( $rowSet ),
212 'job_token' => '' // unclaimed
213 ),
214 __METHOD__
215 );
216 foreach ( $res as $row ) {
217 wfDebug( "Job with hash '{$row->job_sha1}' is a duplicate." );
218 unset( $rowSet[$row->job_sha1] ); // already enqueued
219 }
220 }
221 // Build the full list of job rows to insert
222 $rows = array_merge( $rowList, array_values( $rowSet ) );
223 // Insert the job rows in chunks to avoid slave lag...
224 foreach ( array_chunk( $rows, 50 ) as $rowBatch ) {
225 $dbw->insert( 'job', $rowBatch, __METHOD__ );
226 }
227 wfIncrStats( 'job-insert', count( $rows ) );
228 wfIncrStats( 'job-insert-duplicate',
229 count( $rowSet ) + count( $rowList ) - count( $rows ) );
230 } catch ( DBError $e ) {
231 if ( $atomic ) {
232 $dbw->rollback( __METHOD__ );
233 }
234 throw $e;
235 }
236 if ( $atomic ) {
237 $dbw->commit( __METHOD__ );
238 }
239
240 $cache->set( $key, 'false', JobQueueDB::CACHE_TTL_LONG );
241 } );
242 }
243
244 return true;
245 }
246
247 /**
248 * @see JobQueue::doPop()
249 * @return Job|bool
250 */
251 protected function doPop() {
252 if ( $this->cache->get( $this->getCacheKey( 'empty' ) ) === 'true' ) {
253 return false; // queue is empty
254 }
255
256 list( $dbw, $scope ) = $this->getMasterDB();
257 $dbw->commit( __METHOD__, 'flush' ); // flush existing transaction
258
259 $uuid = wfRandomString( 32 ); // pop attempt
260 $job = false; // job popped off
261 do { // retry when our row is invalid or deleted as a duplicate
262 // Try to reserve a row in the DB...
263 if ( in_array( $this->order, array( 'fifo', 'timestamp' ) ) ) {
264 $row = $this->claimOldest( $uuid );
265 } else { // random first
266 $rand = mt_rand( 0, self::MAX_JOB_RANDOM ); // encourage concurrent UPDATEs
267 $gte = (bool)mt_rand( 0, 1 ); // find rows with rand before/after $rand
268 $row = $this->claimRandom( $uuid, $rand, $gte );
269 }
270 // Check if we found a row to reserve...
271 if ( !$row ) {
272 $this->cache->set( $this->getCacheKey( 'empty' ), 'true', self::CACHE_TTL_LONG );
273 break; // nothing to do
274 }
275 wfIncrStats( 'job-pop' );
276 // Get the job object from the row...
277 $title = Title::makeTitleSafe( $row->job_namespace, $row->job_title );
278 if ( !$title ) {
279 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
280 wfDebugLog( 'JobQueueDB', "Row has invalid title '{$row->job_title}'." );
281 continue; // try again
282 }
283 $job = Job::factory( $row->job_cmd, $title,
284 self::extractBlob( $row->job_params ), $row->job_id );
285 $job->id = $row->job_id; // XXX: work around broken subclasses
286 break; // done
287 } while( true );
288
289 return $job;
290 }
291
292 /**
293 * Reserve a row with a single UPDATE without holding row locks over RTTs...
294 *
295 * @param string $uuid 32 char hex string
296 * @param $rand integer Random unsigned integer (31 bits)
297 * @param bool $gte Search for job_random >= $random (otherwise job_random <= $random)
298 * @return Row|false
299 */
300 protected function claimRandom( $uuid, $rand, $gte ) {
301 list( $dbw, $scope ) = $this->getMasterDB();
302 // Check cache to see if the queue has <= OFFSET items
303 $tinyQueue = $this->cache->get( $this->getCacheKey( 'small' ) );
304
305 $row = false; // the row acquired
306 $invertedDirection = false; // whether one job_random direction was already scanned
307 // This uses a replication safe method for acquiring jobs. One could use UPDATE+LIMIT
308 // instead, but that either uses ORDER BY (in which case it deadlocks in MySQL) or is
309 // not replication safe. Due to http://bugs.mysql.com/bug.php?id=6980, subqueries cannot
310 // be used here with MySQL.
311 do {
312 if ( $tinyQueue ) { // queue has <= MAX_OFFSET rows
313 // For small queues, using OFFSET will overshoot and return no rows more often.
314 // Instead, this uses job_random to pick a row (possibly checking both directions).
315 $ineq = $gte ? '>=' : '<=';
316 $dir = $gte ? 'ASC' : 'DESC';
317 $row = $dbw->selectRow( 'job', '*', // find a random job
318 array(
319 'job_cmd' => $this->type,
320 'job_token' => '', // unclaimed
321 "job_random {$ineq} {$dbw->addQuotes( $rand )}" ),
322 __METHOD__,
323 array( 'ORDER BY' => "job_random {$dir}" )
324 );
325 if ( !$row && !$invertedDirection ) {
326 $gte = !$gte;
327 $invertedDirection = true;
328 continue; // try the other direction
329 }
330 } else { // table *may* have >= MAX_OFFSET rows
331 // Bug 42614: "ORDER BY job_random" with a job_random inequality causes high CPU
332 // in MySQL if there are many rows for some reason. This uses a small OFFSET
333 // instead of job_random for reducing excess claim retries.
334 $row = $dbw->selectRow( 'job', '*', // find a random job
335 array(
336 'job_cmd' => $this->type,
337 'job_token' => '', // unclaimed
338 ),
339 __METHOD__,
340 array( 'OFFSET' => mt_rand( 0, self::MAX_OFFSET ) )
341 );
342 if ( !$row ) {
343 $tinyQueue = true; // we know the queue must have <= MAX_OFFSET rows
344 $this->cache->set( $this->getCacheKey( 'small' ), 1, 30 );
345 continue; // use job_random
346 }
347 }
348 if ( $row ) { // claim the job
349 $dbw->update( 'job', // update by PK
350 array(
351 'job_token' => $uuid,
352 'job_token_timestamp' => $dbw->timestamp(),
353 'job_attempts = job_attempts+1' ),
354 array( 'job_cmd' => $this->type, 'job_id' => $row->job_id, 'job_token' => '' ),
355 __METHOD__
356 );
357 // This might get raced out by another runner when claiming the previously
358 // selected row. The use of job_random should minimize this problem, however.
359 if ( !$dbw->affectedRows() ) {
360 $row = false; // raced out
361 }
362 } else {
363 break; // nothing to do
364 }
365 } while ( !$row );
366
367 return $row;
368 }
369
370 /**
371 * Reserve a row with a single UPDATE without holding row locks over RTTs...
372 *
373 * @param string $uuid 32 char hex string
374 * @return Row|false
375 */
376 protected function claimOldest( $uuid ) {
377 list( $dbw, $scope ) = $this->getMasterDB();
378
379 $row = false; // the row acquired
380 do {
381 if ( $dbw->getType() === 'mysql' ) {
382 // Per http://bugs.mysql.com/bug.php?id=6980, we can't use subqueries on the
383 // same table being changed in an UPDATE query in MySQL (gives Error: 1093).
384 // Oracle and Postgre have no such limitation. However, MySQL offers an
385 // alternative here by supporting ORDER BY + LIMIT for UPDATE queries.
386 $dbw->query( "UPDATE {$dbw->tableName( 'job' )} " .
387 "SET " .
388 "job_token = {$dbw->addQuotes( $uuid ) }, " .
389 "job_token_timestamp = {$dbw->addQuotes( $dbw->timestamp() )}, " .
390 "job_attempts = job_attempts+1 " .
391 "WHERE ( " .
392 "job_cmd = {$dbw->addQuotes( $this->type )} " .
393 "AND job_token = {$dbw->addQuotes( '' )} " .
394 ") ORDER BY job_id ASC LIMIT 1",
395 __METHOD__
396 );
397 } else {
398 // Use a subquery to find the job, within an UPDATE to claim it.
399 // This uses as much of the DB wrapper functions as possible.
400 $dbw->update( 'job',
401 array(
402 'job_token' => $uuid,
403 'job_token_timestamp' => $dbw->timestamp(),
404 'job_attempts = job_attempts+1' ),
405 array( 'job_id = (' .
406 $dbw->selectSQLText( 'job', 'job_id',
407 array( 'job_cmd' => $this->type, 'job_token' => '' ),
408 __METHOD__,
409 array( 'ORDER BY' => 'job_id ASC', 'LIMIT' => 1 ) ) .
410 ')'
411 ),
412 __METHOD__
413 );
414 }
415 // Fetch any row that we just reserved...
416 if ( $dbw->affectedRows() ) {
417 $row = $dbw->selectRow( 'job', '*',
418 array( 'job_cmd' => $this->type, 'job_token' => $uuid ), __METHOD__
419 );
420 if ( !$row ) { // raced out by duplicate job removal
421 wfDebugLog( 'JobQueueDB', "Row deleted as duplicate by another process." );
422 }
423 } else {
424 break; // nothing to do
425 }
426 } while ( !$row );
427
428 return $row;
429 }
430
431 /**
432 * Recycle or destroy any jobs that have been claimed for too long
433 *
434 * @return integer Number of jobs recycled/deleted
435 */
436 public function recycleAndDeleteStaleJobs() {
437 $now = time();
438 list( $dbw, $scope ) = $this->getMasterDB();
439 $count = 0; // affected rows
440
441 if ( !$dbw->lock( "jobqueue-recycle-{$this->type}", __METHOD__, 1 ) ) {
442 return $count; // already in progress
443 }
444
445 // Remove claims on jobs acquired for too long if enabled...
446 if ( $this->claimTTL > 0 ) {
447 $claimCutoff = $dbw->timestamp( $now - $this->claimTTL );
448 // Get the IDs of jobs that have be claimed but not finished after too long.
449 // These jobs can be recycled into the queue by expiring the claim. Selecting
450 // the IDs first means that the UPDATE can be done by primary key (less deadlocks).
451 $res = $dbw->select( 'job', 'job_id',
452 array(
453 'job_cmd' => $this->type,
454 "job_token != {$dbw->addQuotes( '' )}", // was acquired
455 "job_token_timestamp < {$dbw->addQuotes( $claimCutoff )}", // stale
456 "job_attempts < {$dbw->addQuotes( $this->maxTries )}" ), // retries left
457 __METHOD__
458 );
459 $ids = array_map( function( $o ) { return $o->job_id; }, iterator_to_array( $res ) );
460 if ( count( $ids ) ) {
461 // Reset job_token for these jobs so that other runners will pick them up.
462 // Set the timestamp to the current time, as it is useful to now that the job
463 // was already tried before (the timestamp becomes the "released" time).
464 $dbw->update( 'job',
465 array(
466 'job_token' => '',
467 'job_token_timestamp' => $dbw->timestamp( $now ) ), // time of release
468 array(
469 'job_id' => $ids ),
470 __METHOD__
471 );
472 $count += $dbw->affectedRows();
473 wfIncrStats( 'job-recycle', $dbw->affectedRows() );
474 $this->cache->set( $this->getCacheKey( 'empty' ), 'false', self::CACHE_TTL_LONG );
475 }
476 }
477
478 // Just destroy any stale jobs...
479 $pruneCutoff = $dbw->timestamp( $now - self::MAX_AGE_PRUNE );
480 $conds = array(
481 'job_cmd' => $this->type,
482 "job_token != {$dbw->addQuotes( '' )}", // was acquired
483 "job_token_timestamp < {$dbw->addQuotes( $pruneCutoff )}" // stale
484 );
485 if ( $this->claimTTL > 0 ) { // only prune jobs attempted too many times...
486 $conds[] = "job_attempts >= {$dbw->addQuotes( $this->maxTries )}";
487 }
488 // Get the IDs of jobs that are considered stale and should be removed. Selecting
489 // the IDs first means that the UPDATE can be done by primary key (less deadlocks).
490 $res = $dbw->select( 'job', 'job_id', $conds, __METHOD__ );
491 $ids = array_map( function( $o ) { return $o->job_id; }, iterator_to_array( $res ) );
492 if ( count( $ids ) ) {
493 $dbw->delete( 'job', array( 'job_id' => $ids ), __METHOD__ );
494 $count += $dbw->affectedRows();
495 }
496
497 $dbw->unlock( "jobqueue-recycle-{$this->type}", __METHOD__ );
498
499 return $count;
500 }
501
502 /**
503 * @see JobQueue::doAck()
504 * @param Job $job
505 * @throws MWException
506 * @return Job|bool
507 */
508 protected function doAck( Job $job ) {
509 if ( !$job->getId() ) {
510 throw new MWException( "Job of type '{$job->getType()}' has no ID." );
511 }
512
513 list( $dbw, $scope ) = $this->getMasterDB();
514 $dbw->commit( __METHOD__, 'flush' ); // flush existing transaction
515
516 // Delete a row with a single DELETE without holding row locks over RTTs...
517 $dbw->delete( 'job',
518 array( 'job_cmd' => $this->type, 'job_id' => $job->getId() ), __METHOD__ );
519
520 return true;
521 }
522
523 /**
524 * @see JobQueue::doDeduplicateRootJob()
525 * @param Job $job
526 * @throws MWException
527 * @return bool
528 */
529 protected function doDeduplicateRootJob( Job $job ) {
530 $params = $job->getParams();
531 if ( !isset( $params['rootJobSignature'] ) ) {
532 throw new MWException( "Cannot register root job; missing 'rootJobSignature'." );
533 } elseif ( !isset( $params['rootJobTimestamp'] ) ) {
534 throw new MWException( "Cannot register root job; missing 'rootJobTimestamp'." );
535 }
536 $key = $this->getRootJobCacheKey( $params['rootJobSignature'] );
537 // Callers should call batchInsert() and then this function so that if the insert
538 // fails, the de-duplication registration will be aborted. Since the insert is
539 // deferred till "transaction idle", do the same here, so that the ordering is
540 // maintained. Having only the de-duplication registration succeed would cause
541 // jobs to become no-ops without any actual jobs that made them redundant.
542 list( $dbw, $scope ) = $this->getMasterDB();
543 $cache = $this->cache;
544 $dbw->onTransactionIdle( function() use ( $cache, $params, $key, $scope ) {
545 $timestamp = $cache->get( $key ); // current last timestamp of this job
546 if ( $timestamp && $timestamp >= $params['rootJobTimestamp'] ) {
547 return true; // a newer version of this root job was enqueued
548 }
549
550 // Update the timestamp of the last root job started at the location...
551 return $cache->set( $key, $params['rootJobTimestamp'], JobQueueDB::ROOTJOB_TTL );
552 } );
553
554 return true;
555 }
556
557 /**
558 * @see JobQueue::doWaitForBackups()
559 * @return void
560 */
561 protected function doWaitForBackups() {
562 wfWaitForSlaves();
563 }
564
565 /**
566 * @return Array
567 */
568 protected function doGetPeriodicTasks() {
569 return array(
570 'recycleAndDeleteStaleJobs' => array(
571 'callback' => array( $this, 'recycleAndDeleteStaleJobs' ),
572 'period' => ceil( $this->claimTTL / 2 )
573 )
574 );
575 }
576
577 /**
578 * @return void
579 */
580 protected function doFlushCaches() {
581 foreach ( array( 'empty', 'size', 'acquiredcount' ) as $type ) {
582 $this->cache->delete( $this->getCacheKey( $type ) );
583 }
584 }
585
586 /**
587 * @see JobQueue::getAllQueuedJobs()
588 * @return Iterator
589 */
590 public function getAllQueuedJobs() {
591 list( $dbr, $scope ) = $this->getSlaveDB();
592 return new MappedIterator(
593 $dbr->select( 'job', '*', array( 'job_cmd' => $this->getType(), 'job_token' => '' ) ),
594 function( $row ) use ( $scope ) {
595 $job = Job::factory(
596 $row->job_cmd,
597 Title::makeTitle( $row->job_namespace, $row->job_title ),
598 strlen( $row->job_params ) ? unserialize( $row->job_params ) : false,
599 $row->job_id
600 );
601 $job->id = $row->job_id; // XXX: work around broken subclasses
602 return $job;
603 }
604 );
605 }
606
607 /**
608 * @return Array (DatabaseBase, ScopedCallback)
609 */
610 protected function getSlaveDB() {
611 return $this->getDB( DB_SLAVE );
612 }
613
614 /**
615 * @return Array (DatabaseBase, ScopedCallback)
616 */
617 protected function getMasterDB() {
618 return $this->getDB( DB_MASTER );
619 }
620
621 /**
622 * @param $index integer (DB_SLAVE/DB_MASTER)
623 * @return Array (DatabaseBase, ScopedCallback)
624 */
625 protected function getDB( $index ) {
626 $lb = ( $this->cluster !== false )
627 ? wfGetLBFactory()->getExternalLB( $this->cluster, $this->wiki )
628 : wfGetLB( $this->wiki );
629 $conn = $lb->getConnection( $index, array(), $this->wiki );
630 return array(
631 $conn,
632 new ScopedCallback( function() use ( $lb, $conn ) {
633 $lb->reuseConnection( $conn );
634 } )
635 );
636 }
637
638 /**
639 * @param $job Job
640 * @return array
641 */
642 protected function insertFields( Job $job ) {
643 list( $dbw, $scope ) = $this->getMasterDB();
644 return array(
645 // Fields that describe the nature of the job
646 'job_cmd' => $job->getType(),
647 'job_namespace' => $job->getTitle()->getNamespace(),
648 'job_title' => $job->getTitle()->getDBkey(),
649 'job_params' => self::makeBlob( $job->getParams() ),
650 // Additional job metadata
651 'job_id' => $dbw->nextSequenceValue( 'job_job_id_seq' ),
652 'job_timestamp' => $dbw->timestamp(),
653 'job_sha1' => wfBaseConvert(
654 sha1( serialize( $job->getDeduplicationInfo() ) ),
655 16, 36, 31
656 ),
657 'job_random' => mt_rand( 0, self::MAX_JOB_RANDOM )
658 );
659 }
660
661 /**
662 * @return string
663 */
664 private function getCacheKey( $property ) {
665 list( $db, $prefix ) = wfSplitWikiID( $this->wiki );
666 return wfForeignMemcKey( $db, $prefix, 'jobqueue', $this->type, $property );
667 }
668
669 /**
670 * @param $params
671 * @return string
672 */
673 protected static function makeBlob( $params ) {
674 if ( $params !== false ) {
675 return serialize( $params );
676 } else {
677 return '';
678 }
679 }
680
681 /**
682 * @param $blob
683 * @return bool|mixed
684 */
685 protected static function extractBlob( $blob ) {
686 if ( (string)$blob !== '' ) {
687 return unserialize( $blob );
688 } else {
689 return false;
690 }
691 }
692 }