MediaWiki master
lag.php
Go to the documentation of this file.
1<?php
24require_once __DIR__ . '/Maintenance.php';
25
31class DatabaseLag extends Maintenance {
32
33 protected $stopReporting = false;
34
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( 'Shows database lag' );
38 $this->addOption( 'r', "Don't exit immediately, but show the lag every 5 seconds" );
39 }
40
41 public function execute() {
42 $lb = $this->getServiceContainer()->getDBLoadBalancer();
43 if ( $this->hasOption( 'r' ) ) {
44 $this->output( 'time ' );
45
46 $serverCount = $lb->getServerCount();
47 for ( $i = 1; $i < $serverCount; $i++ ) {
48 $hostname = $lb->getServerName( $i );
49 $this->output( sprintf( "%-12s ", $hostname ) );
50 }
51 $this->output( "\n" );
52
53 do {
54 $lags = $lb->getLagTimes();
55 unset( $lags[0] );
56 $this->output( gmdate( 'H:i:s' ) . ' ' );
57 foreach ( $lags as $lag ) {
58 $this->output(
59 sprintf(
60 "%-12s ",
61 $lag === false ? 'replication stopped or errored' : $lag
62 )
63 );
64 }
65 $this->output( "\n" );
66 sleep( 5 );
67 } while ( !$this->stopReporting );
68
69 } else {
70 $lags = $lb->getLagTimes();
71 foreach ( $lags as $i => $lag ) {
72 $name = $lb->getServerName( $i );
73 $this->output(
74 sprintf(
75 "%-20s %s\n",
76 $name,
77 $lag === false ? 'replication stopped or errored' : $lag
78 )
79 );
80 }
81 }
82 }
83}
84
85$maintClass = DatabaseLag::class;
86require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script to show database lag.
Definition lag.php:31
execute()
Do the actual work.
Definition lag.php:41
$stopReporting
Definition lag.php:33
__construct()
Default constructor.
Definition lag.php:35
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
$maintClass
Definition lag.php:85