Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / jobqueue / aggregator / JobQueueAggregatorRedis.php
blobd9457c603f16138ec2eb873605e997311d100ccf
1 <?php
2 /**
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
20 * @file
21 * @author Aaron Schulz
23 use Psr\Log\LoggerInterface;
25 /**
26 * Class to handle tracking information about all queues using PhpRedis
28 * The mediawiki/services/jobrunner background service must be set up and running.
30 * @ingroup JobQueue
31 * @ingroup Redis
32 * @since 1.21
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;
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.
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' );
61 protected function doNotifyQueueEmpty( $wiki, $type ) {
62 return true; // managed by the service
65 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
66 return true; // managed by the service
69 protected function doGetAllReadyWikiQueues() {
70 $conn = $this->getConnection();
71 if ( !$conn ) {
72 return [];
74 try {
75 $map = $conn->hGetAll( $this->getReadyQueueKey() );
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;
84 } else {
85 throw new UnexpectedValueException(
86 "No queue listing found; make sure redisJobChronService is running."
90 return $pendingDBs;
91 } catch ( RedisException $e ) {
92 $this->redisPool->handleError( $conn, $e );
94 return [];
98 protected function doPurge() {
99 return true; // fully and only refreshed by the service
103 * Get a connection to the server that handles all sub-queues for this queue
105 * @return RedisConnRef|bool Returns false on failure
106 * @throws MWException
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;
117 return $conn;
121 * @return string
123 private function getReadyQueueKey() {
124 return "jobqueue:aggregator:h-ready-queues:v2"; // global
128 * @param string $name
129 * @return string[]
131 private function decodeQueueName( $name ) {
132 list( $type, $wiki ) = explode( '/', $name, 2 );
134 return [ rawurldecode( $type ), rawurldecode( $wiki ) ];