Skip to content

Commit

Permalink
Enable cancelation for compilation tasks.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 733933899
  • Loading branch information
gkdn authored and copybara-github committed Mar 6, 2025
1 parent 9d00133 commit 605e0ee
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
5 changes: 4 additions & 1 deletion build_defs/internal_do_not_use/j2cl_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,10 @@ def _j2cl_transpile(
executable = ctx.executable._j2cl_transpiler,
arguments = [args],
env = dict(LANG = "en_US.UTF-8"),
execution_requirements = {"supports-workers": "1"},
execution_requirements = {
"supports-workers": "1",
"supports-worker-cancellation": "1",
},
mnemonic = "J2cl" if backend == "CLOSURE" else "J2wasm",
)

Expand Down
24 changes: 22 additions & 2 deletions transpiler/java/com/google/j2cl/common/bazel/BazelWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.PrintWriter;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
Expand Down Expand Up @@ -65,6 +66,10 @@ private int processRequest(List<String> args, PrintWriter pw) {
return problems.reportAndGetExitCode(pw);
}

private void cancel() {
problems.requestCancellation();
}

public static final void start(String[] args, Supplier<BazelWorker> workerSupplier)
throws Exception {
if (args.length == 1 && args[0].equals("--persistent_worker")) {
Expand All @@ -85,13 +90,28 @@ private static void runStandaloneWorker(Supplier<BazelWorker> workerSupplier, Li
}

private static void runPersistentWorker(Supplier<BazelWorker> workerSupplier) throws IOException {
var activeWorkers = new ConcurrentHashMap<Integer, BazelWorker>();
WorkRequestHandler workerHandler =
new WorkRequestHandler.WorkRequestHandlerBuilder(
new WorkRequestHandler.WorkRequestCallback(
(request, pw) ->
workerSupplier.get().processRequest(request.getArgumentsList(), pw)),
(request, pw) -> {
BazelWorker worker = workerSupplier.get();
activeWorkers.put(request.getRequestId(), worker);
try {
return worker.processRequest(request.getArgumentsList(), pw);
} finally {
activeWorkers.remove(request.getRequestId());
}
}),
System.err,
new ProtoWorkerMessageProcessor(System.in, System.out))
.setCancelCallback(
(requestId, thread) -> {
BazelWorker worker = activeWorkers.get(requestId);
if (worker != null) {
worker.cancel();
}
})
.setIdleTimeBeforeGc(Duration.ofSeconds(4))
.build();
workerHandler.processRequests();
Expand Down

0 comments on commit 605e0ee

Please sign in to comment.