Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,7 @@ public void write(WorkRequest req) throws IOException {
writeTo(req, this.writeStream);
}

public WorkResponse waitAndRead() throws IOException, InterruptedException {
try {
waitForInput(processWrapper::isAlive, readStream);
} catch (IOException e) {
String stdErrMsg = processWrapper.getErrorString();
String stdOut = "";
try {
if (processWrapper.isAlive() && readStream.available() > 0) {
stdOut = IOUtils.toString(readStream, StandardCharsets.UTF_8);
} else {
stdOut = "no stream available";
}
} catch (IOException e2) {
stdOut = "Exception trying to read stdout: " + e2;
}
throw new IOException(
"IOException on waitForInput; stdErr: " + stdErrMsg + "\nStdout: " + stdOut, e);
}
public WorkResponse waitAndRead() throws IOException {
return readResponse(readStream);
}

Expand All @@ -79,24 +62,4 @@ public static WorkResponse readResponse(InputStream inputStream) throws IOExcept
public static WorkRequest readRequest(InputStream inputStream) throws IOException {
return WorkRequest.parseDelimitedFrom(inputStream);
}

public static void waitForInput(Supplier<Boolean> liveCheck, InputStream inputStream)
throws IOException, InterruptedException {
String workerDeathMsg = "Worker process for died while waiting for response";
// TODO can we do better than spinning? i.e. condition variable?
while (inputAvailable(inputStream, workerDeathMsg) == 0) {
Thread.sleep(10);
if (!liveCheck.get()) {
throw new IOException(workerDeathMsg + "\n");
}
}
}

private static int inputAvailable(InputStream inputStream, String errorMsg) throws IOException {
try {
return inputStream.available();
} catch (IOException e) {
throw new IOException(errorMsg, e);
}
}
}