| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
require_once __DIR__ . '/Maintenance.php'; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 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 |
|
| 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 |
|
| 68 |
$maintClass = GetReplicaServer::class; |
| 69 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 70 |
|