Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
40.54% covered (danger)
40.54%
15 / 37
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
24require_once __DIR__ . '/Maintenance.php';
25
26/**
27 * Maintenance script to show database lag.
28 *
29 * @ingroup Maintenance
30 */
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;