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() {
}
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 {
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;
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;
}
package org.sonar.application.process;
import org.sonar.process.ProcessCommands;
+import org.sonar.process.ProcessId;
import static java.util.Objects.requireNonNull;
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");
}
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;
@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);
}
}
@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);
}
}
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;
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();
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();
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();
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();