]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5408 - Added workingDirectory to ProcessWrapper
authorStephane Gamard <stephane.gamard@searchbox.com>
Thu, 17 Jul 2014 09:43:47 +0000 (11:43 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Fri, 18 Jul 2014 09:27:59 +0000 (11:27 +0200)
server/sonar-process/src/main/java/org/sonar/process/Process.java
server/sonar-process/src/main/java/org/sonar/process/ProcessWrapper.java
server/sonar-search/src/main/java/org/sonar/search/ElasticSearch.java

index cf0ef61e557f7bb12b24f6d4f02f0bf6933dda48..8aef7ff81a19afa27b6c1003d53d6c173f9ed1db 100644 (file)
@@ -89,6 +89,13 @@ public abstract class Process implements ProcessMXBean {
     } catch (NotCompliantMBeanException e) {
       throw new IllegalStateException("Process is not a compliant MBean", e);
     }
+
+    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable(){
+      @Override
+      public void run() {
+        Process.this.stop();
+      }
+    }));
   }
 
   public ObjectName getObjectName() {
index 379a5ceb2fc3ce81de2f01c834d52a571e7dbe3b..3a9bb4f33ac71eef7bc6356bea4d663f82d718c9 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.process;
 
 import com.google.common.io.Closeables;
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -30,6 +31,7 @@ import javax.management.remote.JMXConnector;
 import javax.management.remote.JMXConnectorFactory;
 import javax.management.remote.JMXServiceURL;
 import java.io.BufferedReader;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -48,6 +50,7 @@ public class ProcessWrapper {
 
   final int port;
   final String name;
+  final String workDir;
   final String className;
   final String[] classPath;
   final Map<String, String> properties;
@@ -70,10 +73,11 @@ public class ProcessWrapper {
 
 
 
-  public ProcessWrapper(String className, Map<String, String> properties, final String name, Integer port, String... classPath) {
-    LOGGER.info("Creating Process for '{}' with monitoring port: {}", name, port);
+  public ProcessWrapper(String workDir, String className, Map<String, String> properties, final String name, String... classPath) {
+    this.port = NetworkUtils.freePort();
+    LOGGER.info("Creating Process for '{}' with workDir: '{}' and monitoring port: {}", name, workDir, port);
+    this.workDir = workDir;
     this.name = name;
-    this.port = port;
     this.className = className;
     this.classPath = classPath;
     this.properties = properties;
@@ -149,6 +153,14 @@ public class ProcessWrapper {
     processBuilder.environment().put(Process.NAME_PROPERTY, this.getName());
     processBuilder.environment().put(Process.PORT_PROPERTY, Integer.toString(port));
 
+    //check that working directory exists.
+    File workDirectory = new File(workDir);
+    if(!workDirectory.exists()) {
+      throw new IllegalStateException("Work directory does not exist.");
+    } else {
+      processBuilder.directory(FileUtils.getFile(workDir));
+    }
+
     try {
       java.lang.Process process = processBuilder.start();
       errorGobbler = new StreamGobbler(process.getErrorStream(), this.getName() + "-ERROR");
index e07850b285d5f86f8b25fbe68d42c25427abc709..7f72ad39969a3aec652606fa00bab138a9c24307 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.search;
 
 import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
 import org.elasticsearch.common.settings.ImmutableSettings;
+import org.elasticsearch.common.unit.TimeValue;
 import org.elasticsearch.node.Node;
 import org.elasticsearch.node.NodeBuilder;
 import org.slf4j.Logger;
@@ -104,9 +105,14 @@ public class ElasticSearch extends Process {
   @Override
   public boolean isReady() {
     try {
-      ClusterHealthStatus status = node.client().admin().cluster().prepareClusterStats()
-        .get().getStatus();
-      return status != null && status == ClusterHealthStatus.GREEN;
+      return (node.client().admin().cluster().prepareHealth()
+        .setWaitForYellowStatus()
+        .setTimeout(TimeValue.timeValueSeconds(3L))
+        .get()
+        .getStatus() != ClusterHealthStatus.RED);
+//      ClusterHealthStatus status = node.client().admin().cluster().prepareClusterStats()
+//        .get().getStatus();
+//      return status != null && status == ClusterHealthStatus.GREEN;
     } catch (Exception e) {
       return false;
     }