aae800ebf0fcf25408642442eee5609389cac0f2
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
31 class JobQueueAggregatorRedis
extends JobQueueAggregator
{
32 /** @var RedisConnectionPool */
37 * - redisConfig : An array of parameters to RedisConnectionPool::__construct().
38 * - redisServer : A hostname/port combination or the absolute path of a UNIX socket.
39 * If a hostname is specified but no port, the standard port number
40 * 6379 will be used. Required.
41 * @param array $params
43 protected function __construct( array $params ) {
44 parent
::__construct( $params );
45 $this->server
= $params['redisServer'];
46 $this->redisPool
= RedisConnectionPool
::singleton( $params['redisConfig'] );
50 * @see JobQueueAggregator::doNotifyQueueEmpty()
52 protected function doNotifyQueueEmpty( $wiki, $type ) {
53 $conn = $this->getConnection();
58 $conn->hDel( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ) );
60 } catch ( RedisException
$e ) {
61 $this->handleException( $conn, $e );
67 * @see JobQueueAggregator::doNotifyQueueNonEmpty()
69 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
70 $conn = $this->getConnection();
75 $conn->hSet( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ), time() );
77 } catch ( RedisException
$e ) {
78 $this->handleException( $conn, $e );
84 * @see JobQueueAggregator::doAllGetReadyWikiQueues()
86 protected function doGetAllReadyWikiQueues() {
87 $conn = $this->getConnection();
92 $conn->multi( Redis
::PIPELINE
);
93 $conn->exists( $this->getReadyQueueKey() );
94 $conn->hGetAll( $this->getReadyQueueKey() );
95 list( $exists, $map ) = $conn->exec();
97 if ( $exists ) { // cache hit
98 $pendingDBs = array(); // (type => list of wikis)
99 foreach ( $map as $key => $time ) {
100 list( $type, $wiki ) = $this->dencQueueName( $key );
101 $pendingDBs[$type][] = $wiki;
103 } else { // cache miss
104 $pendingDBs = $this->findPendingWikiQueues(); // (type => list of wikis)
108 foreach ( $pendingDBs as $type => $wikis ) {
109 foreach ( $wikis as $wiki ) {
110 $map[$this->encQueueName( $type, $wiki )] = $now;
113 $conn->hMSet( $this->getReadyQueueKey(), $map );
117 } catch ( RedisException
$e ) {
118 $this->handleException( $conn, $e );
124 * @see JobQueueAggregator::doPurge()
126 protected function doPurge() {
127 $conn = $this->getConnection();
132 $conn->delete( $this->getReadyQueueKey() );
133 } catch ( RedisException
$e ) {
134 $this->handleException( $conn, $e );
141 * Get a connection to the server that handles all sub-queues for this queue
143 * @return Array (server name, Redis instance)
144 * @throws MWException
146 protected function getConnection() {
147 return $this->redisPool
->getConnection( $this->server
);
151 * @param RedisConnRef $conn
152 * @param RedisException $e
155 protected function handleException( RedisConnRef
$conn, $e ) {
156 $this->redisPool
->handleException( $this->server
, $conn, $e );
162 private function getReadyQueueKey() {
163 return "jobqueue:aggregator:h-ready-queues:v1"; // global
167 * @param string $type
168 * @param string $wiki
171 private function encQueueName( $type, $wiki ) {
172 return rawurlencode( $type ) . '/' . rawurlencode( $wiki );
176 * @param string $name
179 private function dencQueueName( $name ) {
180 list( $type, $wiki ) = explode( '/', $name, 2 );
181 return array( rawurldecode( $type ), rawurldecode( $wiki ) );