Fix ApiBase::getErrorFromStatus() and ApiMessages
[mediawiki.git] / includes / jobqueue / aggregator / JobQueueAggregatorRedis.php
blob906a48e379a8f754afd764bfd573c397358eaf10
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
24 /**
25 * Class to handle tracking information about all queues using PhpRedis
27 * The mediawiki/services/jobrunner background service must be set up and running.
29 * @ingroup JobQueue
30 * @ingroup Redis
31 * @since 1.21
33 class JobQueueAggregatorRedis extends JobQueueAggregator {
34 /** @var RedisConnectionPool */
35 protected $redisPool;
36 /** @var array List of Redis server addresses */
37 protected $servers;
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.
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'] );
57 protected function doNotifyQueueEmpty( $wiki, $type ) {
58 return true; // managed by the service
61 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
62 return true; // managed by the service
65 protected function doGetAllReadyWikiQueues() {
66 $conn = $this->getConnection();
67 if ( !$conn ) {
68 return [];
70 try {
71 $map = $conn->hGetAll( $this->getReadyQueueKey() );
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;
80 } else {
81 throw new UnexpectedValueException(
82 "No queue listing found; make sure redisJobChronService is running."
86 return $pendingDBs;
87 } catch ( RedisException $e ) {
88 $this->redisPool->handleError( $conn, $e );
90 return [];
94 protected function doPurge() {
95 return true; // fully and only refreshed by the service
98 /**
99 * Get a connection to the server that handles all sub-queues for this queue
101 * @return RedisConnRef|bool Returns false on failure
102 * @throws MWException
104 protected function getConnection() {
105 $conn = false;
106 foreach ( $this->servers as $server ) {
107 $conn = $this->redisPool->getConnection( $server );
108 if ( $conn ) {
109 break;
113 return $conn;
117 * @return string
119 private function getReadyQueueKey() {
120 return "jobqueue:aggregator:h-ready-queues:v2"; // global
124 * @param string $name
125 * @return string
127 private function decodeQueueName( $name ) {
128 list( $type, $wiki ) = explode( '/', $name, 2 );
130 return [ rawurldecode( $type ), rawurldecode( $wiki ) ];