From: Aaron Schulz Date: Sun, 3 Mar 2013 04:41:38 +0000 (-0800) Subject: [JobQueue] Cleaned up handling of orders a bit. X-Git-Tag: 1.31.0-rc.0~20425 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22upgrade%22%2C%22reinstall=non%22%29%20.%20%22?a=commitdiff_plain;h=5090f78e11ea671769de69da02fe05ac6540a762;p=lhc%2Fweb%2Fwiklou.git [JobQueue] Cleaned up handling of orders a bit. Change-Id: I83d060e0db97d5bebf042041b0bd20563b093418 --- diff --git a/includes/job/JobQueue.php b/includes/job/JobQueue.php index 8ade0d564a..641a61de98 100644 --- a/includes/job/JobQueue.php +++ b/includes/job/JobQueue.php @@ -43,9 +43,16 @@ abstract class JobQueue { protected function __construct( array $params ) { $this->wiki = $params['wiki']; $this->type = $params['type']; - $this->order = isset( $params['order'] ) ? $params['order'] : 'random'; $this->claimTTL = isset( $params['claimTTL'] ) ? $params['claimTTL'] : 0; $this->maxTries = isset( $params['maxTries'] ) ? $params['maxTries'] : 3; + if ( isset( $params['order'] ) && $params['order'] !== 'any' ) { + $this->order = $params['order']; + } else { + $this->order = $this->optimalOrder(); + } + if ( !in_array( $this->order, $this->supportedOrders() ) ) { + throw new MWException( __CLASS__ . " does not support '{$this->order}' order." ); + } } /** @@ -60,10 +67,10 @@ abstract class JobQueue { * job runners since jobs can take different times to finish once popped. * If "timestamp" is used, the queue will at least be loosely ordered * by timestamp, allowing for some jobs to be popped off out of order. - * If "random" is used, pop() will pick jobs in random order. This might be - * useful for improving concurrency depending on the queue storage medium. - * Note that "random" really means "don't care", so it may actually be FIFO - * or only weakly random (e.g. pop() takes one of the first X jobs randomly). + * If "random" is used, pop() will pick jobs in random order. + * Note that it may only be weakly random (e.g. a lottery of the oldest X). + * If "any" is choosen, the queue will use whatever order is the fastest. + * This might be useful for improving concurrency for job acquisition. * - claimTTL : If supported, the queue will recycle jobs that have been popped * but not acknowledged as completed after this many seconds. Recycling * of jobs simple means re-inserting them into the queue. Jobs can be @@ -101,6 +108,23 @@ abstract class JobQueue { return $this->type; } + /** + * @return string One of (random, timestamp, fifo) + */ + final public function getOrder() { + return $this->order; + } + + /** + * @return Array Subset of (random, timestamp, fifo) + */ + abstract public function supportedOrders(); + + /** + * @return string One of (random, timestamp, fifo) + */ + abstract protected function optimalOrder(); + /** * Quickly check if the queue is empty (has no available jobs). * Queue classes should use caching if they are any slower without memcached. diff --git a/includes/job/JobQueueDB.php b/includes/job/JobQueueDB.php index ada0ac4a40..eb01c3e9a8 100644 --- a/includes/job/JobQueueDB.php +++ b/includes/job/JobQueueDB.php @@ -50,6 +50,14 @@ class JobQueueDB extends JobQueue { $this->cluster = isset( $params['cluster'] ) ? $params['cluster'] : false; } + public function supportedOrders() { + return array( 'random', 'timestamp', 'fifo' ); + } + + protected function optimalOrder() { + return 'random'; + } + /** * @see JobQueue::doIsEmpty() * @return bool diff --git a/includes/job/JobQueueRedis.php b/includes/job/JobQueueRedis.php index 7dc9900dba..8e7588ec26 100644 --- a/includes/job/JobQueueRedis.php +++ b/includes/job/JobQueueRedis.php @@ -52,6 +52,14 @@ class JobQueueRedis extends JobQueue { $this->redisPool = RedisConnectionPool::singleton( $params['redisConfig'] ); } + public function supportedOrders() { + return array( 'timestamp', 'fifo' ); + } + + protected function optimalOrder() { + return 'fifo'; + } + /** * @see JobQueue::doIsEmpty() * @return bool