|
| 1 | +/* |
| 2 | + * Copyright 2016 The Closure Rules Authors. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.bazel.rules.closure; |
| 18 | + |
| 19 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 20 | + |
| 21 | +import com.google.common.base.CharMatcher; |
| 22 | +import com.google.common.base.Joiner; |
| 23 | +import com.google.common.base.Throwables; |
| 24 | +import com.google.common.collect.Iterables; |
| 25 | +import com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest; |
| 26 | +import com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse; |
| 27 | +import io.bazel.rules.closure.program.CommandLineProgram; |
| 28 | +import java.io.ByteArrayInputStream; |
| 29 | +import java.io.ByteArrayOutputStream; |
| 30 | +import java.io.IOException; |
| 31 | +import java.io.InputStream; |
| 32 | +import java.io.InterruptedIOException; |
| 33 | +import java.io.PrintStream; |
| 34 | +import java.lang.annotation.Documented; |
| 35 | +import java.lang.annotation.Retention; |
| 36 | +import java.lang.annotation.RetentionPolicy; |
| 37 | +import java.nio.file.Files; |
| 38 | +import java.nio.file.Path; |
| 39 | +import java.nio.file.Paths; |
| 40 | +import javax.inject.Inject; |
| 41 | +import javax.inject.Qualifier; |
| 42 | + |
| 43 | +/** |
| 44 | + * Bazel worker runner. |
| 45 | + * |
| 46 | + * <p>This class adapts a traditional command line program so it can be spawned by Bazel as a |
| 47 | + * persistent worker process that handles multiple invocations per JVM. It will also be backwards |
| 48 | + * compatible with being run as a normal single-invocation command. |
| 49 | + * |
| 50 | + * @param <T> delegate program type |
| 51 | + */ |
| 52 | +public final class BazelWorker<T extends CommandLineProgram> implements CommandLineProgram { |
| 53 | + |
| 54 | + /** Qualifier for name of Bazel persistent worker. */ |
| 55 | + @Qualifier |
| 56 | + @Retention(RetentionPolicy.RUNTIME) |
| 57 | + @Documented |
| 58 | + public @interface Mnemonic {} |
| 59 | + |
| 60 | + private final CommandLineProgram delegate; |
| 61 | + private final String mnemonic; |
| 62 | + private final PrintStream output; |
| 63 | + |
| 64 | + @Inject |
| 65 | + public BazelWorker(T delegate, PrintStream output, @Mnemonic String mnemonic) { |
| 66 | + this.delegate = delegate; |
| 67 | + this.output = output; |
| 68 | + this.mnemonic = mnemonic; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Integer apply(Iterable<String> args) { |
| 73 | + if (Iterables.contains(args, "--persistent_worker")) { |
| 74 | + return runAsPersistentWorker(); |
| 75 | + } else { |
| 76 | + return delegate.apply(loadArguments(args, false)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private int runAsPersistentWorker() { |
| 81 | + InputStream realStdIn = System.in; |
| 82 | + PrintStream realStdOut = System.out; |
| 83 | + PrintStream realStdErr = System.err; |
| 84 | + try (InputStream emptyIn = new ByteArrayInputStream(new byte[0]); |
| 85 | + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 86 | + PrintStream ps = new PrintStream(buffer)) { |
| 87 | + System.setIn(emptyIn); |
| 88 | + System.setOut(ps); |
| 89 | + System.setErr(ps); |
| 90 | + while (true) { |
| 91 | + WorkRequest request = WorkRequest.parseDelimitedFrom(realStdIn); |
| 92 | + if (request == null) { |
| 93 | + return 0; |
| 94 | + } |
| 95 | + int exitCode = 0; |
| 96 | + Iterable<String> args = loadArguments(request.getArgumentsList(), true); |
| 97 | + try { |
| 98 | + exitCode = delegate.apply(args); |
| 99 | + } catch (RuntimeException e) { |
| 100 | + if (wasInterrupted(e)) { |
| 101 | + return 0; |
| 102 | + } |
| 103 | + System.err.println( |
| 104 | + String.format("ERROR: Worker threw uncaught exception with args: %s", |
| 105 | + Joiner.on(' ').join(args))); |
| 106 | + e.printStackTrace(System.err); |
| 107 | + exitCode = 1; |
| 108 | + } |
| 109 | + WorkResponse.newBuilder() |
| 110 | + .setOutput(buffer.toString()) |
| 111 | + .setExitCode(exitCode) |
| 112 | + .build() |
| 113 | + .writeDelimitedTo(realStdOut); |
| 114 | + realStdOut.flush(); |
| 115 | + buffer.reset(); |
| 116 | + System.gc(); // be a good little worker process and consume less memory when idle |
| 117 | + } |
| 118 | + } catch (IOException | RuntimeException e) { |
| 119 | + if (wasInterrupted(e)) { |
| 120 | + return 0; |
| 121 | + } |
| 122 | + Throwables.throwIfUnchecked(e); |
| 123 | + throw new RuntimeException(e); |
| 124 | + } finally { |
| 125 | + System.setIn(realStdIn); |
| 126 | + System.setOut(realStdOut); |
| 127 | + System.setErr(realStdErr); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private Iterable<String> loadArguments(Iterable<String> args, boolean isWorker) { |
| 132 | + String lastArg = Iterables.getLast(args, ""); |
| 133 | + if (lastArg.startsWith("@")) { |
| 134 | + Path flagFile = Paths.get(CharMatcher.is('@').trimLeadingFrom(lastArg)); |
| 135 | + if ((isWorker && lastArg.startsWith("@@")) || Files.exists(flagFile)) { |
| 136 | + if (!isWorker && !mnemonic.isEmpty()) { |
| 137 | + output.printf( |
| 138 | + "HINT: %s will compile faster if you run: " |
| 139 | + + "echo \"build --strategy=%s=worker\" >>~/.bazelrc\n", |
| 140 | + mnemonic, mnemonic); |
| 141 | + } |
| 142 | + try { |
| 143 | + return Files.readAllLines(flagFile, UTF_8); |
| 144 | + } catch (IOException e) { |
| 145 | + throw new RuntimeException(e); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + return args; |
| 150 | + } |
| 151 | + |
| 152 | + private boolean wasInterrupted(Throwable e) { |
| 153 | + Throwable cause = Throwables.getRootCause(e); |
| 154 | + if (cause instanceof InterruptedException |
| 155 | + || cause instanceof InterruptedIOException) { |
| 156 | + output.println("Terminating worker due to interrupt signal"); |
| 157 | + return true; |
| 158 | + } |
| 159 | + return false; |
| 160 | + } |
| 161 | +} |
0 commit comments