3 * Job queue aggregator code that uses PhpRedis.
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.
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.
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
21 * @author Aaron Schulz
25 * Class to handle tracking information about all queues using PhpRedis
30 class JobQueueAggregatorRedis
extends JobQueueAggregator
{
31 /** @var RedisConnectionPool */
36 * - redisConfig : An array of parameters to RedisConnectionPool::__construct().
37 * - redisServer : A hostname/port combination or the absolute path of a UNIX socket.
38 * If a hostname is specified but no port, the standard port number
39 * 6379 will be used. Required.
40 * @param array $params
42 protected function __construct( array $params ) {
43 parent
::__construct( $params );
44 $this->server
= $params['redisServer'];
45 $this->redisPool
= RedisConnectionPool
::singleton( $params['redisConfig'] );
49 * @see JobQueueAggregator::doNotifyQueueEmpty()
51 protected function doNotifyQueueEmpty( $wiki, $type ) {
52 $conn = $this->getConnection();
57 $conn->hDel( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ) );
59 } catch ( RedisException
$e ) {
60 $this->handleException( $conn, $e );
66 * @see JobQueueAggregator::doNotifyQueueNonEmpty()
68 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
69 $conn = $this->getConnection();
74 $conn->hSet( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ), time() );
76 } catch ( RedisException
$e ) {
77 $this->handleException( $conn, $e );
83 * @see JobQueueAggregator::doAllGetReadyWikiQueues()
85 protected function doGetAllReadyWikiQueues() {
86 $conn = $this->getConnection();
91 $conn->multi( Redis
::PIPELINE
);
92 $conn->exists( $this->getReadyQueueKey() );
93 $conn->hGetAll( $this->getReadyQueueKey() );
94 list( $exists, $map ) = $conn->exec();
96 if ( $exists ) { // cache hit
97 $pendingDBs = array(); // (type => list of wikis)
98 foreach ( $map as $key => $time ) {
99 list( $type, $wiki ) = $this->dencQueueName( $key );
100 $pendingDBs[$type][] = $wiki;
102 } else { // cache miss
103 $pendingDBs = $this->findPendingWikiQueues(); // (type => list of wikis)
107 foreach ( $pendingDBs as $type => $wikis ) {
108 foreach ( $wikis as $wiki ) {
109 $map[$this->encQueueName( $type, $wiki )] = $now;
112 $conn->hMSet( $this->getReadyQueueKey(), $map );
116 } catch ( RedisException
$e ) {
117 $this->handleException( $conn, $e );
123 * Get a connection to the server that handles all sub-queues for this queue
125 * @return Array (server name, Redis instance)
126 * @throws MWException
128 protected function getConnection() {
129 return $this->redisPool
->getConnection( $this->server
);
133 * @param RedisConnRef $conn
134 * @param RedisException $e
137 protected function handleException( RedisConnRef
$conn, $e ) {
138 $this->redisPool
->handleException( $this->server
, $conn, $e );
144 private function getReadyQueueKey() {
145 return "jobqueue:aggregator:h-ready-queues:v1"; // global
149 * @param string $type
150 * @param string $wiki
153 private function encQueueName( $type, $wiki ) {
154 return rawurlencode( $type ) . '/' . rawurlencode( $wiki );
158 * @param string $name
161 private function dencQueueName( $name ) {
162 list( $type, $wiki ) = explode( '/', $name, 2 );
163 return array( rawurldecode( $type ), rawurldecode( $wiki ) );