Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CirrusNeedsToBeBuilt
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 2
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2
3namespace CirrusSearch\Maintenance;
4
5use CirrusSearch\SearchConfig;
6
7/**
8 * Returns zero status if a Cirrus index needs to be built for this wiki.  If
9 * Elasticsearch doesn't look to be up it'll wait a minute for it to come up.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 */
26
27$IP = getenv( 'MW_INSTALL_PATH' );
28if ( $IP === false ) {
29    $IP = __DIR__ . '/../../..';
30}
31require_once "$IP/maintenance/Maintenance.php";
32require_once __DIR__ . '/../includes/Maintenance/Maintenance.php';
33
34class CirrusNeedsToBeBuilt extends Maintenance {
35    public function __construct() {
36        parent::__construct();
37        $this->addDescription( "Update the configuration or contents of all search indices. Always operates on a single cluster." );
38    }
39
40    public function execute() {
41        $indexPattern = $this->getSearchConfig()->get( SearchConfig::INDEX_BASE_NAME ) . '_*';
42        $end = microtime( true ) + 60;
43        while ( true ) {
44            try {
45                $health = new \CirrusSearch\Elastica\Health( $this->getConnection()->getClient(), $indexPattern );
46                $status = $health->getStatus();
47                $this->output( "Elasticsearch status:  $status\n" );
48                if ( $status === 'green' ) {
49                    break;
50                }
51            } catch ( \Elastica\Exception\Connection\HttpException $e ) {
52                if ( $e->getError() === CURLE_COULDNT_CONNECT ) {
53                    $this->output( "Elasticsearch not up.\n" );
54                    $this->getConnection()->destroyClient();
55                } else {
56                    // The two exit code here makes puppet fail with an error.
57                    $this->fatalError( 'Connection error:  ' . $e->getMessage(), 2 );
58                }
59            }
60            if ( $end < microtime( true ) ) {
61                $this->fatalError( 'Elasticsearch was not ready in time.' );
62            }
63            sleep( 1 );
64        }
65
66        foreach ( $this->getConnection()->getAllIndexSuffixes() as $indexSuffix ) {
67            try {
68                $count = $this->getConnection()
69                    ->getIndex( $this->getSearchConfig()->get( SearchConfig::INDEX_BASE_NAME ), $indexSuffix )
70                    ->count();
71            } catch ( \Elastica\Exception\ResponseException $e ) {
72                $this->output( "$indexSuffix doesn't exist.\n" );
73                $this->error( "true" );
74                return true;
75            }
76            if ( $indexSuffix === 'content' && $count === 0 ) {
77                $this->output( "No pages in the content index.  Indexes were probably wiped.\n" );
78                return true;
79            }
80            $this->output( "Page count in $indexSuffix:  $count\n" );
81        }
82        // This result in non-zero exit code, which makes puppet decide that it needs to run whatever is gated by this.
83        return false;
84    }
85}
86
87$maintClass = CirrusNeedsToBeBuilt::class;
88require_once RUN_MAINTENANCE_IF_MAIN;