| 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 DatabaseLag extends Maintenance { |
| 34 |
|
| 35 |
|
| 36 |
protected $stopReporting = false; |
| 37 |
|
| 38 |
public function __construct() { |
| 39 |
parent::__construct(); |
| 40 |
$this->addDescription( 'Shows database lag' ); |
| 41 |
$this->addOption( 'r', "Don't exit immediately, but show the lag every 5 seconds" ); |
| 42 |
} |
| 43 |
|
| 44 |
public function execute() { |
| 45 |
$lb = $this->getServiceContainer()->getDBLoadBalancer(); |
| 46 |
if ( $this->hasOption( 'r' ) ) { |
| 47 |
$this->output( 'time ' ); |
| 48 |
|
| 49 |
$serverCount = $lb->getServerCount(); |
| 50 |
for ( $i = 1; $i < $serverCount; $i++ ) { |
| 51 |
$hostname = $lb->getServerName( $i ); |
| 52 |
$this->output( sprintf( "%-12s ", $hostname ) ); |
| 53 |
} |
| 54 |
$this->output( "\n" ); |
| 55 |
|
| 56 |
do { |
| 57 |
$lags = $lb->getLagTimes(); |
| 58 |
unset( $lags[0] ); |
| 59 |
$this->output( gmdate( 'H:i:s' ) . ' ' ); |
| 60 |
foreach ( $lags as $lag ) { |
| 61 |
$this->output( |
| 62 |
sprintf( |
| 63 |
"%-12s ", |
| 64 |
$lag === false ? 'replication stopped or errored' : $lag |
| 65 |
) |
| 66 |
); |
| 67 |
} |
| 68 |
$this->output( "\n" ); |
| 69 |
sleep( 5 ); |
| 70 |
} while ( !$this->stopReporting ); |
| 71 |
|
| 72 |
} else { |
| 73 |
$lags = $lb->getLagTimes(); |
| 74 |
foreach ( $lags as $i => $lag ) { |
| 75 |
$name = $lb->getServerName( $i ); |
| 76 |
$this->output( |
| 77 |
sprintf( |
| 78 |
"%-20s %s\n", |
| 79 |
$name, |
| 80 |
$lag === false ? 'replication stopped or errored' : $lag |
| 81 |
) |
| 82 |
); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
$maintClass = DatabaseLag::class; |
| 90 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 91 |
|