Skip to content

Commit 5103662

Browse files
larsrc-googlecopybara-github
authored andcommitted
Add builder for WorkRequestHandler.
Preparation for adding another parameter for cancellation. RELNOTES: None. PiperOrigin-RevId: 371139211
1 parent 21d1bee commit 5103662

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

src/java_tools/buildjar/java/com/google/devtools/build/buildjar/BazelJavaBuilder.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.devtools.build.buildjar.javac.plugins.errorprone.ErrorPronePlugin;
2626
import com.google.devtools.build.lib.worker.ProtoWorkerMessageProcessor;
2727
import com.google.devtools.build.lib.worker.WorkRequestHandler;
28+
import com.google.devtools.build.lib.worker.WorkRequestHandler.WorkRequestHandlerBuilder;
2829
import java.io.IOException;
2930
import java.io.OutputStreamWriter;
3031
import java.io.PrintWriter;
@@ -44,11 +45,12 @@ public static void main(String[] args) {
4445
BazelJavaBuilder builder = new BazelJavaBuilder();
4546
if (args.length == 1 && args[0].equals("--persistent_worker")) {
4647
WorkRequestHandler workerHandler =
47-
new WorkRequestHandler(
48-
builder::parseAndBuild,
49-
System.err,
50-
new ProtoWorkerMessageProcessor(System.in, System.out),
51-
Duration.ofSeconds(10));
48+
new WorkRequestHandlerBuilder(
49+
builder::parseAndBuild,
50+
System.err,
51+
new ProtoWorkerMessageProcessor(System.in, System.out))
52+
.setCpuUsageBeforeGc(Duration.ofSeconds(10))
53+
.build();
5254
try {
5355
workerHandler.processRequests();
5456
} catch (IOException e) {

src/main/java/com/google/devtools/build/lib/worker/WorkRequestHandler.java

+45-1
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ public WorkRequestHandler(
9191
* @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server and
9292
* writing {@code WorkResponses} to the server.
9393
* @param cpuUsageBeforeGc The minimum amount of CPU time between explicit garbage collection
94-
* calls.
94+
* calls. Pass Duration.ZERO to not do explicit garbage collection.
95+
* @deprecated Use WorkRequestHandlerBuilder instead.
9596
*/
97+
@Deprecated()
9698
public WorkRequestHandler(
9799
BiFunction<List<String>, PrintWriter, Integer> callback,
98100
PrintStream stderr,
@@ -104,6 +106,48 @@ public WorkRequestHandler(
104106
this.gcScheduler = new CpuTimeBasedGcScheduler(cpuUsageBeforeGc);
105107
}
106108

109+
/** Builder class for WorkRequestHandler. Required parameters are passed to the constructor. */
110+
public static class WorkRequestHandlerBuilder {
111+
private final BiFunction<List<String>, PrintWriter, Integer> callback;
112+
private final PrintStream stderr;
113+
private final WorkerMessageProcessor messageProcessor;
114+
private Duration cpuUsageBeforeGc = Duration.ZERO;
115+
116+
/**
117+
* Creates a {@code WorkRequestHandlerBuilder}.
118+
*
119+
* @param callback Callback method for executing a single WorkRequest in a thread. The first
120+
* argument to {@code callback} is the set of command-line arguments, the second is where
121+
* all error messages and other user-oriented messages should be written to. The callback
122+
* must return an exit code indicating success (zero) or failure (nonzero).
123+
* @param stderr Stream that log messages should be written to, typically the process' stderr.
124+
* @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server
125+
* and writing {@code WorkResponses} to the server.
126+
*/
127+
public WorkRequestHandlerBuilder(
128+
BiFunction<List<String>, PrintWriter, Integer> callback,
129+
PrintStream stderr,
130+
WorkerMessageProcessor messageProcessor) {
131+
this.callback = callback;
132+
this.stderr = stderr;
133+
this.messageProcessor = messageProcessor;
134+
}
135+
136+
/**
137+
* Sets the minimum amount of CPU time between explicit garbage collection calls. Pass
138+
* Duration.ZERO to not do explicit garbage collection (the default).
139+
*/
140+
public WorkRequestHandlerBuilder setCpuUsageBeforeGc(Duration cpuUsageBeforeGc) {
141+
this.cpuUsageBeforeGc = cpuUsageBeforeGc;
142+
return this;
143+
}
144+
145+
/** Returns a WorkRequestHandler instance with the values in this Builder. */
146+
public WorkRequestHandler build() {
147+
return new WorkRequestHandler(callback, stderr, messageProcessor, cpuUsageBeforeGc);
148+
}
149+
}
150+
107151
/**
108152
* Runs an infinite loop of reading {@link WorkRequest} from {@code in}, running the callback,
109153
* then writing the corresponding {@link WorkResponse} to {@code out}. If there is an error

0 commit comments

Comments
 (0)