]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5409 - Added explicit child shutdown on JVM shutdown (child does not have to...
authorStephane Gamard <stephane.gamard@searchbox.com>
Thu, 17 Jul 2014 10:02:23 +0000 (12:02 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Fri, 18 Jul 2014 09:27:59 +0000 (11:27 +0200)
sonar-start/src/main/java/org/sonar/start/StartServer.java

index fa8ea9ff6586c1a788ad22522ee23b24b403678e..e28cb51df99d0d97f66cfc28ed8bba3788b40bc5 100644 (file)
@@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableMap;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.sonar.process.NetworkUtils;
 import org.sonar.process.ProcessWrapper;
 
 import java.io.File;
@@ -38,20 +39,17 @@ public final class StartServer {
 
   private final static Logger LOGGER = LoggerFactory.getLogger(StartServer.class);
 
-  private final static String SONAR_HOME = "SONAR_HOME";
+  public final static String SONAR_HOME = "SONAR_HOME";
 
   private final Env env;
   private final String esPort;
   private final Map<String, String> properties;
+  private final Thread shutdownHook;
 
   private ExecutorService executor;
   private ProcessWrapper elasticsearch;
   private ProcessWrapper sonarqube;
 
-  public StartServer(Env env) throws IOException {
-    this(env, new String[]{});
-  }
-
   public StartServer(Env env, String... args) throws IOException {
     this.env = env;
     this.executor = Executors.newFixedThreadPool(2);
@@ -61,50 +59,43 @@ public final class StartServer {
     if (Arrays.binarySearch(args, "--debug") > -1) {
       properties.put("esDebug", "true");
     }
-  }
-
-  public void shutdown() {
-    LOGGER.info("Shutting down sonar Node");
-  }
-
-  public void start() {
-
-    // Start ES
-    //this.startES(NetworkUtils.freePort());
 
+    shutdownHook = new Thread(new Runnable() {
+      @Override
+      public void run() {
+        StartServer.this.stop();
+      }
+    });
 
-    // Start SQ
-    this.startSQ(NetworkUtils.freePort());
+    Runtime.getRuntime().addShutdownHook(shutdownHook);
+  }
 
-//    // And monitor the activity
-//    try {
-//      monitor.join();
-//    } catch (InterruptedException e) {
-//      LOGGER.warn("Shutting down the node...");
-//    }
-    //   shutdown();
+  public void stop() {
+    LOGGER.info("Shutting down all node services");
+    Runtime.getRuntime().removeShutdownHook(shutdownHook);
+    if (elasticsearch != null) {
+      LOGGER.info("Shutting down ES service");
+      elasticsearch.stop();
+    }
+    if (sonarqube != null) {
+      LOGGER.info("Shutting down SQ service");
+      sonarqube.stop();
+    }
   }
 
+  public void start() {
 
-  private void startSQ(int port) {
-    sonarqube = new ProcessWrapper(
-      "org.sonar.application.StartServer",
-      ImmutableMap.of(
-        "SONAR_HOME", env.rootDir().getAbsolutePath(),
-        "test", "test"),
-      "SQ", port,
-      env.rootDir().getAbsolutePath() + "/lib/server/sonar-application-4.5-SNAPSHOT.jar");
-  }
+    String workingDirectory = env.rootDir().getAbsolutePath();
 
-  private void startES(int port) {
+    // Start ES
     elasticsearch = new ProcessWrapper(
+      env.rootDir().getAbsolutePath(),
       "org.sonar.search.ElasticSearch",
       ImmutableMap.of(
-        "SONAR_HOME", env.rootDir().getAbsolutePath(),
         "esDebug", properties.containsKey("esDebug") ? properties.get("esDebug") : "false",
         "esPort", esPort,
         "esHome", env.rootDir().getAbsolutePath()),
-      "ES", port,
+      "ES",
       env.rootDir().getAbsolutePath() + "/lib/search/sonar-search-4.5-SNAPSHOT.jar");
 
     while (!elasticsearch.isReady()) {
@@ -115,8 +106,28 @@ public final class StartServer {
         e.printStackTrace();
       }
     }
+
+    // Start SQ
+//    sonarqube = new ProcessWrapper(
+//      workingDirectory,
+//      "org.sonar.application.SonarServer",
+//      ImmutableMap.of(
+//        "SONAR_HOME", workingDirectory,
+//        "esPort", esPort,
+//        "test", "test"),
+//      "SQ",
+//      env.rootDir().getAbsolutePath() + "/lib/server/sonar-application-4.5-SNAPSHOT.jar");
+
+//    // And monitor the activity
+//    try {
+//      monitor.join();
+//    } catch (InterruptedException e) {
+//      LOGGER.warn("Shutting down the node...");
+//    }
+    //   shutdown();
   }
 
+
   public static void main(String... args) throws InterruptedException, IOException, URISyntaxException {
 
     String home = System.getenv(SONAR_HOME);