MediaWiki master
lag.php
Go to the documentation of this file.
1<?php
11
12// @codeCoverageIgnoreStart
13require_once __DIR__ . '/Maintenance.php';
14// @codeCoverageIgnoreEnd
15
21class DatabaseLag extends Maintenance {
22
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;
78require_once RUN_MAINTENANCE_IF_MAIN;
79// @codeCoverageIgnoreEnd
Maintenance script to show database lag.
Definition lag.php:21
execute()
Do the actual work.
Definition lag.php:32
__construct()
Default constructor.
Definition lag.php:26
bool $stopReporting
Definition lag.php:24
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
$maintClass
Definition lag.php:77