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