MediaWiki  1.34.0
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  echo 'time ';
44 
45  $serverCount = $lb->getServerCount();
46  for ( $i = 1; $i < $serverCount; $i++ ) {
47  $hostname = $lb->getServerName( $i );
48  printf( "%-12s ", $hostname );
49  }
50  echo "\n";
51 
52  while ( 1 ) {
53  $lags = $lb->getLagTimes();
54  unset( $lags[0] );
55  echo gmdate( 'H:i:s' ) . ' ';
56  foreach ( $lags as $lag ) {
57  printf( "%-12s ", $lag === false ? 'false' : $lag );
58  }
59  echo "\n";
60  sleep( 5 );
61  }
62  } else {
63  $lags = $lb->getLagTimes();
64  foreach ( $lags as $i => $lag ) {
65  $name = $lb->getServerName( $i );
66  $this->output( sprintf( "%-20s %s\n", $name, $lag === false ? 'false' : $lag ) );
67  }
68  }
69  }
70 }
71 
72 $maintClass = DatabaseLag::class;
73 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
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:267
DatabaseLag\__construct
__construct()
Default constructor.
Definition: lag.php:34
$maintClass
$maintClass
Definition: lag.php:72
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288