Search in sources :

Example 1 with CancelOperationRequest

use of com.google.longrunning.CancelOperationRequest in project gax-java by googleapis.

the class OperationsClient method cancelOperation.

// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
 * Starts asynchronous cancellation on a long-running operation. The server makes a best effort to
 * cancel the operation, but success is not guaranteed. If the server doesn't support this method,
 * it returns `google.rpc.Code.UNIMPLEMENTED`. Clients can use
 * [Operations.GetOperation][google.longrunning.Operations.GetOperation] or other methods to check
 * whether the cancellation succeeded or whether the operation completed despite cancellation. On
 * successful cancellation, the operation is not deleted; instead, it becomes an operation with an
 * [Operation.error][google.longrunning.Operation.error] value with a
 * [google.rpc.Status.code][google.rpc.Status.code] of 1, corresponding to `Code.CANCELLED`.
 *
 * <p>Sample code:
 *
 * <pre>{@code
 * try (OperationsClient operationsClient = OperationsClient.create()) {
 *   String name = "name3373707";
 *   operationsClient.cancelOperation(name);
 * }
 * }</pre>
 *
 * @param name The name of the operation resource to be cancelled.
 * @throws com.google.api.gax.rpc.ApiException if the remote call fails
 */
public final void cancelOperation(String name) {
    CancelOperationRequest request = CancelOperationRequest.newBuilder().setName(name).build();
    cancelOperation(request);
}
Also used : CancelOperationRequest(com.google.longrunning.CancelOperationRequest)

Example 2 with CancelOperationRequest

use of com.google.longrunning.CancelOperationRequest in project java-spanner by googleapis.

the class GapicSpannerRpc method cancelOperation.

@Override
public void cancelOperation(String name) throws SpannerException {
    acquireAdministrativeRequestsRateLimiter();
    final CancelOperationRequest request = CancelOperationRequest.newBuilder().setName(name).build();
    final GrpcCallContext context = newCallContext(null, name, request, OperationsGrpc.getCancelOperationMethod());
    runWithRetryOnAdministrativeRequestsExceeded(() -> {
        get(databaseAdminStub.getOperationsStub().cancelOperationCallable().futureCall(request, context));
        return null;
    });
}
Also used : GrpcCallContext(com.google.api.gax.grpc.GrpcCallContext) CancelOperationRequest(com.google.longrunning.CancelOperationRequest)

Example 3 with CancelOperationRequest

use of com.google.longrunning.CancelOperationRequest in project gax-java by googleapis.

the class GrpcLongRunningClientTest method cancel.

@Test
public void cancel() {
    OperationsStub operationsStub = mock(OperationsStub.class);
    when(operationsStub.cancelOperationCallable()).thenReturn(new UnaryCallable<CancelOperationRequest, Empty>() {

        @Override
        public ApiFuture<Empty> futureCall(CancelOperationRequest request, ApiCallContext context) {
            if (!request.getName().equals("poseidon")) {
                return ApiFutures.immediateFailedFuture(new IllegalStateException("Unexpected request: " + request));
            } else {
                return ApiFutures.immediateFuture(Empty.getDefaultInstance());
            }
        }
    });
    LongRunningClient longRunningClient = new GrpcLongRunningClient(operationsStub);
    Truth.assertThat(longRunningClient.cancelOperationCallable().call("poseidon")).isNull();
}
Also used : ApiFuture(com.google.api.core.ApiFuture) OperationsStub(com.google.longrunning.stub.OperationsStub) Empty(com.google.protobuf.Empty) LongRunningClient(com.google.api.gax.rpc.LongRunningClient) CancelOperationRequest(com.google.longrunning.CancelOperationRequest) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) Test(org.junit.Test)

Example 4 with CancelOperationRequest

use of com.google.longrunning.CancelOperationRequest in project bazel-buildfarm by bazelbuild.

the class BuildFarmServerTest method cancelledOperationHasCancelledState.

