MediaWiki master
lag.php
Go to the documentation of this file.
1<?php
24// @codeCoverageIgnoreStart
25require_once __DIR__ . '/Maintenance.php';
26// @codeCoverageIgnoreEnd
27
33class DatabaseLag extends Maintenance {
34
36 protected $stopReporting = false;
37
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription( 'Shows database lag' );
41 $this->addOption( 'r', "Don't exit immediately, but show the lag every 5 seconds" );
42 }
43
44 public function execute() {
45 $lb = $this->getServiceContainer()->getDBLoadBalancer();
46 if ( $this->hasOption( 'r' ) ) {
47 $this->output( 'time ' );
48
49 $serverCount = $lb->getServerCount();
50 for ( $i = 1; $i < $serverCount; $i++ ) {
51 $hostname = $lb->getServerName( $i );
52 $this->output( sprintf( "%-12s ", $hostname ) );
53 }
54 $this->output( "\n" );
55
56 do {
57 $lags = $lb->getLagTimes();
58 unset( $lags[0] );
59 $this->output( gmdate( 'H:i:s' ) . ' ' );
60 foreach ( $lags as $lag ) {
61 $this->output(
62 sprintf(
63 "%-12s ",
64 $lag === false ? 'replication stopped or errored' : $lag
65 )
66 );
67 }
68 $this->output( "\n" );
69 sleep( 5 );
70 } while ( !$this->stopReporting );
71
72 } else {
73 $lags = $lb->getLagTimes();
74 foreach ( $lags as $i => $lag ) {
75 $name = $lb->getServerName( $i );
76 $this->output(
77 sprintf(
78 "%-20s %s\n",
79 $name,
80 $lag === false ? 'replication stopped or errored' : $lag
81 )
82 );
83 }
84 }
85 }
86}
87
88// @codeCoverageIgnoreStart
89$maintClass = DatabaseLag::class;
90require_once RUN_MAINTENANCE_IF_MAIN;
91// @codeCoverageIgnoreEnd
Maintenance script to show database lag.
Definition lag.php:33
execute()
Do the actual work.
Definition lag.php:44
__construct()
Default constructor.
Definition lag.php:38
bool $stopReporting
Definition lag.php:36
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:89