Skip to content

Commit

Permalink
Add builder for WorkRequestHandler.
Browse files Browse the repository at this point in the history
Preparation for adding another parameter for cancellation.

RELNOTES: None.
PiperOrigin-RevId: 371139211
  • Loading branch information
larsrc-google authored and copybara-github committed Apr 29, 2021
1 parent 21d1bee commit 5103662
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.devtools.build.buildjar.javac.plugins.errorprone.ErrorPronePlugin;
import com.google.devtools.build.lib.worker.ProtoWorkerMessageProcessor;
import com.google.devtools.build.lib.worker.WorkRequestHandler;
import com.google.devtools.build.lib.worker.WorkRequestHandler.WorkRequestHandlerBuilder;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
Expand All @@ -44,11 +45,12 @@ public static void main(String[] args) {
BazelJavaBuilder builder = new BazelJavaBuilder();
if (args.length == 1 && args[0].equals("--persistent_worker")) {
WorkRequestHandler workerHandler =
new WorkRequestHandler(
builder::parseAndBuild,
System.err,
new ProtoWorkerMessageProcessor(System.in, System.out),
Duration.ofSeconds(10));
new WorkRequestHandlerBuilder(
builder::parseAndBuild,
System.err,
new ProtoWorkerMessageProcessor(System.in, System.out))
.setCpuUsageBeforeGc(Duration.ofSeconds(10))
.build();
try {
workerHandler.processRequests();
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ public WorkRequestHandler(
* @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server and
* writing {@code WorkResponses} to the server.
* @param cpuUsageBeforeGc The minimum amount of CPU time between explicit garbage collection
* calls.
* calls. Pass Duration.ZERO to not do explicit garbage collection.
* @deprecated Use WorkRequestHandlerBuilder instead.
*/
@Deprecated()
public WorkRequestHandler(
BiFunction<List<String>, PrintWriter, Integer> callback,
PrintStream stderr,
Expand All @@ -104,6 +106,48 @@ public WorkRequestHandler(
this.gcScheduler = new CpuTimeBasedGcScheduler(cpuUsageBeforeGc);
}

/** Builder class for WorkRequestHandler. Required parameters are passed to the constructor. */
public static class WorkRequestHandlerBuilder {
private final BiFunction<List<String>, PrintWriter, Integer> callback;
private final PrintStream stderr;
private final WorkerMessageProcessor messageProcessor;
private Duration cpuUsageBeforeGc = Duration.ZERO;

/**
* Creates a {@code WorkRequestHandlerBuilder}.
*
* @param callback Callback method for executing a single WorkRequest in a thread. The first
* argument to {@code callback} is the set of command-line arguments, the second is where
* all error messages and other user-oriented messages should be written to. The callback
* must return an exit code indicating success (zero) or failure (nonzero).
* @param stderr Stream that log messages should be written to, typically the process' stderr.
* @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server
* and writing {@code WorkResponses} to the server.
*/
public WorkRequestHandlerBuilder(
BiFunction<List<String>, PrintWriter, Integer> callback,
PrintStream stderr,
WorkerMessageProcessor messageProcessor) {
this.callback = callback;
this.stderr = stderr;
this.messageProcessor = messageProcessor;
}

/**
* Sets the minimum amount of CPU time between explicit garbage collection calls. Pass
* Duration.ZERO to not do explicit garbage collection (the default).
*/
public WorkRequestHandlerBuilder setCpuUsageBeforeGc(Duration cpuUsageBeforeGc) {
this.cpuUsageBeforeGc = cpuUsageBeforeGc;
return this;
}

/** Returns a WorkRequestHandler instance with the values in this Builder. */
public WorkRequestHandler build() {
return new WorkRequestHandler(callback, stderr, messageProcessor, cpuUsageBeforeGc);
}
}

/**
* Runs an infinite loop of reading {@link WorkRequest} from {@code in}, running the callback,
* then writing the corresponding {@link WorkResponse} to {@code out}. If there is an error
Expand Down

0 comments on commit 5103662

Please sign in to comment.