mirror of https://github.com/apache/cloudstack.git
Improve logs on ProcessRunner (#4703)
Co-authored-by: Daniel Augusto Veronezi Salvador <daniel@scclouds.com.br>
This commit is contained in:
parent
a0788f5816
commit
a5a368e353
|
|
@ -184,6 +184,10 @@
|
|||
<artifactId>commons-compress</artifactId>
|
||||
<version>${cs.commons-compress.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ import java.util.concurrent.ExecutorService;
|
|||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public final class ProcessRunner {
|
||||
public static final Logger LOG = Logger.getLogger(ProcessRunner.class);
|
||||
|
|
@ -73,8 +74,13 @@ public final class ProcessRunner {
|
|||
String stdOutput = null;
|
||||
String stdError = null;
|
||||
|
||||
String oneLineCommand = StringUtils.join(commands, " ");
|
||||
|
||||
try {
|
||||
LOG.debug(String.format("Preparing command [%s] to execute.", oneLineCommand));
|
||||
final Process process = new ProcessBuilder().command(commands).start();
|
||||
|
||||
LOG.debug(String.format("Submitting command [%s].", oneLineCommand));
|
||||
final Future<Integer> processFuture = executor.submit(new Callable<Integer>() {
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
|
|
@ -82,19 +88,16 @@ public final class ProcessRunner {
|
|||
}
|
||||
});
|
||||
try {
|
||||
LOG.debug(String.format("Waiting for a response from command [%s]. Defined timeout: [%s].", oneLineCommand, timeOut.getStandardSeconds()));
|
||||
retVal = processFuture.get(timeOut.getStandardSeconds(), TimeUnit.SECONDS);
|
||||
} catch (ExecutionException e) {
|
||||
LOG.warn(String.format("Failed to complete the requested command [%s] due to execution error.", oneLineCommand), e);
|
||||
retVal = -2;
|
||||
stdError = e.getMessage();
|
||||
if (LOG.isTraceEnabled()) {
|
||||
LOG.trace("Failed to complete the requested command due to execution error: " + e.getMessage());
|
||||
}
|
||||
} catch (TimeoutException e) {
|
||||
LOG.warn(String.format("Failed to complete the requested command [%s] within timeout. Defined timeout: [%s].", oneLineCommand, timeOut.getStandardSeconds()), e);
|
||||
retVal = -1;
|
||||
stdError = "Operation timed out, aborted";
|
||||
if (LOG.isTraceEnabled()) {
|
||||
LOG.trace("Failed to complete the requested command within timeout: " + e.getMessage());
|
||||
}
|
||||
stdError = "Operation timed out, aborted.";
|
||||
} finally {
|
||||
if (Strings.isNullOrEmpty(stdError)) {
|
||||
stdOutput = CharStreams.toString(new InputStreamReader(process.getInputStream()));
|
||||
|
|
@ -102,14 +105,13 @@ public final class ProcessRunner {
|
|||
}
|
||||
process.destroy();
|
||||
}
|
||||
if (LOG.isTraceEnabled()) {
|
||||
LOG.trace("Process standard output: " + stdOutput);
|
||||
LOG.trace("Process standard error output: " + stdError);
|
||||
}
|
||||
|
||||
LOG.debug(String.format("Process standard output for command [%s]: [%s].", oneLineCommand, stdOutput));
|
||||
LOG.debug(String.format("Process standard error output command [%s]: [%s].", oneLineCommand, stdError));
|
||||
} catch (IOException | InterruptedException e) {
|
||||
LOG.error(String.format("Exception caught error running command [%s].", oneLineCommand), e);
|
||||
stdError = e.getMessage();
|
||||
LOG.error("Exception caught error running commands: " + e.getMessage());
|
||||
}
|
||||
return new ProcessResult(stdOutput, stdError, retVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ public class ProcessTest {
|
|||
ProcessResult result = RUNNER.executeCommands(Arrays.asList("sleep", "5"), Duration.standardSeconds(1));
|
||||
Assert.assertNotEquals(result.getReturnCode(), 0);
|
||||
Assert.assertTrue(result.getStdError().length() > 0);
|
||||
Assert.assertEquals(result.getStdError(), "Operation timed out, aborted");
|
||||
Assert.assertEquals(result.getStdError(), "Operation timed out, aborted.");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -57,7 +57,7 @@ public class ProcessTest {
|
|||
ProcessResult result = RUNNER.executeCommands(Arrays.asList("ls", "/some/dir/that/should/not/exist"), Duration.standardSeconds(2));
|
||||
Assert.assertNotEquals(result.getReturnCode(), 0);
|
||||
Assert.assertTrue(result.getStdError().length() > 0);
|
||||
Assert.assertNotEquals(result.getStdError(), "Operation timed out, aborted");
|
||||
Assert.assertNotEquals(result.getStdError(), "Operation timed out, aborted.");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
|
|
|
|||
Loading…
Reference in New Issue