]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8798 log exit value of terminated sub processes
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>
Tue, 8 Aug 2017 11:57:09 +0000 (13:57 +0200)
committerDaniel Schwarz <bartfastiel@users.noreply.github.com>
Wed, 9 Aug 2017 13:09:54 +0000 (15:09 +0200)
server/sonar-process-monitor/src/main/java/org/sonar/application/process/AbstractProcessMonitor.java
server/sonar-process-monitor/src/main/java/org/sonar/application/process/EsProcessMonitor.java
server/sonar-process-monitor/src/main/java/org/sonar/application/process/ProcessCommandsProcessMonitor.java
server/sonar-process-monitor/src/main/java/org/sonar/application/process/ProcessLauncherImpl.java
server/sonar-process-monitor/src/test/java/org/sonar/application/process/ProcessCommandsProcessMonitorTest.java

index 596db4939c56ae3464a9b3d61b18814cece192c3..d5d761234da1dd05f0842f446bafb75f882da60b 100644 (file)
@@ -24,12 +24,21 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.concurrent.TimeUnit;
 import javax.annotation.Nullable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.process.ProcessId;
 
 abstract class AbstractProcessMonitor implements ProcessMonitor {
+
+  private static final Logger LOG = LoggerFactory.getLogger(EsProcessMonitor.class);
+  private static final int EXPECTED_EXIT_VALUE = 0;
+
   protected final Process process;
+  private final ProcessId processId;
 
-  protected AbstractProcessMonitor(Process process) {
+  protected AbstractProcessMonitor(Process process, ProcessId processId) {
     this.process = process;
+    this.processId = processId;
   }
 
   public InputStream getInputStream() {
@@ -61,7 +70,12 @@ abstract class AbstractProcessMonitor implements ProcessMonitor {
   }
 
   public void waitFor() throws InterruptedException {
-    process.waitFor();
+    int exitValue = process.waitFor();
+    if (exitValue != EXPECTED_EXIT_VALUE) {
+      LOG.warn("Process exited with exit value [{}]: {}", processId.getKey(), exitValue);
+    } else {
+      LOG.debug("Process exited with exit value [{}]: {}", processId.getKey(), exitValue);
+    }
   }
 
   public void waitFor(long timeout, TimeUnit unit) throws InterruptedException {
index 14ddd80220af53f2a76c38b3bf3f9279fd341287..e7502535d54808e29f23cb1c0358484882f11a91 100644 (file)
@@ -41,6 +41,7 @@ import org.elasticsearch.common.transport.TransportAddress;
 import org.elasticsearch.transport.Netty4Plugin;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.sonar.process.ProcessId;
 
 import static java.util.Collections.singletonList;
 import static java.util.Collections.unmodifiableList;
@@ -61,8 +62,8 @@ public class EsProcessMonitor extends AbstractProcessMonitor {
   private final EsCommand esCommand;
   private AtomicReference<TransportClient> transportClient = new AtomicReference<>(null);
 
-  public EsProcessMonitor(Process process, EsCommand esCommand) throws MalformedURLException {
-    super(process);
+  public EsProcessMonitor(Process process, ProcessId processId, EsCommand esCommand) throws MalformedURLException {
+    super(process, processId);
     this.esCommand = esCommand;
   }
 
index 734ce60c6817477497ef221add02330c4b72b1b7..77ebdeeb521711966a6e257607c1b67f262d2851 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.application.process;
 
 import org.sonar.process.ProcessCommands;
+import org.sonar.process.ProcessId;
 
 import static java.util.Objects.requireNonNull;
 
@@ -27,8 +28,8 @@ class ProcessCommandsProcessMonitor extends AbstractProcessMonitor {
 
   private final ProcessCommands commands;
 
-  ProcessCommandsProcessMonitor(Process process, ProcessCommands commands) {
-    super(process);
+  ProcessCommandsProcessMonitor(Process process, ProcessId processId, ProcessCommands commands) {
+    super(process, processId);
     this.commands = requireNonNull(commands, "commands can't be null");
   }
 
index b849c5f52c939788b683546be45782baf8aeb20f..3ac0fd1dcd0ac75c88d8350127ca58579aec921c 100644 (file)
@@ -39,6 +39,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.process.AllProcessesCommands;
 import org.sonar.process.ProcessCommands;
+import org.sonar.process.ProcessId;
 
 import static java.lang.String.format;
 import static org.sonar.process.ProcessEntryPoint.PROPERTY_PROCESS_INDEX;
@@ -76,20 +77,21 @@ public class ProcessLauncherImpl implements ProcessLauncher {
   @Override
   public ProcessMonitor launch(EsCommand esCommand) {
     Process process = null;
+    ProcessId processId = esCommand.getProcessId();
     try {
       writeConfFiles(esCommand);
       ProcessBuilder processBuilder = create(esCommand);
-      LOG.info("Launch process[{}]: {}", esCommand.getProcessId().getKey(), String.join(" ", processBuilder.command()));
+      LOG.info("Launch process[{}]: {}", processId.getKey(), String.join(" ", processBuilder.command()));
 
       process = processBuilder.start();
 
-      return new EsProcessMonitor(process, esCommand);
+      return new EsProcessMonitor(process, processId, esCommand);
     } catch (Exception e) {
       // just in case
       if (process != null) {
         process.destroyForcibly();
       }
-      throw new IllegalStateException(format("Fail to launch process [%s]", esCommand.getProcessId().getKey()), e);
+      throw new IllegalStateException(format("Fail to launch process [%s]", processId.getKey()), e);
     }
   }
 
@@ -114,19 +116,20 @@ public class ProcessLauncherImpl implements ProcessLauncher {
   @Override
   public ProcessMonitor launch(JavaCommand javaCommand) {
     Process process = null;
+    ProcessId processId = javaCommand.getProcessId();
     try {
-      ProcessCommands commands = allProcessesCommands.createAfterClean(javaCommand.getProcessId().getIpcIndex());
+      ProcessCommands commands = allProcessesCommands.createAfterClean(processId.getIpcIndex());
 
       ProcessBuilder processBuilder = create(javaCommand);
-      LOG.info("Launch process[{}]: {}", javaCommand.getProcessId().getKey(), String.join(" ", processBuilder.command()));
+      LOG.info("Launch process[{}]: {}", processId.getKey(), String.join(" ", processBuilder.command()));
       process = processBuilder.start();
-      return new ProcessCommandsProcessMonitor(process, commands);
+      return new ProcessCommandsProcessMonitor(process, processId, commands);
     } catch (Exception e) {
       // just in case
       if (process != null) {
         process.destroyForcibly();
       }
-      throw new IllegalStateException(format("Fail to launch process [%s]", javaCommand.getProcessId().getKey()), e);
+      throw new IllegalStateException(format("Fail to launch process [%s]", processId.getKey()), e);
     }
   }
 
index e8e5c63e47a58f0d275a92780c67c37dadc8c463..c7a779bcb610d675c62390a3721f709a86246095 100644 (file)
@@ -25,6 +25,7 @@ import java.util.concurrent.TimeUnit;
 import org.junit.Test;
 import org.mockito.Mockito;
 import org.sonar.process.ProcessCommands;
+import org.sonar.process.ProcessId;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
@@ -41,7 +42,7 @@ public class ProcessCommandsProcessMonitorTest {
     Process process = mock(Process.class, RETURNS_DEEP_STUBS);
     ProcessCommands commands = mock(ProcessCommands.class, RETURNS_DEEP_STUBS);
 
-    ProcessCommandsProcessMonitor underTest = new ProcessCommandsProcessMonitor(process, commands);
+    ProcessCommandsProcessMonitor underTest = new ProcessCommandsProcessMonitor(process, ProcessId.WEB_SERVER, commands);
 
     underTest.waitFor();
     verify(process).waitFor();
@@ -68,7 +69,7 @@ public class ProcessCommandsProcessMonitorTest {
     Process process = mock(Process.class, RETURNS_DEEP_STUBS);
     ProcessCommands commands = mock(ProcessCommands.class, RETURNS_DEEP_STUBS);
 
-    ProcessCommandsProcessMonitor underTest = new ProcessCommandsProcessMonitor(process, commands);
+    ProcessCommandsProcessMonitor underTest = new ProcessCommandsProcessMonitor(process, null, commands);
 
     underTest.askForStop();
     verify(commands).askForStop();
@@ -89,7 +90,7 @@ public class ProcessCommandsProcessMonitorTest {
     Process process = mock(Process.class);
     when(process.getInputStream()).thenReturn(null);
 
-    ProcessCommandsProcessMonitor underTest = new ProcessCommandsProcessMonitor(process, commands);
+    ProcessCommandsProcessMonitor underTest = new ProcessCommandsProcessMonitor(process, null, commands);
 
     // no failures
     underTest.closeStreams();
@@ -102,7 +103,7 @@ public class ProcessCommandsProcessMonitorTest {
     Process process = mock(Process.class);
     when(process.getInputStream()).thenReturn(stream);
 
-    ProcessCommandsProcessMonitor underTest = new ProcessCommandsProcessMonitor(process, mock(ProcessCommands.class, Mockito.RETURNS_MOCKS));
+    ProcessCommandsProcessMonitor underTest = new ProcessCommandsProcessMonitor(process, null, mock(ProcessCommands.class, Mockito.RETURNS_MOCKS));
 
     // no failures
     underTest.closeStreams();