001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.search;
021
022import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
023import org.elasticsearch.common.unit.TimeValue;
024import org.elasticsearch.node.internal.InternalNode;
025import org.slf4j.LoggerFactory;
026import org.sonar.process.MinimumViableSystem;
027import org.sonar.process.Monitored;
028import org.sonar.process.ProcessEntryPoint;
029import org.sonar.process.Props;
030
031public class SearchServer implements Monitored {
032
033  private final SearchSettings settings;
034  private InternalNode node;
035
036  public SearchServer(Props props) {
037    this.settings = new SearchSettings(props);
038    new MinimumViableSystem().check();
039  }
040
041  @Override
042  public void start() {
043    LoggerFactory.getLogger(SearchServer.class).info("Starting Elasticsearch[{}] on port {}", settings.clusterName(), settings.tcpPort());
044
045    node = new InternalNode(settings.build(), false);
046    node.start();
047  }
048
049  @Override
050  public boolean isReady() {
051    return node != null && node.client().admin().cluster().prepareHealth()
052      .setWaitForYellowStatus()
053      .setTimeout(TimeValue.timeValueSeconds(30L))
054      .get()
055      .getStatus() != ClusterHealthStatus.RED;
056  }
057
058  @Override
059  public void awaitStop() {
060    while (node != null && !node.isClosed()) {
061      try {
062        Thread.sleep(200L);
063      } catch (InterruptedException e) {
064        // Ignore
065      }
066    }
067  }
068
069  @Override
070  public void stop() {
071    if (node != null && !node.isClosed()) {
072      node.close();
073    }
074  }
075
076  public static void main(String... args) {
077    ProcessEntryPoint entryPoint = ProcessEntryPoint.createForArguments(args);
078    new SearchLogging().configure();
079    SearchServer searchServer = new SearchServer(entryPoint.getProps());
080    entryPoint.launch(searchServer);
081  }
082}