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