Merge "(Bug 44987) Allow n=form in plural syntax"
[lhc/web/wiklou.git] / includes / job / JobQueueAggregatorRedis.php
1 <?php
2 /**
3 * Job queue aggregator code that uses PhpRedis.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Aaron Schulz
22 */
23
24 /**
25 * Class to handle tracking information about all queues using PhpRedis
26 *
27 * @ingroup JobQueue
28 * @since 1.21
29 */
30 class JobQueueAggregatorRedis extends JobQueueAggregator {
31 /** @var RedisConnectionPool */
32 protected $redisPool;
33
34 /**
35 * @params include:
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
41 */
42 protected function __construct( array $params ) {
43 parent::__construct( $params );
44 $this->server = $params['redisServer'];
45 $this->redisPool = RedisConnectionPool::singleton( $params['redisConfig'] );
46 }
47
48 /**
49 * @see JobQueueAggregator::doNotifyQueueEmpty()
50 */
51 protected function doNotifyQueueEmpty( $wiki, $type ) {
52 $conn = $this->getConnection();
53 if ( !$conn ) {
54 return false;
55 }
56 try {
57 $conn->hDel( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ) );
58 return true;
59 } catch ( RedisException $e ) {
60 $this->handleException( $conn, $e );
61 return false;
62 }
63 }
64
65 /**
66 * @see JobQueueAggregator::doNotifyQueueNonEmpty()
67 */
68 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
69 $conn = $this->getConnection();
70 if ( !$conn ) {
71 return false;
72 }
73 try {
74 $conn->hSet( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ), time() );
75 return true;
76 } catch ( RedisException $e ) {
77 $this->handleException( $conn, $e );
78 return false;
79 }
80 }
81
82 /**
83 * @see JobQueueAggregator::doAllGetReadyWikiQueues()
84 */
85 protected function doGetAllReadyWikiQueues() {
86 $conn = $this->getConnection();
87 if ( !$conn ) {
88 return array();
89 }
90 try {
91 $conn->multi( Redis::PIPELINE );
92 $conn->exists( $this->getReadyQueueKey() );
93 $conn->hGetAll( $this->getReadyQueueKey() );
94 list( $exists, $map ) = $conn->exec();
95
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;
101 }
102 } else { // cache miss
103 $pendingDBs = $this->findPendingWikiQueues(); // (type => list of wikis)
104
105 $now = time();
106 $map = array();
107 foreach ( $pendingDBs as $type => $wikis ) {
108 foreach ( $wikis as $wiki ) {
109 $map[$this->encQueueName( $type, $wiki )] = $now;
110 }
111 }
112 $conn->hMSet( $this->getReadyQueueKey(), $map );
113 }
114
115 return $pendingDBs;
116 } catch ( RedisException $e ) {
117 $this->handleException( $conn, $e );
118 return array();
119 }
120 }
121
122 /**
123 * Get a connection to the server that handles all sub-queues for this queue
124 *
125 * @return Array (server name, Redis instance)
126 * @throws MWException
127 */
128 protected function getConnection() {
129 return $this->redisPool->getConnection( $this->server );
130 }
131
132 /**
133 * @param RedisConnRef $conn
134 * @param RedisException $e
135 * @return void
136 */
137 protected function handleException( RedisConnRef $conn, $e ) {
138 $this->redisPool->handleException( $this->server, $conn, $e );
139 }
140
141 /**
142 * @return string
143 */
144 private function getReadyQueueKey() {
145 return "jobqueue:aggregator:h-ready-queues:v1"; // global
146 }
147
148 /**
149 * @param string $type
150 * @param string $wiki
151 * @return string
152 */
153 private function encQueueName( $type, $wiki ) {
154 return rawurlencode( $type ) . '/' . rawurlencode( $wiki );
155 }
156
157 /**
158 * @param string $name
159 * @return string
160 */
161 private function dencQueueName( $name ) {
162 list( $type, $wiki ) = explode( '/', $name, 2 );
163 return array( rawurldecode( $type ), rawurldecode( $wiki ) );
164 }
165 }