Debian Codegraph @

maintenance/lag.php

raw symbols SCIP
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
24 // @codeCoverageIgnoreStart
25 require_once __DIR__ . '/Maintenance.php';
26 // @codeCoverageIgnoreEnd
27
28 /**
29 * Maintenance script to show database lag.
30 *
31 * @ingroup Maintenance
32 */
33 class DatabaseLag extends Maintenance {
34
35 /** @var bool */
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;
90 require_once RUN_MAINTENANCE_IF_MAIN;
91 // @codeCoverageIgnoreEnd