use of com.google.devtools.build.lib.util.io.FileOutErr in project bazel by bazelbuild.
the class StandaloneSpawnStrategy method actuallyExec.
/**
* Executes the given {@code spawn}.
*/
private void actuallyExec(Spawn spawn, ActionExecutionContext actionExecutionContext) throws ExecException {
Executor executor = actionExecutionContext.getExecutor();
if (executor.reportsSubcommands()) {
executor.reportSubcommand(spawn);
}
int timeoutSeconds = Spawns.getTimeoutSeconds(spawn);
// We must wrap the subprocess with process-wrapper to kill the process tree.
// All actions therefore depend on the process-wrapper file. Since it's embedded,
// we don't bother with declaring it as an input.
List<String> args = new ArrayList<>();
if (OS.getCurrent() != OS.WINDOWS) {
// TODO(bazel-team): process-wrapper seems to work on Windows, but requires
// additional setup as it is an msys2 binary, so it needs msys2 DLLs on %PATH%.
// Disable it for now to make the setup easier and to avoid further PATH hacks.
// Ideally we should have a native implementation of process-wrapper for Windows.
args.add(processWrapper.getPathString());
args.add(Integer.toString(timeoutSeconds));
args.add("5");
/* kill delay: give some time to print stacktraces and whatnot. */
// TODO(bazel-team): use process-wrapper redirection so we don't have to
// pass test logs through the Java heap.
args.add("-");
/* stdout. */
args.add("-");
/* stderr. */
}
args.addAll(spawn.getArguments());
String cwd = executor.getExecRoot().getPathString();
Command cmd = new Command(args.toArray(new String[] {}), locallyDeterminedEnv(execRoot, productName, spawn.getEnvironment()), new File(cwd), OS.getCurrent() == OS.WINDOWS && timeoutSeconds >= 0 ? timeoutSeconds * 1000 : -1);
FileOutErr outErr = actionExecutionContext.getFileOutErr();
try {
cmd.execute(/* stdin */
new byte[] {}, Command.NO_OBSERVER, outErr.getOutputStream(), outErr.getErrorStream(), /*killSubprocessOnInterrupt*/
true);
} catch (AbnormalTerminationException e) {
TerminationStatus status = e.getResult().getTerminationStatus();
boolean timedOut = !status.exited() && (status.timedout() || status.getTerminatingSignal() == 14);
String message = CommandFailureUtils.describeCommandFailure(verboseFailures, spawn.getArguments(), spawn.getEnvironment(), cwd);
throw new UserExecException(String.format("%s: %s", message, e), timedOut);
} catch (CommandException e) {
String message = CommandFailureUtils.describeCommandFailure(verboseFailures, spawn.getArguments(), spawn.getEnvironment(), cwd);
throw new UserExecException(message, e);
}
}
use of com.google.devtools.build.lib.util.io.FileOutErr in project bazel by bazelbuild.
the class SkyframeActionExecutor method dumpRecordedOutErr.
/**
* Dump the output from the action.
*
* @param prefixEvent An event to post before dumping the output
* @param outErrBuffer The OutErr that recorded the actions output
*/
private void dumpRecordedOutErr(Event prefixEvent, FileOutErr outErrBuffer) {
// actions will not be interleaved.
synchronized (reporter) {
// Only print the output if we're not winding down.
if (isBuilderAborting()) {
return;
}
reporter.handle(prefixEvent);
if (outErrBuffer != null && outErrBuffer.hasRecordedOutput()) {
OutErr outErr = this.reporter.getOutErr();
outErrBuffer.dumpOutAsLatin1(outErr.getOutputStream());
outErrBuffer.dumpErrAsLatin1(outErr.getErrorStream());
}
}
}
use of com.google.devtools.build.lib.util.io.FileOutErr in project bazel by bazelbuild.
the class WorkerSpawnStrategy method actuallyExec.
private void actuallyExec(Spawn spawn, ActionExecutionContext actionExecutionContext, AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles) throws ExecException, InterruptedException {
Executor executor = actionExecutionContext.getExecutor();
EventHandler eventHandler = executor.getEventHandler();
if (executor.reportsSubcommands()) {
executor.reportSubcommand(spawn);
}
// We assume that the spawn to be executed always gets at least one @flagfile.txt or
// --flagfile=flagfile.txt argument, which contains the flags related to the work itself (as
// opposed to start-up options for the executed tool). Thus, we can extract those elements from
// its args and put them into the WorkRequest instead.
List<String> flagfiles = new ArrayList<>();
List<String> startupArgs = new ArrayList<>();
for (String arg : spawn.getArguments()) {
if (FLAG_FILE_PATTERN.matcher(arg).matches()) {
flagfiles.add(arg);
} else {
startupArgs.add(arg);
}
}
if (flagfiles.isEmpty()) {
throw new UserExecException(String.format(ERROR_MESSAGE_PREFIX + REASON_NO_FLAGFILE, spawn.getMnemonic()));
}
if (Iterables.isEmpty(spawn.getToolFiles())) {
throw new UserExecException(String.format(ERROR_MESSAGE_PREFIX + REASON_NO_TOOLS, spawn.getMnemonic()));
}
FileOutErr outErr = actionExecutionContext.getFileOutErr();
ImmutableList<String> args = ImmutableList.<String>builder().addAll(startupArgs).add("--persistent_worker").addAll(MoreObjects.firstNonNull(extraFlags.get(spawn.getMnemonic()), ImmutableList.<String>of())).build();
ImmutableMap<String, String> env = spawn.getEnvironment();
try {
ActionInputFileCache inputFileCache = actionExecutionContext.getActionInputFileCache();
HashCode workerFilesHash = WorkerFilesHash.getWorkerFilesHash(spawn.getToolFiles(), actionExecutionContext);
Map<PathFragment, Path> inputFiles = new SpawnHelpers(execRoot).getMounts(spawn, actionExecutionContext);
Set<PathFragment> outputFiles = SandboxHelpers.getOutputFiles(spawn);
WorkerKey key = new WorkerKey(args, env, execRoot, spawn.getMnemonic(), workerFilesHash, inputFiles, outputFiles, writeOutputFiles != null);
WorkRequest.Builder requestBuilder = WorkRequest.newBuilder();
for (String flagfile : flagfiles) {
expandArgument(requestBuilder, flagfile);
}
List<ActionInput> inputs = ActionInputHelper.expandArtifacts(spawn.getInputFiles(), actionExecutionContext.getArtifactExpander());
for (ActionInput input : inputs) {
byte[] digestBytes = inputFileCache.getDigest(input);
ByteString digest;
if (digestBytes == null) {
digest = ByteString.EMPTY;
} else {
digest = ByteString.copyFromUtf8(HashCode.fromBytes(digestBytes).toString());
}
requestBuilder.addInputsBuilder().setPath(input.getExecPathString()).setDigest(digest).build();
}
WorkResponse response = execInWorker(eventHandler, key, requestBuilder.build(), maxRetries, writeOutputFiles);
outErr.getErrorStream().write(response.getOutputBytes().toByteArray());
if (response.getExitCode() != 0) {
throw new UserExecException(String.format("Worker process sent response with exit code: %d.", response.getExitCode()));
}
} catch (IOException e) {
String message = CommandFailureUtils.describeCommandFailure(verboseFailures, spawn.getArguments(), env, execRoot.getPathString());
throw new UserExecException(message, e);
}
}
use of com.google.devtools.build.lib.util.io.FileOutErr in project bazel by bazelbuild.
the class FileWriteActionTestCase method createExecutorAndContext.
@Before
public final void createExecutorAndContext() throws Exception {
executor = new TestExecutorBuilder(directories, binTools).build();
context = new ActionExecutionContext(executor, null, null, new FileOutErr(), ImmutableMap.<String, String>of(), null);
}
Aggregations