MediaWiki master
getReplicaServer.php
Go to the documentation of this file.
1<?php
24require_once __DIR__ . '/Maintenance.php';
25
32 public function __construct() {
33 parent::__construct();
34 $this->addOption( "group", "Query group to check specifically" );
35 $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
36 $this->addDescription( 'Report the hostname of a replica DB server' );
37 }
38
39 public function execute() {
40 $lbf = $this->getServiceContainer()->getDBLoadBalancerFactory();
41 if ( $this->hasOption( 'cluster' ) ) {
42 try {
43 $lb = $lbf->getExternalLB( $this->getOption( 'cluster' ) );
44 } catch ( InvalidArgumentException $e ) {
45 $this->fatalError( 'Error: ' . $e->getMessage() );
46 }
47 } else {
48 $lb = $lbf->getMainLB();
49 }
50
51 $group = $this->getOption( 'group', false );
52 $index = $lb->getReaderIndex( $group );
53 if ( $index === false && $group ) {
54 // retry without the group; it may not exist
55 $index = $lb->getReaderIndex( false );
56 }
57 if ( $index === false ) {
58 $this->fatalError( 'Error: unable to get reader index' );
59 }
60
61 $this->output( $lb->getServerName( $index ) . "\n" );
62 }
63}
64
65$maintClass = GetReplicaServer::class;
66require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script that reports the hostname of a replica DB server.
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.