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