| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Logger\LoggerFactory; |
| 22 |
use MediaWiki\WikiMap\WikiMap; |
| 23 |
use Psr\Log\LoggerInterface; |
| 24 |
use Wikimedia\ObjectCache\RedisConnectionPool; |
| 25 |
use Wikimedia\ObjectCache\RedisConnRef; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
class JobQueueRedis extends JobQueue { |
| 74 |
|
| 75 |
protected $redisPool; |
| 76 |
|
| 77 |
protected $logger; |
| 78 |
|
| 79 |
|
| 80 |
protected $server; |
| 81 |
|
| 82 |
protected $compression; |
| 83 |
|
| 84 |
private const MAX_PUSH_SIZE = 25; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public function __construct( array $params ) { |
| 99 |
parent::__construct( $params ); |
| 100 |
$params['redisConfig']['serializer'] = 'none'; |
| 101 |
$this->server = $params['redisServer']; |
| 102 |
$this->compression = $params['compression'] ?? 'none'; |
| 103 |
$this->redisPool = RedisConnectionPool::singleton( $params['redisConfig'] ); |
| 104 |
if ( empty( $params['daemonized'] ) ) { |
| 105 |
throw new InvalidArgumentException( |
| 106 |
"Non-daemonized mode is no longer supported. Please install the " . |
| 107 |
"mediawiki/services/jobrunner service and update \$wgJobTypeConf as needed." ); |
| 108 |
} |
| 109 |
$this->logger = LoggerFactory::getInstance( 'redis' ); |
| 110 |
} |
| 111 |
|
| 112 |
protected function supportedOrders() { |
| 113 |
return [ 'timestamp', 'fifo' ]; |
| 114 |
} |
| 115 |
|
| 116 |
protected function optimalOrder() { |
| 117 |
return 'fifo'; |
| 118 |
} |
| 119 |
|
| 120 |
protected function supportsDelayedJobs() { |
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
protected function doIsEmpty() { |
| 130 |
return $this->doGetSize() == 0; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
protected function doGetSize() { |
| 139 |
$conn = $this->getConnection(); |
| 140 |
try { |
| 141 |
return $conn->lLen( $this->getQueueKey( 'l-unclaimed' ) ); |
| 142 |
} catch ( RedisException $e ) { |
| 143 |
throw $this->handleErrorAndMakeException( $conn, $e ); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
protected function doGetAcquiredCount() { |
| 153 |
$conn = $this->getConnection(); |
| 154 |
try { |
| 155 |
$conn->multi( Redis::PIPELINE ); |
| 156 |
$conn->zCard( $this->getQueueKey( 'z-claimed' ) ); |
| 157 |
$conn->zCard( $this->getQueueKey( 'z-abandoned' ) ); |
| 158 |
|
| 159 |
return array_sum( $conn->exec() ); |
| 160 |
} catch ( RedisException $e ) { |
| 161 |
throw $this->handleErrorAndMakeException( $conn, $e ); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
protected function doGetDelayedCount() { |
| 171 |
$conn = $this->getConnection(); |
| 172 |
try { |
| 173 |
return $conn->zCard( $this->getQueueKey( 'z-delayed' ) ); |
| 174 |
} catch ( RedisException $e ) { |
| 175 |
throw $this->handleErrorAndMakeException( $conn, $e ); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
protected function doGetAbandonedCount() { |
| 185 |
$conn = $this->getConnection(); |
| 186 |
try { |
| 187 |
return $conn->zCard( $this->getQueueKey( 'z-abandoned' ) ); |
| 188 |
} catch ( RedisException $e ) { |
| 189 |
throw $this->handleErrorAndMakeException( $conn, $e ); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
protected function doBatchPush( array $jobs, $flags ) { |
| 201 |
|
| 202 |
$items = []; |
| 203 |
foreach ( $jobs as $job ) { |
| 204 |
$item = $this->getNewJobFields( $job ); |
| 205 |
if ( strlen( $item['sha1'] ) ) { |
| 206 |
$items[$item['sha1']] = $item; |
| 207 |
} else { |
| 208 |
$items[$item['uuid']] = $item; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if ( $items === [] ) { |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
$conn = $this->getConnection(); |
| 217 |
try { |
| 218 |
|
| 219 |
if ( $flags & self::QOS_ATOMIC ) { |
| 220 |
$batches = [ $items ]; |
| 221 |
} else { |
| 222 |
$batches = array_chunk( $items, self::MAX_PUSH_SIZE ); |
| 223 |
} |
| 224 |
$failed = 0; |
| 225 |
$pushed = 0; |
| 226 |
foreach ( $batches as $itemBatch ) { |
| 227 |
$added = $this->pushBlobs( $conn, $itemBatch ); |
| 228 |
if ( is_int( $added ) ) { |
| 229 |
$pushed += $added; |
| 230 |
} else { |
| 231 |
$failed += count( $itemBatch ); |
| 232 |
} |
| 233 |
} |
| 234 |
$this->incrStats( 'inserts', $this->type, count( $items ) ); |
| 235 |
$this->incrStats( 'inserts_actual', $this->type, $pushed ); |
| 236 |
$this->incrStats( 'dupe_inserts', $this->type, |
| 237 |
count( $items ) - $failed - $pushed ); |
| 238 |
if ( $failed > 0 ) { |
| 239 |
$err = "Could not insert {$failed} {$this->type} job(s)."; |
| 240 |
wfDebugLog( 'JobQueue', $err ); |
| 241 |
throw new RedisException( $err ); |
| 242 |
} |
| 243 |
} catch ( RedisException $e ) { |
| 244 |
throw $this->handleErrorAndMakeException( $conn, $e ); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
protected function pushBlobs( RedisConnRef $conn, array $items ) { |
| 255 |
$args = [ $this->encodeQueueName() ]; |
| 256 |
|
| 257 |
foreach ( $items as $item ) { |
| 258 |
$args[] = (string)$item['uuid']; |
| 259 |
$args[] = (string)$item['sha1']; |
| 260 |
$args[] = (string)$item['rtimestamp']; |
| 261 |
$args[] = (string)$this->serialize( $item ); |
| 262 |
} |
| 263 |
static $script = |
| 264 |
|
| 265 |
<<<LUA |
| 266 |
local kUnclaimed, kSha1ById, kIdBySha1, kDelayed, kData, kQwJobs = unpack(KEYS) |
| 267 |
-- First argument is the queue ID |
| 268 |
local queueId = ARGV[1] |
| 269 |
-- Next arguments all come in 4s (one per job) |
| 270 |
local variadicArgCount = #ARGV - 1 |
| 271 |
if variadicArgCount % 4 ~= 0 then |
| 272 |
return redis.error_reply('Unmatched arguments') |
| 273 |
end |
| 274 |
-- Insert each job into this queue as needed |
| 275 |
local pushed = 0 |
| 276 |
for i = 2,#ARGV,4 do |
| 277 |
local id,sha1,rtimestamp,blob = ARGV[i],ARGV[i+1],ARGV[i+2],ARGV[i+3] |
| 278 |
if sha1 == '' or redis.call('hExists',kIdBySha1,sha1) == 0 then |
| 279 |
if 1*rtimestamp > 0 then |
| 280 |
-- Insert into delayed queue (release time as score) |
| 281 |
redis.call('zAdd',kDelayed,rtimestamp,id) |
| 282 |
else |
| 283 |
-- Insert into unclaimed queue |
| 284 |
redis.call('lPush',kUnclaimed,id) |
| 285 |
end |
| 286 |
if sha1 ~= '' then |
| 287 |
redis.call('hSet',kSha1ById,id,sha1) |
| 288 |
redis.call('hSet',kIdBySha1,sha1,id) |
| 289 |
end |
| 290 |
redis.call('hSet',kData,id,blob) |
| 291 |
pushed = pushed + 1 |
| 292 |
end |
| 293 |
end |
| 294 |
-- Mark this queue as having jobs |
| 295 |
redis.call('sAdd',kQwJobs,queueId) |
| 296 |
return pushed |
| 297 |
LUA; |
| 298 |
return $conn->luaEval( $script, |
| 299 |
array_merge( |
| 300 |
[ |
| 301 |
$this->getQueueKey( 'l-unclaimed' ), |
| 302 |
$this->getQueueKey( 'h-sha1ById' ), |
| 303 |
$this->getQueueKey( 'h-idBySha1' ), |
| 304 |
$this->getQueueKey( 'z-delayed' ), |
| 305 |
$this->getQueueKey( 'h-data' ), |
| 306 |
$this->getGlobalKey( 's-queuesWithJobs' ), |
| 307 |
], |
| 308 |
$args |
| 309 |
), |
| 310 |
6 |
| 311 |
); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
protected function doPop() { |
| 320 |
$job = false; |
| 321 |
|
| 322 |
$conn = $this->getConnection(); |
| 323 |
try { |
| 324 |
do { |
| 325 |
$blob = $this->popAndAcquireBlob( $conn ); |
| 326 |
if ( !is_string( $blob ) ) { |
| 327 |
break; |
| 328 |
} |
| 329 |
|
| 330 |
$this->incrStats( 'pops', $this->type ); |
| 331 |
$item = $this->unserialize( $blob ); |
| 332 |
if ( $item === false ) { |
| 333 |
wfDebugLog( 'JobQueue', "Could not unserialize {$this->type} job." ); |
| 334 |
continue; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
$job = $this->getJobFromFields( $item ); |
| 339 |
} while ( !$job ); |
| 340 |
} catch ( RedisException $e ) { |
| 341 |
throw $this->handleErrorAndMakeException( $conn, $e ); |
| 342 |
} |
| 343 |
|
| 344 |
return $job; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
protected function popAndAcquireBlob( RedisConnRef $conn ) { |
| 353 |
static $script = |
| 354 |
|
| 355 |
<<<LUA |
| 356 |
local kUnclaimed, kSha1ById, kIdBySha1, kClaimed, kAttempts, kData = unpack(KEYS) |
| 357 |
local rTime = unpack(ARGV) |
| 358 |
-- Pop an item off the queue |
| 359 |
local id = redis.call('rPop',kUnclaimed) |
| 360 |
if not id then |
| 361 |
return false |
| 362 |
end |
| 363 |
-- Allow new duplicates of this job |
| 364 |
local sha1 = redis.call('hGet',kSha1ById,id) |
| 365 |
if sha1 then redis.call('hDel',kIdBySha1,sha1) end |
| 366 |
redis.call('hDel',kSha1ById,id) |
| 367 |
-- Mark the jobs as claimed and return it |
| 368 |
redis.call('zAdd',kClaimed,rTime,id) |
| 369 |
redis.call('hIncrBy',kAttempts,id,1) |
| 370 |
return redis.call('hGet',kData,id) |
| 371 |
LUA; |
| 372 |
return $conn->luaEval( $script, |
| 373 |
[ |
| 374 |
$this->getQueueKey( 'l-unclaimed' ), |
| 375 |
$this->getQueueKey( 'h-sha1ById' ), |
| 376 |
$this->getQueueKey( 'h-idBySha1' ), |
| 377 |
$this->getQueueKey( 'z-claimed' ), |
| 378 |
$this->getQueueKey( 'h-attempts' ), |
| 379 |
$this->getQueueKey( 'h-data' ), |
| 380 |
time(), |
| 381 |
], |
| 382 |
6 |
| 383 |
); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
protected function doAck( RunnableJob $job ) { |
| 394 |
$uuid = $job->getMetadata( 'uuid' ); |
| 395 |
if ( $uuid === null ) { |
| 396 |
throw new UnexpectedValueException( "Job of type '{$job->getType()}' has no UUID." ); |
| 397 |
} |
| 398 |
|
| 399 |
$conn = $this->getConnection(); |
| 400 |
try { |
| 401 |
static $script = |
| 402 |
|
| 403 |
<<<LUA |
| 404 |
local kClaimed, kAttempts, kData = unpack(KEYS) |
| 405 |
local id = unpack(ARGV) |
| 406 |
-- Unmark the job as claimed |
| 407 |
local removed = redis.call('zRem',kClaimed,id) |
| 408 |
-- Check if the job was recycled |
| 409 |
if removed == 0 then |
| 410 |
return 0 |
| 411 |
end |
| 412 |
-- Delete the retry data |
| 413 |
redis.call('hDel',kAttempts,id) |
| 414 |
-- Delete the job data itself |
| 415 |
return redis.call('hDel',kData,id) |
| 416 |
LUA; |
| 417 |
$res = $conn->luaEval( $script, |
| 418 |
[ |
| 419 |
$this->getQueueKey( 'z-claimed' ), |
| 420 |
$this->getQueueKey( 'h-attempts' ), |
| 421 |
$this->getQueueKey( 'h-data' ), |
| 422 |
$uuid |
| 423 |
], |
| 424 |
3 |
| 425 |
); |
| 426 |
|
| 427 |
if ( !$res ) { |
| 428 |
wfDebugLog( 'JobQueue', "Could not acknowledge {$this->type} job $uuid." ); |
| 429 |
|
| 430 |
return false; |
| 431 |
} |
| 432 |
|
| 433 |
$this->incrStats( 'acks', $this->type ); |
| 434 |
} catch ( RedisException $e ) { |
| 435 |
throw $this->handleErrorAndMakeException( $conn, $e ); |
| 436 |
} |
| 437 |
|
| 438 |
return true; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
protected function doDeduplicateRootJob( IJobSpecification $job ) { |
| 448 |
if ( !$job->hasRootJobParams() ) { |
| 449 |
throw new LogicException( "Cannot register root job; missing parameters." ); |
| 450 |
} |
| 451 |
$params = $job->getRootJobParams(); |
| 452 |
|
| 453 |
$key = $this->getRootJobCacheKey( $params['rootJobSignature'], $job->getType() ); |
| 454 |
|
| 455 |
$conn = $this->getConnection(); |
| 456 |
try { |
| 457 |
$timestamp = $conn->get( $key ); |
| 458 |
if ( $timestamp && $timestamp >= $params['rootJobTimestamp'] ) { |
| 459 |
return true; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
return $conn->set( $key, $params['rootJobTimestamp'], self::ROOTJOB_TTL ); |
| 464 |
} catch ( RedisException $e ) { |
| 465 |
throw $this->handleErrorAndMakeException( $conn, $e ); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
protected function doIsRootJobOldDuplicate( IJobSpecification $job ) { |
| 476 |
if ( !$job->hasRootJobParams() ) { |
| 477 |
return false; |
| 478 |
} |
| 479 |
$params = $job->getRootJobParams(); |
| 480 |
|
| 481 |
$conn = $this->getConnection(); |
| 482 |
try { |
| 483 |
|
| 484 |
$timestamp = $conn->get( $this->getRootJobCacheKey( $params['rootJobSignature'], $job->getType() ) ); |
| 485 |
} catch ( RedisException $e ) { |
| 486 |
throw $this->handleErrorAndMakeException( $conn, $e ); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
return ( $timestamp && $timestamp > $params['rootJobTimestamp'] ); |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
protected function doDelete() { |
| 499 |
static $props = [ 'l-unclaimed', 'z-claimed', 'z-abandoned', |
| 500 |
'z-delayed', 'h-idBySha1', 'h-sha1ById', 'h-attempts', 'h-data' ]; |
| 501 |
|
| 502 |
$conn = $this->getConnection(); |
| 503 |
try { |
| 504 |
$keys = []; |
| 505 |
foreach ( $props as $prop ) { |
| 506 |
$keys[] = $this->getQueueKey( $prop ); |
| 507 |
} |
| 508 |
|
| 509 |
$ok = ( $conn->del( $keys ) !== false ); |
| 510 |
$conn->sRem( $this->getGlobalKey( 's-queuesWithJobs' ), $this->encodeQueueName() ); |
| 511 |
|
| 512 |
return $ok; |
| 513 |
} catch ( RedisException $e ) { |
| 514 |
throw $this->handleErrorAndMakeException( $conn, $e ); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
public function getAllQueuedJobs() { |
| 524 |
$conn = $this->getConnection(); |
| 525 |
try { |
| 526 |
$uids = $conn->lRange( $this->getQueueKey( 'l-unclaimed' ), 0, -1 ); |
| 527 |
} catch ( RedisException $e ) { |
| 528 |
throw $this->handleErrorAndMakeException( $conn, $e ); |
| 529 |
} |
| 530 |
|
| 531 |
return $this->getJobIterator( $conn, $uids ); |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
public function getAllDelayedJobs() { |
| 540 |
$conn = $this->getConnection(); |
| 541 |
try { |
| 542 |
$uids = $conn->zRange( $this->getQueueKey( 'z-delayed' ), 0, -1 ); |
| 543 |
} catch ( RedisException $e ) { |
| 544 |
throw $this->handleErrorAndMakeException( $conn, $e ); |
| 545 |
} |
| 546 |
|
| 547 |
return $this->getJobIterator( $conn, $uids ); |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
public function getAllAcquiredJobs() { |
| 556 |
$conn = $this->getConnection(); |
| 557 |
try { |
| 558 |
$uids = $conn->zRange( $this->getQueueKey( 'z-claimed' ), 0, -1 ); |
| 559 |
} catch ( RedisException $e ) { |
| 560 |
throw $this->handleErrorAndMakeException( $conn, $e ); |
| 561 |
} |
| 562 |
|
| 563 |
return $this->getJobIterator( $conn, $uids ); |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
public function getAllAbandonedJobs() { |
| 572 |
$conn = $this->getConnection(); |
| 573 |
try { |
| 574 |
$uids = $conn->zRange( $this->getQueueKey( 'z-abandoned' ), 0, -1 ); |
| 575 |
} catch ( RedisException $e ) { |
| 576 |
throw $this->handleErrorAndMakeException( $conn, $e ); |
| 577 |
} |
| 578 |
|
| 579 |
return $this->getJobIterator( $conn, $uids ); |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
protected function getJobIterator( RedisConnRef $conn, array $uids ) { |
| 588 |
return new MappedIterator( |
| 589 |
$uids, |
| 590 |
function ( $uid ) use ( $conn ) { |
| 591 |
return $this->getJobFromUidInternal( $uid, $conn ); |
| 592 |
}, |
| 593 |
[ 'accept' => static function ( $job ) { |
| 594 |
return is_object( $job ); |
| 595 |
} ] |
| 596 |
); |
| 597 |
} |
| 598 |
|
| 599 |
public function getCoalesceLocationInternal() { |
| 600 |
return "RedisServer:" . $this->server; |
| 601 |
} |
| 602 |
|
| 603 |
protected function doGetSiblingQueuesWithJobs( array $types ) { |
| 604 |
return array_keys( array_filter( $this->doGetSiblingQueueSizes( $types ) ) ); |
| 605 |
} |
| 606 |
|
| 607 |
protected function doGetSiblingQueueSizes( array $types ) { |
| 608 |
$sizes = []; |
| 609 |
$types = array_values( $types ); |
| 610 |
$conn = $this->getConnection(); |
| 611 |
try { |
| 612 |
$conn->multi( Redis::PIPELINE ); |
| 613 |
foreach ( $types as $type ) { |
| 614 |
$conn->lLen( $this->getQueueKey( 'l-unclaimed', $type ) ); |
| 615 |
} |
| 616 |
$res = $conn->exec(); |
| 617 |
if ( is_array( $res ) ) { |
| 618 |
foreach ( $res as $i => $size ) { |
| 619 |
$sizes[$types[$i]] = $size; |
| 620 |
} |
| 621 |
} |
| 622 |
} catch ( RedisException $e ) { |
| 623 |
throw $this->handleErrorAndMakeException( $conn, $e ); |
| 624 |
} |
| 625 |
|
| 626 |
return $sizes; |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
public function getJobFromUidInternal( $uid, $conn ) { |
| 639 |
try { |
| 640 |
$data = $conn->hGet( $this->getQueueKey( 'h-data' ), $uid ); |
| 641 |
if ( $data === false ) { |
| 642 |
return false; |
| 643 |
} |
| 644 |
$item = $this->unserialize( $data ); |
| 645 |
if ( !is_array( $item ) ) { |
| 646 |
throw new UnexpectedValueException( "Could not unserialize job with ID '$uid'." ); |
| 647 |
} |
| 648 |
|
| 649 |
$params = $item['params']; |
| 650 |
$params += [ 'namespace' => $item['namespace'], 'title' => $item['title'] ]; |
| 651 |
$job = $this->factoryJob( $item['type'], $params ); |
| 652 |
$job->setMetadata( 'uuid', $item['uuid'] ); |
| 653 |
$job->setMetadata( 'timestamp', $item['timestamp'] ); |
| 654 |
|
| 655 |
$job->setMetadata( 'attempts', |
| 656 |
$conn->hGet( $this->getQueueKey( 'h-attempts' ), $uid ) ); |
| 657 |
|
| 658 |
return $job; |
| 659 |
} catch ( RedisException $e ) { |
| 660 |
throw $this->handleErrorAndMakeException( $conn, $e ); |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
public function getServerQueuesWithJobs() { |
| 670 |
$queues = []; |
| 671 |
|
| 672 |
$conn = $this->getConnection(); |
| 673 |
try { |
| 674 |
$set = $conn->sMembers( $this->getGlobalKey( 's-queuesWithJobs' ) ); |
| 675 |
foreach ( $set as $queue ) { |
| 676 |
$queues[] = $this->decodeQueueName( $queue ); |
| 677 |
} |
| 678 |
} catch ( RedisException $e ) { |
| 679 |
throw $this->handleErrorAndMakeException( $conn, $e ); |
| 680 |
} |
| 681 |
|
| 682 |
return $queues; |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
protected function getNewJobFields( IJobSpecification $job ) { |
| 690 |
return [ |
| 691 |
|
| 692 |
'type' => $job->getType(), |
| 693 |
'namespace' => $job->getParams()['namespace'] ?? NS_SPECIAL, |
| 694 |
'title' => $job->getParams()['title'] ?? '', |
| 695 |
'params' => $job->getParams(), |
| 696 |
|
| 697 |
'rtimestamp' => $job->getReleaseTimestamp() ?: 0, |
| 698 |
|
| 699 |
'uuid' => $this->idGenerator->newRawUUIDv4(), |
| 700 |
'sha1' => $job->ignoreDuplicates() |
| 701 |
? Wikimedia\base_convert( sha1( serialize( $job->getDeduplicationInfo() ) ), 16, 36, 31 ) |
| 702 |
: '', |
| 703 |
'timestamp' => time() |
| 704 |
]; |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
protected function getJobFromFields( array $fields ) { |
| 712 |
$params = $fields['params']; |
| 713 |
$params += [ 'namespace' => $fields['namespace'], 'title' => $fields['title'] ]; |
| 714 |
|
| 715 |
$job = $this->factoryJob( $fields['type'], $params ); |
| 716 |
$job->setMetadata( 'uuid', $fields['uuid'] ); |
| 717 |
$job->setMetadata( 'timestamp', $fields['timestamp'] ); |
| 718 |
|
| 719 |
return $job; |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
protected function serialize( array $fields ) { |
| 727 |
$blob = serialize( $fields ); |
| 728 |
if ( $this->compression === 'gzip' |
| 729 |
&& strlen( $blob ) >= 1024 |
| 730 |
&& function_exists( 'gzdeflate' ) |
| 731 |
) { |
| 732 |
$object = (object)[ 'blob' => gzdeflate( $blob ), 'enc' => 'gzip' ]; |
| 733 |
$blobz = serialize( $object ); |
| 734 |
|
| 735 |
return ( strlen( $blobz ) < strlen( $blob ) ) ? $blobz : $blob; |
| 736 |
} else { |
| 737 |
return $blob; |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
protected function unserialize( $blob ) { |
| 746 |
$fields = unserialize( $blob ); |
| 747 |
if ( is_object( $fields ) ) { |
| 748 |
if ( $fields->enc === 'gzip' && function_exists( 'gzinflate' ) ) { |
| 749 |
$fields = unserialize( gzinflate( $fields->blob ) ); |
| 750 |
} else { |
| 751 |
$fields = false; |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
return is_array( $fields ) ? $fields : false; |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
protected function getConnection() { |
| 765 |
$conn = $this->redisPool->getConnection( $this->server, $this->logger ); |
| 766 |
if ( !$conn ) { |
| 767 |
throw new JobQueueConnectionError( |
| 768 |
"Unable to connect to redis server {$this->server}." ); |
| 769 |
} |
| 770 |
|
| 771 |
return $conn; |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
protected function handleErrorAndMakeException( RedisConnRef $conn, $e ) { |
| 780 |
$this->redisPool->handleError( $conn, $e ); |
| 781 |
return new JobQueueError( "Redis server error: {$e->getMessage()}\n" ); |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
private function encodeQueueName() { |
| 788 |
return json_encode( [ $this->type, $this->domain ] ); |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
private function decodeQueueName( $name ) { |
| 796 |
return json_decode( $name ); |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
private function getGlobalKey( $name ) { |
| 804 |
$parts = [ 'global', 'jobqueue', $name ]; |
| 805 |
foreach ( $parts as $part ) { |
| 806 |
if ( !preg_match( '/[a-zA-Z0-9_-]+/', $part ) ) { |
| 807 |
throw new InvalidArgumentException( "Key part characters are out of range." ); |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
return implode( ':', $parts ); |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
private function getQueueKey( $prop, $type = null ) { |
| 820 |
$type = is_string( $type ) ? $type : $this->type; |
| 821 |
|
| 822 |
|
| 823 |
$keyspace = WikiMap::getWikiIdFromDbDomain( $this->domain ); |
| 824 |
|
| 825 |
$parts = [ $keyspace, 'jobqueue', $type, $prop ]; |
| 826 |
|
| 827 |
|
| 828 |
return implode( ':', array_map( 'rawurlencode', $parts ) ); |
| 829 |
} |
| 830 |
} |