@Test
public void cancelledOperationHasCancelledState() throws IOException, InterruptedException {
    Operation operation = executeAction(createSimpleAction());
    OperationsGrpc.OperationsBlockingStub operationsStub = OperationsGrpc.newBlockingStub(inProcessChannel);
    // should be available with cancelled state
    Operation preCancelOperation = getOperation(operation.getName());
    assertThat(preCancelOperation.getDone()).isFalse();
    CancelOperationRequest cancelRequest = CancelOperationRequest.newBuilder().setName(operation.getName()).build();
    operationsStub.cancelOperation(cancelRequest);
    Operation cancelledOperation = getOperation(operation.getName());
    assertThat(cancelledOperation.getDone()).isTrue();
    assertThat(cancelledOperation.getResultCase()).isEqualTo(Operation.ResultCase.RESPONSE);
    ExecuteResponse executeResponse = cancelledOperation.getResponse().unpack(ExecuteResponse.class);
    assertThat(executeResponse.getStatus().getCode()).isEqualTo(Code.CANCELLED.getNumber());
}
Also used : OperationsGrpc(com.google.longrunning.OperationsGrpc) CancelOperationRequest(com.google.longrunning.CancelOperationRequest) ExecuteResponse(build.bazel.remote.execution.v2.ExecuteResponse) Operation(com.google.longrunning.Operation) Test(org.junit.Test)

Example 5 with CancelOperationRequest

use of com.google.longrunning.CancelOperationRequest in project bazel-buildfarm by bazelbuild.

the class BuildFarmServerTest method canceledOperationIsNoLongerOutstanding.

@Test
public void canceledOperationIsNoLongerOutstanding() throws IOException, InterruptedException {
    Operation operation = executeAction(createSimpleAction());
    // should appear in outstanding list
    ListOperationsRequest listRequest = ListOperationsRequest.newBuilder().setName(INSTANCE_NAME + "/operations").setPageSize(1024).build();
    OperationsGrpc.OperationsBlockingStub operationsStub = OperationsGrpc.newBlockingStub(inProcessChannel);
    ListOperationsResponse listResponse = operationsStub.listOperations(listRequest);
    assertThat(Iterables.transform(listResponse.getOperationsList(), Operation::getName)).containsExactly(operation.getName());
    CancelOperationRequest cancelRequest = CancelOperationRequest.newBuilder().setName(operation.getName()).build();
    operationsStub.cancelOperation(cancelRequest);
    // should now be gone
    listResponse = operationsStub.listOperations(listRequest);
    assertThat(listResponse.getOperationsList()).isEmpty();
}
Also used : ListOperationsResponse(com.google.longrunning.ListOperationsResponse) OperationsGrpc(com.google.longrunning.OperationsGrpc) CancelOperationRequest(com.google.longrunning.CancelOperationRequest) ListOperationsRequest(com.google.longrunning.ListOperationsRequest) Operation(com.google.longrunning.Operation) Test(org.junit.Test)

Aggregations

CancelOperationRequest (com.google.longrunning.CancelOperationRequest)5 Test (org.junit.Test)3 Operation (com.google.longrunning.Operation)2 OperationsGrpc (com.google.longrunning.OperationsGrpc)2 ExecuteResponse (build.bazel.remote.execution.v2.ExecuteResponse)1 ApiFuture (com.google.api.core.ApiFuture)1 GrpcCallContext (com.google.api.gax.grpc.GrpcCallContext)1 ApiCallContext (com.google.api.gax.rpc.ApiCallContext)1 LongRunningClient (com.google.api.gax.rpc.LongRunningClient)1 ListOperationsRequest (com.google.longrunning.ListOperationsRequest)1 ListOperationsResponse (com.google.longrunning.ListOperationsResponse)1 OperationsStub (com.google.longrunning.stub.OperationsStub)1 Empty (com.google.protobuf.Empty)1