From 1ca3cc260fe47e1e87d15f3a679addceaebff5a2 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Fri, 29 Mar 2019 21:11:35 -0700 Subject: [PATCH] jobqueue: change MWException throw statements to using JobQueueError Also fix some minor phpstorm warnings in JobQueue Change-Id: Ie4970f3e93b504fae509ec755b0e0afad71a7749 --- includes/jobqueue/JobQueue.php | 48 ++++++++++++++--------------- includes/jobqueue/JobQueueGroup.php | 14 ++++++--- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/includes/jobqueue/JobQueue.php b/includes/jobqueue/JobQueue.php index cb5cd82c21..c9f17cf54c 100644 --- a/includes/jobqueue/JobQueue.php +++ b/includes/jobqueue/JobQueue.php @@ -29,7 +29,7 @@ use MediaWiki\MediaWikiServices; * @since 1.21 */ abstract class JobQueue { - /** @var string Wiki ID */ + /** @var string DB domain ID */ protected $domain; /** @var string Job type */ protected $type; @@ -53,7 +53,7 @@ abstract class JobQueue { /** * @param array $params - * @throws MWException + * @throws JobQueueError */ protected function __construct( array $params ) { $this->domain = $params['domain'] ?? $params['wiki']; // b/c @@ -66,7 +66,7 @@ abstract class JobQueue { $this->order = $this->optimalOrder(); } if ( !in_array( $this->order, $this->supportedOrders() ) ) { - throw new MWException( __CLASS__ . " does not support '{$this->order}' order." ); + throw new JobQueueError( __CLASS__ . " does not support '{$this->order}' order." ); } $this->dupCache = wfGetCache( CACHE_ANYTHING ); $this->aggr = $params['aggregator'] ?? new JobQueueAggregatorNull( [] ); @@ -99,16 +99,16 @@ abstract class JobQueue { * * @param array $params * @return JobQueue - * @throws MWException + * @throws JobQueueError */ final public static function factory( array $params ) { $class = $params['class']; if ( !class_exists( $class ) ) { - throw new MWException( "Invalid job queue class '$class'." ); + throw new JobQueueError( "Invalid job queue class '$class'." ); } $obj = new $class( $params ); if ( !( $obj instanceof self ) ) { - throw new MWException( "Class '$class' is not a " . __CLASS__ . " class." ); + throw new JobQueueError( "Class '$class' is not a " . __CLASS__ . " class." ); } return $obj; @@ -318,7 +318,7 @@ abstract class JobQueue { * @param IJobSpecification[] $jobs * @param int $flags Bitfield (supports JobQueue::QOS_ATOMIC) * @return void - * @throws MWException + * @throws JobQueueError */ final public function batchPush( array $jobs, $flags = 0 ) { $this->assertNotReadOnly(); @@ -329,10 +329,10 @@ abstract class JobQueue { foreach ( $jobs as $job ) { if ( $job->getType() !== $this->type ) { - throw new MWException( + throw new JobQueueError( "Got '{$job->getType()}' job; expected a '{$this->type}' job." ); } elseif ( $job->getReleaseTimestamp() && !$this->supportsDelayedJobs() ) { - throw new MWException( + throw new JobQueueError( "Got delayed '{$job->getType()}' job; delays are not supported." ); } } @@ -359,7 +359,7 @@ abstract class JobQueue { * This requires $wgJobClasses to be set for the given job type. * Outside callers should use JobQueueGroup::pop() instead of this function. * - * @throws MWException + * @throws JobQueueError * @return Job|bool Returns false if there are no jobs */ final public function pop() { @@ -367,11 +367,11 @@ abstract class JobQueue { $this->assertNotReadOnly(); if ( !WikiMap::isCurrentWikiDbDomain( $this->domain ) ) { - throw new MWException( + throw new JobQueueError( "Cannot pop '{$this->type}' job off foreign '{$this->domain}' wiki queue." ); } elseif ( !isset( $wgJobClasses[$this->type] ) ) { // Do not pop jobs if there is no class for the queue type - throw new MWException( "Unrecognized job type '{$this->type}'." ); + throw new JobQueueError( "Unrecognized job type '{$this->type}'." ); } $job = $this->doPop(); @@ -407,12 +407,12 @@ abstract class JobQueue { * * @param Job $job * @return void - * @throws MWException + * @throws JobQueueError */ final public function ack( Job $job ) { $this->assertNotReadOnly(); if ( $job->getType() !== $this->type ) { - throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." ); + throw new JobQueueError( "Got '{$job->getType()}' job; expected '{$this->type}'." ); } $this->doAck( $job ); @@ -452,13 +452,13 @@ abstract class JobQueue { * This does nothing for certain queue classes. * * @param IJobSpecification $job - * @throws MWException + * @throws JobQueueError * @return bool */ final public function deduplicateRootJob( IJobSpecification $job ) { $this->assertNotReadOnly(); if ( $job->getType() !== $this->type ) { - throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." ); + throw new JobQueueError( "Got '{$job->getType()}' job; expected '{$this->type}'." ); } return $this->doDeduplicateRootJob( $job ); @@ -467,12 +467,12 @@ abstract class JobQueue { /** * @see JobQueue::deduplicateRootJob() * @param IJobSpecification $job - * @throws MWException + * @throws JobQueueError * @return bool */ protected function doDeduplicateRootJob( IJobSpecification $job ) { if ( !$job->hasRootJobParams() ) { - throw new MWException( "Cannot register root job; missing parameters." ); + throw new JobQueueError( "Cannot register root job; missing parameters." ); } $params = $job->getRootJobParams(); @@ -495,12 +495,12 @@ abstract class JobQueue { * Check if the "root" job of a given job has been superseded by a newer one * * @param Job $job - * @throws MWException + * @throws JobQueueError * @return bool */ final protected function isRootJobOldDuplicate( Job $job ) { if ( $job->getType() !== $this->type ) { - throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." ); + throw new JobQueueError( "Got '{$job->getType()}' job; expected '{$this->type}'." ); } $isDuplicate = $this->doIsRootJobOldDuplicate( $job ); @@ -555,10 +555,10 @@ abstract class JobQueue { /** * @see JobQueue::delete() - * @throws MWException + * @throws JobQueueError */ protected function doDelete() { - throw new MWException( "This method is not implemented." ); + throw new JobQueueError( "This method is not implemented." ); } /** @@ -659,7 +659,7 @@ abstract class JobQueue { * * @param array $types List of queues types * @return array|null (list of non-empty queue types) or null if unsupported - * @throws MWException + * @throws JobQueueError * @since 1.22 */ final public function getSiblingQueuesWithJobs( array $types ) { @@ -682,7 +682,7 @@ abstract class JobQueue { * * @param array $types List of queues types * @return array|null (job type => whether queue is empty) or null if unsupported - * @throws MWException + * @throws JobQueueError * @since 1.22 */ final public function getSiblingQueueSizes( array $types ) { diff --git a/includes/jobqueue/JobQueueGroup.php b/includes/jobqueue/JobQueueGroup.php index 13111491a2..7ae9713115 100644 --- a/includes/jobqueue/JobQueueGroup.php +++ b/includes/jobqueue/JobQueueGroup.php @@ -353,12 +353,14 @@ class JobQueueGroup { /** * Get the list of job types that have non-empty queues * - * @return array List of job types that have non-empty queues + * @return string[] List of job types that have non-empty queues */ public function getQueuesWithJobs() { $types = []; foreach ( $this->getCoalescedQueues() as $info ) { - $nonEmpty = $info['queue']->getSiblingQueuesWithJobs( $this->getQueueTypes() ); + /** @var JobQueue $queue */ + $queue = $info['queue']; + $nonEmpty = $queue->getSiblingQueuesWithJobs( $this->getQueueTypes() ); if ( is_array( $nonEmpty ) ) { // batching features supported $types = array_merge( $types, $nonEmpty ); } else { // we have to go through the queues in the bucket one-by-one @@ -376,12 +378,14 @@ class JobQueueGroup { /** * Get the size of the queus for a list of job types * - * @return array Map of (job type => size) + * @return int[] Map of (job type => size) */ public function getQueueSizes() { $sizeMap = []; foreach ( $this->getCoalescedQueues() as $info ) { - $sizes = $info['queue']->getSiblingQueueSizes( $this->getQueueTypes() ); + /** @var JobQueue $queue */ + $queue = $info['queue']; + $sizes = $queue->getSiblingQueueSizes( $this->getQueueTypes() ); if ( is_array( $sizes ) ) { // batching features supported $sizeMap = $sizeMap + $sizes; } else { // we have to go through the queues in the bucket one-by-one @@ -395,7 +399,7 @@ class JobQueueGroup { } /** - * @return array + * @return JobQueue[] */ protected function getCoalescedQueues() { global $wgJobTypeConf; -- 2.20.1