use of com.google.devtools.build.lib.worker.WorkerProtocol.Input in project bazel by bazelbuild.
the class ExampleWorker method runPersistentWorker.
private static void runPersistentWorker(ExampleWorkerOptions workerOptions) throws IOException {
PrintStream originalStdOut = System.out;
PrintStream originalStdErr = System.err;
while (true) {
try {
WorkRequest request = WorkRequest.parseDelimitedFrom(System.in);
if (request == null) {
break;
}
inputs.clear();
for (Input input : request.getInputsList()) {
inputs.put(input.getPath(), input.getDigest().toStringUtf8());
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int exitCode = 0;
try (PrintStream ps = new PrintStream(baos)) {
System.setOut(ps);
System.setErr(ps);
if (poisoned) {
System.out.println("I'm a poisoned worker and this is not a protobuf.");
System.out.println("Here's a fake stack trace for you:");
System.out.println(" at com.example.Something(Something.java:83)");
System.out.println(" at java.lang.Thread.run(Thread.java:745)");
System.out.print("And now, 8k of random bytes: ");
byte[] b = new byte[8192];
new Random().nextBytes(b);
System.out.write(b);
} else {
try {
processRequest(request.getArgumentsList());
} catch (Exception e) {
e.printStackTrace();
exitCode = 1;
}
}
} finally {
System.setOut(originalStdOut);
System.setErr(originalStdErr);
}
if (poisoned) {
baos.writeTo(System.out);
} else {
WorkResponse.newBuilder().setOutput(baos.toString()).setExitCode(exitCode).build().writeDelimitedTo(System.out);
}
System.out.flush();
if (workerOptions.exitAfter > 0 && workUnitCounter > workerOptions.exitAfter) {
return;
}
if (workerOptions.poisonAfter > 0 && workUnitCounter > workerOptions.poisonAfter) {
poisoned = true;
}
} finally {
// Be a good worker process and consume less memory when idle.
System.gc();
}
}
}
Aggregations