MediaWiki  master
lag.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
27 
33 class DatabaseLag extends Maintenance {
34  public function __construct() {
35  parent::__construct();
36  $this->addDescription( 'Shows database lag' );
37  $this->addOption( 'r', "Don't exit immediately, but show the lag every 5 seconds" );
38  }
39 
40  public function execute() {
41  $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
42  if ( $this->hasOption( 'r' ) ) {
43  $this->output( 'time ' );
44 
45  $serverCount = $lb->getServerCount();
46  for ( $i = 1; $i < $serverCount; $i++ ) {
47  $hostname = $lb->getServerName( $i );
48  $this->output( sprintf( "%-12s ", $hostname ) );
49  }
50  $this->output( "\n" );
51 
52  // @phan-suppress-next-line PhanInfiniteLoop
53  while ( 1 ) {
54  $lags = $lb->getLagTimes();
55  unset( $lags[0] );
56  $this->output( gmdate( 'H:i:s' ) . ' ' );
57  foreach ( $lags as $lag ) {
58  $this->output( sprintf( "%-12s ", $lag === false ? 'false' : $lag ) );
59  }
60  $this->output( "\n" );
61  sleep( 5 );
62  }
63  } else {
64  $lags = $lb->getLagTimes();
65  foreach ( $lags as $i => $lag ) {
66  $name = $lb->getServerName( $i );
67  $this->output( sprintf( "%-20s %s\n", $name, $lag === false ? 'false' : $lag ) );
68  }
69  }
70  }
71 }
72 
73 $maintClass = DatabaseLag::class;
74 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
DatabaseLag\execute
execute()
Do the actual work.
Definition: lag.php:40
DatabaseLag
Maintenance script to show database lag.
Definition: lag.php:33
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
DatabaseLag\__construct
__construct()
Default constructor.
Definition: lag.php:34
$maintClass
$maintClass
Definition: lag.php:73
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266