use of com.google.devtools.build.android.dexer.Dexing.DexingKey in project bazel by bazelbuild.
the class DexBuilder method runPersistentWorker.
/**
* Implements a persistent worker process for use with Bazel (see {@code WorkerSpawnStrategy}).
*/
private static void runPersistentWorker() throws IOException {
ExecutorService executor = newFixedThreadPool(Runtime.getRuntime().availableProcessors());
Cache<DexingKey, byte[]> dexCache = CacheBuilder.newBuilder().maximumWeight(Math.min(Runtime.getRuntime().maxMemory() - 25 * ONE_MEG, 200 * ONE_MEG)).weigher(new Weigher<DexingKey, byte[]>() {
@Override
public int weigh(DexingKey key, byte[] value) {
return key.classfileContent().length + value.length;
}
}).build();
try {
while (true) {
WorkRequest request = WorkRequest.parseDelimitedFrom(System.in);
if (request == null) {
return;
}
// Redirect dx's output so we can return it in response
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, /*autoFlush*/
true);
DxConsole.out = DxConsole.err = ps;
// Make sure that we exit nonzero in case uncaught errors occur during processRequest.
int exitCode = 1;
try {
processRequest(executor, dexCache, request.getArgumentsList());
// success!
exitCode = 0;
} catch (Exception e) {
// Deliberate catch-all so we can capture a stack trace.
// TODO(bazel-team): Consider canceling any outstanding futures created for this request
e.printStackTrace(ps);
} catch (Error e) {
e.printStackTrace();
// try capturing the error, may fail if out of memory
e.printStackTrace(ps);
// rethrow to kill the worker
throw e;
} finally {
// Try sending a response no matter what
String output;
try {
output = baos.toString();
} catch (Throwable t) {
// most likely out of memory, so log with minimal memory needs
t.printStackTrace();
output = "check worker log for exceptions";
}
WorkResponse.newBuilder().setOutput(output).setExitCode(exitCode).build().writeDelimitedTo(System.out);
System.out.flush();
}
}
} finally {
executor.shutdown();
}
}
Aggregations