Merge "mw.Upload.BookletLayout/Dialog: Add determinate progress bar"
[lhc/web/wiklou.git] / includes / jobqueue / aggregator / 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 * The mediawiki/services/jobrunner background service must be set up and running.
28 *
29 * @ingroup JobQueue
30 * @ingroup Redis
31 * @since 1.21
32 */
33 class JobQueueAggregatorRedis extends JobQueueAggregator {
34 /** @var RedisConnectionPool */
35 protected $redisPool;
36 /** @var array List of Redis server addresses */
37 protected $servers;
38
39 /**
40 * @param array $params Possible keys:
41 * - redisConfig : An array of parameters to RedisConnectionPool::__construct().
42 * - redisServers : Array of server entries, the first being the primary and the
43 * others being fallback servers. Each entry is either a hostname/port
44 * combination or the absolute path of a UNIX socket.
45 * If a hostname is specified but no port, the standard port number
46 * 6379 will be used. Required.
47 */
48 public function __construct( array $params ) {
49 parent::__construct( $params );
50 $this->servers = isset( $params['redisServers'] )
51 ? $params['redisServers']
52 : [ $params['redisServer'] ]; // b/c
53 $params['redisConfig']['serializer'] = 'none';
54 $this->redisPool = RedisConnectionPool::singleton( $params['redisConfig'] );
55 }
56
57 protected function doNotifyQueueEmpty( $wiki, $type ) {
58 return true; // managed by the service
59 }
60
61 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
62 return true; // managed by the service
63 }
64
65 protected function doGetAllReadyWikiQueues() {
66 $conn = $this->getConnection();
67 if ( !$conn ) {
68 return [];
69 }
70 try {
71 $map = $conn->hGetAll( $this->getReadyQueueKey() );
72
73 if ( is_array( $map ) && isset( $map['_epoch'] ) ) {
74 unset( $map['_epoch'] ); // ignore
75 $pendingDBs = []; // (type => list of wikis)
76 foreach ( $map as $key => $time ) {
77 list( $type, $wiki ) = $this->decodeQueueName( $key );
78 $pendingDBs[$type][] = $wiki;
79 }
80 } else {
81 throw new UnexpectedValueException(
82 "No queue listing found; make sure redisJobChronService is running."
83 );
84 }
85
86 return $pendingDBs;
87 } catch ( RedisException $e ) {
88 $this->redisPool->handleError( $conn, $e );
89
90 return [];
91 }
92 }
93
94 protected function doPurge() {
95 return true; // fully and only refreshed by the service
96 }
97
98 /**
99 * Get a connection to the server that handles all sub-queues for this queue
100 *
101 * @return RedisConnRef|bool Returns false on failure
102 * @throws MWException
103 */
104 protected function getConnection() {
105 $conn = false;
106 foreach ( $this->servers as $server ) {
107 $conn = $this->redisPool->getConnection( $server );
108 if ( $conn ) {
109 break;
110 }
111 }
112
113 return $conn;
114 }
115
116 /**
117 * @return string
118 */
119 private function getReadyQueueKey() {
120 return "jobqueue:aggregator:h-ready-queues:v2"; // global
121 }
122
123 /**
124 * @param string $name
125 * @return string
126 */
127 private function decodeQueueName( $name ) {
128 list( $type, $wiki ) = explode( '/', $name, 2 );
129
130 return [ rawurldecode( $type ), rawurldecode( $wiki ) ];
131 }
132 }