Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
44.12% |
15 / 34 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| DatabaseLag | |
44.12% |
15 / 34 |
|
50.00% |
1 / 2 |
19.17 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
38.71% |
12 / 31 |
|
0.00% |
0 / 1 |
18.28 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Shows database lag |
| 4 | * |
| 5 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | * @ingroup Maintenance |
| 8 | */ |
| 9 | |
| 10 | use MediaWiki\Maintenance\Maintenance; |
| 11 | |
| 12 | // @codeCoverageIgnoreStart |
| 13 | require_once __DIR__ . '/Maintenance.php'; |
| 14 | // @codeCoverageIgnoreEnd |
| 15 | |
| 16 | /** |
| 17 | * Maintenance script to show database lag. |
| 18 | * |
| 19 | * @ingroup Maintenance |
| 20 | */ |
| 21 | class DatabaseLag extends Maintenance { |
| 22 | |
| 23 | /** @var bool */ |
| 24 | protected $stopReporting = false; |
| 25 | |
| 26 | public function __construct() { |
| 27 | parent::__construct(); |
| 28 | $this->addDescription( 'Shows database lag' ); |
| 29 | $this->addOption( 'r', "Don't exit immediately, but show the lag every 5 seconds" ); |
| 30 | } |
| 31 | |
| 32 | public function execute() { |
| 33 | $lb = $this->getServiceContainer()->getDBLoadBalancer(); |
| 34 | if ( $this->hasOption( 'r' ) ) { |
| 35 | $this->output( 'time ' ); |
| 36 | |
| 37 | $serverCount = $lb->getServerCount(); |
| 38 | for ( $i = 1; $i < $serverCount; $i++ ) { |
| 39 | $hostname = $lb->getServerName( $i ); |
| 40 | $this->output( sprintf( "%-12s ", $hostname ) ); |
| 41 | } |
| 42 | $this->output( "\n" ); |
| 43 | |
| 44 | do { |
| 45 | $lags = $lb->getLagTimes(); |
| 46 | unset( $lags[0] ); |
| 47 | $this->output( gmdate( 'H:i:s' ) . ' ' ); |
| 48 | foreach ( $lags as $lag ) { |
| 49 | $this->output( |
| 50 | sprintf( |
| 51 | "%-12s ", |
| 52 | $lag === false ? 'replication stopped or errored' : $lag |
| 53 | ) |
| 54 | ); |
| 55 | } |
| 56 | $this->output( "\n" ); |
| 57 | sleep( 5 ); |
| 58 | } while ( !$this->stopReporting ); |
| 59 | |
| 60 | } else { |
| 61 | $lags = $lb->getLagTimes(); |
| 62 | foreach ( $lags as $i => $lag ) { |
| 63 | $name = $lb->getServerName( $i ); |
| 64 | $this->output( |
| 65 | sprintf( |
| 66 | "%-20s %s\n", |
| 67 | $name, |
| 68 | $lag === false ? 'replication stopped or errored' : $lag |
| 69 | ) |
| 70 | ); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // @codeCoverageIgnoreStart |
| 77 | $maintClass = DatabaseLag::class; |
| 78 | require_once RUN_MAINTENANCE_IF_MAIN; |
| 79 | // @codeCoverageIgnoreEnd |