Skip to content

Commit

Permalink
Start using official Bazel worker helper.
Browse files Browse the repository at this point in the history
This helper provides additional features like GC and worker multiplexing - though they are not yet enabled in this CL.

CL is temporarily forking the files until bazel_worker_java could be added as a dep for repo:
bazelbuild/bazel-worker-api#10

PiperOrigin-RevId: 733024990
  • Loading branch information
gkdn authored and copybara-github committed Mar 3, 2025
1 parent 73c7c3a commit a8accf3
Show file tree
Hide file tree
Showing 5 changed files with 898 additions and 43 deletions.
1 change: 1 addition & 0 deletions transpiler/java/com/google/j2cl/common/bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ java_library(
deps = [
":worker_protocol_java_proto",
"//third_party:args4j",
"//third_party:error_prone_annotations",
"//third_party:guava",
"//transpiler/java/com/google/j2cl/common",
],
Expand Down
53 changes: 17 additions & 36 deletions transpiler/java/com/google/j2cl/common/bazel/BazelWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest;
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse;
import com.google.j2cl.common.Problems;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.function.Supplier;
import org.kohsuke.args4j.CmdLineException;
Expand All @@ -49,26 +46,22 @@ public abstract class BazelWorker {
* Process the request described by the arguments. Note that you must output errors and warnings
* via {@link Problems} to avoid interrupting the worker protocol which occurs over stdout.
*/
private int processRequest(List<String> args) {
private int processRequest(List<String> args, PrintWriter pw) {
CmdLineParser parser = new CmdLineParser(this);

try {
parser.parseArgument(args);
} catch (CmdLineException e) {
problems.error("%s", e.getMessage());
return problems.reportAndGetExitCode(System.err);
return problems.reportAndGetExitCode(pw);
}

try {
run();
} catch (Problems.Exit e) {
// Program aborted due to errors recorded in problems.
} catch (Throwable e) {
// Program crash.
e.printStackTrace(System.err);
return 1;
}
return problems.reportAndGetExitCode(System.err);
return problems.reportAndGetExitCode(pw);
}

public static final void start(String[] args, Supplier<BazelWorker> workerSupplier)
Expand All @@ -83,35 +76,23 @@ public static final void start(String[] args, Supplier<BazelWorker> workerSuppli
@SuppressWarnings("SystemExitOutsideMain")
private static void runStandaloneWorker(Supplier<BazelWorker> workerSupplier, List<String> args) {
// This is a single invocation of builder that exits after it processed the request.
int exitCode = workerSupplier.get().processRequest(args);
int exitCode =
workerSupplier
.get()
.processRequest(args, new PrintWriter(System.err, /* autoFlush= */ true));
System.exit(exitCode);
}

private static void runPersistentWorker(Supplier<BazelWorker> workerSupplier) throws IOException {
PrintStream realStdOut = System.out;

// Ensure we capture stdout/sterr for potential debug/error messages.
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(buffer, true);
System.setOut(ps);
System.setErr(ps);

while (true) {
WorkRequest request = WorkRequest.parseDelimitedFrom(System.in);

if (request == null) {
break;
}

int exitCode = workerSupplier.get().processRequest(request.getArgumentsList());
WorkResponse.newBuilder()
.setOutput(buffer.toString())
.setExitCode(exitCode)
.build()
.writeDelimitedTo(realStdOut);
realStdOut.flush();
buffer.reset();
}
WorkRequestHandler workerHandler =
new WorkRequestHandler.WorkRequestHandlerBuilder(
new WorkRequestHandler.WorkRequestCallback(
(request, pw) ->
workerSupplier.get().processRequest(request.getArgumentsList(), pw)),
System.err,
new ProtoWorkerMessageProcessor(System.in, System.out))
.build();
workerHandler.processRequests();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2020 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.j2cl.common.bazel;

import com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest;
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

// TODO(goktug): Remove this class after https://github.com/bazelbuild/bazel-worker-api/issues/10

/** Implementation of the Worker Protocol using Proto to communicate with Bazel. */
final class ProtoWorkerMessageProcessor implements WorkRequestHandler.WorkerMessageProcessor {

/** This worker's stdin. */
private final InputStream stdin;

/** This worker's stdout. Only {@link WorkRequest}s should be written here. */
private final OutputStream stdout;

/** Constructs a {@link WorkRequestHandler} that reads and writes Protocol Buffers. */
public ProtoWorkerMessageProcessor(InputStream stdin, OutputStream stdout) {
this.stdin = stdin;
this.stdout = stdout;
}

@Override
public WorkRequest readWorkRequest() throws IOException {
return WorkRequest.parseDelimitedFrom(stdin);
}

@Override
public void writeWorkResponse(WorkResponse workResponse) throws IOException {
try {
workResponse.writeDelimitedTo(stdout);
} finally {
stdout.flush();
}
}

@Override
public void close() {}
}
Loading

0 comments on commit a8accf3

Please sign in to comment.