Skip to content

Commit

Permalink
ignite-5732: refactoring: protection against process execution hanging
Browse files Browse the repository at this point in the history
  • Loading branch information
daradurvs committed Aug 9, 2017
1 parent 3f5e467 commit 5587716
Showing 1 changed file with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ public static String getMavenLocalRepositoryPath() throws Exception {
* @throws Exception In case of an error.
*/
private static String defineMavenLocalRepositoryPath() throws Exception {
Process p = exec("mvn help:effective-settings");

String output = CharStreams.toString(new InputStreamReader(p.getInputStream(), Charsets.UTF_8));
String output = exec("mvn help:effective-settings");

int endTagPos = output.indexOf("</localRepository>");

Expand All @@ -164,10 +162,10 @@ private static void downloadArtifact(String artifact) throws Exception {
* Executes given command in operation system.
*
* @param cmd Command to execute.
* @return Process of executed command.
* @return Output of result of executed command.
* @throws Exception In case of an error.
*/
private static Process exec(String cmd) throws Exception {
private static String exec(String cmd) throws Exception {
ProcessBuilder pb = new ProcessBuilder();
pb.redirectErrorStream(true);

Expand All @@ -177,27 +175,34 @@ private static Process exec(String cmd) throws Exception {

final Process p = pb.start();

Future<Integer> fut = Executors.newSingleThreadExecutor().submit(new Callable<Integer>() {
@Override public Integer call() throws Exception {
return p.waitFor();
Future<String> fut = Executors.newSingleThreadExecutor().submit(new Callable<String>() {
@Override public String call() throws Exception {
String output = CharStreams.toString(new InputStreamReader(p.getInputStream(), Charsets.UTF_8));

p.waitFor();

return output;
}
});

String output = null;

try {
int exitVal = fut.get(10, TimeUnit.MINUTES);
output = fut.get(5, TimeUnit.MINUTES);

int exitVal = p.exitValue();

if (exitVal != 0)
throw new Exception(String.format("Abnormal exit value of %s for pid %s", exitVal, U.jvmPid()));

return output;
}
catch (Exception e) {
p.destroy();

X.printerrln("Command='" + cmd + "' couldn't be executed: "
+ CharStreams.toString(new InputStreamReader(p.getInputStream(), Charsets.UTF_8)), e);
X.printerrln("Command='" + cmd + "' couldn't be executed: " + output, Charsets.UTF_8, e);

throw e;
}

return p;
}
}

0 comments on commit 5587716

Please sign in to comment.