Search in sources :

Example 1 with Input

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();
        }
    }
}
Also used : PrintStream(java.io.PrintStream) WorkRequest(com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest) Input(com.google.devtools.build.lib.worker.WorkerProtocol.Input) Random(java.util.Random) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

Input (com.google.devtools.build.lib.worker.WorkerProtocol.Input)1 WorkRequest (com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 Random (java.util.Random)1