Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
44.12% covered (danger)
44.12%
15 / 34
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DatabaseLag
44.12% covered (danger)
44.12%
15 / 34
50.00% covered (danger)
50.00%
1 / 2
19.17
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 execute
38.71% covered (danger)
38.71%
12 / 31
0.00% covered (danger)
0.00%
0 / 1
18.28
1<?php
2/**
3 * Shows database lag
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24use MediaWiki\Maintenance\Maintenance;
25
26// @codeCoverageIgnoreStart
27require_once __DIR__ . '/Maintenance.php';
28// @codeCoverageIgnoreEnd
29
30/**
31 * Maintenance script to show database lag.
32 *
33 * @ingroup Maintenance
34 */
35class DatabaseLag extends Maintenance {
36
37    /** @var bool */
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