Debian Codegraph @

maintenance/getReplicaServer.php

raw symbols SCIP
1 <?php
2 /**
3 * Reports the hostname of a replica DB server.
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 * @ingroup Maintenance
22 */
23
24 // @codeCoverageIgnoreStart
25 require_once __DIR__ . '/Maintenance.php';
26 // @codeCoverageIgnoreEnd
27
28 /**
29 * Maintenance script that reports the hostname of a replica DB server.
30 *
31 * @ingroup Maintenance
32 */
33 class GetReplicaServer extends Maintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->addOption( "group", "Query group to check specifically" );
37 $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
38 $this->addDescription( 'Report the hostname of a replica DB server' );
39 }
40
41 public function execute() {
42 $lbf = $this->getServiceContainer()->getDBLoadBalancerFactory();
43 if ( $this->hasOption( 'cluster' ) ) {
44 try {
45 $lb = $lbf->getExternalLB( $this->getOption( 'cluster' ) );
46 } catch ( InvalidArgumentException $e ) {
47 $this->fatalError( 'Error: ' . $e->getMessage() );
48 }
49 } else {
50 $lb = $lbf->getMainLB();
51 }
52
53 $group = $this->getOption( 'group', false );
54 $index = $lb->getReaderIndex( $group );
55 if ( $index === false && $group ) {
56 // retry without the group; it may not exist
57 $index = $lb->getReaderIndex( false );
58 }
59 if ( $index === false ) {
60 $this->fatalError( 'Error: unable to get reader index' );
61 }
62
63 $this->output( $lb->getServerName( $index ) . "\n" );
64 }
65 }
66
67 // @codeCoverageIgnoreStart
68 $maintClass = GetReplicaServer::class;
69 require_once RUN_MAINTENANCE_IF_MAIN;
70 // @codeCoverageIgnoreEnd