// Copyright 2015 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.devtools.build.lib.worker; import com.google.common.hash.HashCode; import com.google.devtools.build.lib.shell.Subprocess; import com.google.devtools.build.lib.shell.SubprocessBuilder; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.SortedMap; /** * Interface to a worker process running as a child process. * *
A worker process must follow this protocol to be usable via this class: The worker process is * spawned on demand. The worker process is free to exit whenever necessary, as new instances will * be relaunched automatically. Communication happens via the WorkerProtocol protobuf, sent to and * received from the worker process via stdin / stdout. * *
Other code in Blaze can talk to the worker process via input / output streams provided by this
* class.
*/
class Worker {
private final WorkerKey workerKey;
private final int workerId;
private final Path workDir;
private final Path logFile;
private Subprocess process;
private Thread shutdownHook;
Worker(WorkerKey workerKey, int workerId, final Path workDir, Path logFile) {
this.workerKey = workerKey;
this.workerId = workerId;
this.workDir = workDir;
this.logFile = logFile;
final Worker self = this;
this.shutdownHook =
new Thread() {
@Override
public void run() {
try {
self.shutdownHook = null;
self.destroy();
} catch (IOException e) {
// We can't do anything here.
}
}
};
Runtime.getRuntime().addShutdownHook(shutdownHook);
}
void createProcess() throws IOException {
List