Search in sources :

Example 1 with RunResponse

use of com.google.devtools.build.lib.server.CommandProtos.RunResponse in project bazel by bazelbuild.

the class GrpcServerImpl method executeCommand.

private void executeCommand(RunRequest request, StreamObserver<RunResponse> observer, GrpcSink sink) {
    sink.setCommandThread(Thread.currentThread());
    if (!request.getCookie().equals(requestCookie) || request.getClientDescription().isEmpty()) {
        try {
            observer.onNext(RunResponse.newBuilder().setExitCode(ExitCode.LOCAL_ENVIRONMENTAL_ERROR.getNumericExitCode()).build());
            observer.onCompleted();
        } catch (StatusRuntimeException e) {
            log.info("Client cancelled command while rejecting it: " + e.getMessage());
        }
        return;
    }
    // case by explicitly checking for disconnection here.
    if (sink.disconnected()) {
        return;
    }
    ImmutableList.Builder<String> args = ImmutableList.builder();
    for (ByteString requestArg : request.getArgList()) {
        args.add(requestArg.toString(CHARSET));
    }
    String commandId;
    int exitCode;
    try (RunningCommand command = new RunningCommand()) {
        commandId = command.id;
        try {
            // Send the client the command id as soon as we know it.
            observer.onNext(RunResponse.newBuilder().setCookie(responseCookie).setCommandId(commandId).build());
        } catch (StatusRuntimeException e) {
            log.info("The client cancelled the command before receiving the command id: " + e.getMessage());
        }
        OutErr rpcOutErr = OutErr.create(new RpcOutputStream(command.id, responseCookie, StreamType.STDOUT, sink), new RpcOutputStream(command.id, responseCookie, StreamType.STDERR, sink));
        exitCode = commandExecutor.exec(args.build(), rpcOutErr, request.getBlockForLock() ? LockingMode.WAIT : LockingMode.ERROR_OUT, request.getClientDescription(), clock.currentTimeMillis());
    } catch (InterruptedException e) {
        exitCode = ExitCode.INTERRUPTED.getNumericExitCode();
        // The default value, the client will ignore it
        commandId = "";
    }
    if (sink.finish()) {
        // Client disconnected. Then we are not allowed to call any methods on the observer.
        log.info(String.format("Client disconnected before we could send exit code for command %s", commandId));
        return;
    }
    // There is a chance that an Uninterruptibles#getUninterruptibly() leaves us with the
    // interrupt bit set. So we just reset the interruption state here to make these cancel
    // requests not have any effect outside of command execution (after the try block above,
    // the cancel request won't find the thread to interrupt)
    Thread.interrupted();
    RunResponse response = RunResponse.newBuilder().setCookie(responseCookie).setCommandId(commandId).setFinished(true).setExitCode(exitCode).build();
    try {
        observer.onNext(response);
        observer.onCompleted();
    } catch (StatusRuntimeException e) {
        // The client cancelled the call. Log an error and go on.
        log.info(String.format("Client cancelled command %s just right before its end: %s", commandId, e.getMessage()));
    }
    if (commandExecutor.shutdown()) {
        pidFileWatcherThread.signalShutdown();
        server.shutdown();
    }
}
Also used : OutErr(com.google.devtools.build.lib.util.io.OutErr) ImmutableList(com.google.common.collect.ImmutableList) ByteString(com.google.protobuf.ByteString) RunResponse(com.google.devtools.build.lib.server.CommandProtos.RunResponse) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 RunResponse (com.google.devtools.build.lib.server.CommandProtos.RunResponse)1 OutErr (com.google.devtools.build.lib.util.io.OutErr)1 ByteString (com.google.protobuf.ByteString)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1