From 2ac26bfb14fa7e60f5c0fce212f092275630734b Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Thu, 17 Jul 2014 11:43:47 +0200 Subject: [PATCH] SONAR-5408 - Added workingDirectory to ProcessWrapper --- .../main/java/org/sonar/process/Process.java | 7 +++++++ .../java/org/sonar/process/ProcessWrapper.java | 18 +++++++++++++++--- .../java/org/sonar/search/ElasticSearch.java | 12 +++++++++--- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/server/sonar-process/src/main/java/org/sonar/process/Process.java b/server/sonar-process/src/main/java/org/sonar/process/Process.java index cf0ef61e557..8aef7ff81a1 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/Process.java +++ b/server/sonar-process/src/main/java/org/sonar/process/Process.java @@ -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() { diff --git a/server/sonar-process/src/main/java/org/sonar/process/ProcessWrapper.java b/server/sonar-process/src/main/java/org/sonar/process/ProcessWrapper.java index 379a5ceb2fc..3a9bb4f33ac 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/ProcessWrapper.java +++ b/server/sonar-process/src/main/java/org/sonar/process/ProcessWrapper.java @@ -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 properties; @@ -70,10 +73,11 @@ public class ProcessWrapper { - public ProcessWrapper(String className, Map 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 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"); diff --git a/server/sonar-search/src/main/java/org/sonar/search/ElasticSearch.java b/server/sonar-search/src/main/java/org/sonar/search/ElasticSearch.java index e07850b285d..7f72ad39969 100644 --- a/server/sonar-search/src/main/java/org/sonar/search/ElasticSearch.java +++ b/server/sonar-search/src/main/java/org/sonar/search/ElasticSearch.java @@ -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; } -- 2.39.5