Search in sources :

Example 1 with ListOperationsRequest

use of com.google.longrunning.ListOperationsRequest in project java-automl by googleapis.

the class GetOperationStatusTest method setUp.

@Before
public void setUp() throws IOException {
    // Use list operations to get a single operation id for the get call.
    try (AutoMlClient client = AutoMlClient.create()) {
        LocationName projectLocation = LocationName.of(PROJECT_ID, "us-central1");
        ListOperationsRequest request = ListOperationsRequest.newBuilder().setName(projectLocation.toString()).build();
        Operation operation = client.getOperationsClient().listOperations(request).iterateAll().iterator().next();
        operationId = operation.getName();
    }
    bout = new ByteArrayOutputStream();
    out = new PrintStream(bout);
    originalPrintStream = System.out;
    System.setOut(out);
}
Also used : PrintStream(java.io.PrintStream) ListOperationsRequest(com.google.longrunning.ListOperationsRequest) Operation(com.google.longrunning.Operation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AutoMlClient(com.google.cloud.automl.v1beta1.AutoMlClient) LocationName(com.google.cloud.automl.v1beta1.LocationName) Before(org.junit.Before)

Example 2 with ListOperationsRequest

use of com.google.longrunning.ListOperationsRequest in project java-automl by googleapis.

the class ListOperationStatusTest method setUp.

@Before
public void setUp() throws IOException, InterruptedException {
    bout = new ByteArrayOutputStream();
    out = new PrintStream(bout);
    originalPrintStream = System.out;
    System.setOut(out);
    // if the LRO status count more than 300, delete half of operations.
    try (AutoMlClient client = AutoMlClient.create()) {
        OperationsClient operationsClient = client.getOperationsClient();
        LocationName projectLocation = LocationName.of(PROJECT_ID, "us-central1");
        ListOperationsRequest listRequest = ListOperationsRequest.newBuilder().setName(projectLocation.toString()).build();
        List<String> operationFullPathsToBeDeleted = new ArrayList<>();
        for (Operation operation : operationsClient.listOperations(listRequest).iterateAll()) {
            // Filter: deleting already done operations.
            if (operation.getDone() && !operation.hasError()) {
                operationFullPathsToBeDeleted.add(operation.getName());
            }
        }
        if (operationFullPathsToBeDeleted.size() > 300) {
            System.out.println("Cleaning up...");
            for (String operationFullPath : operationFullPathsToBeDeleted.subList(0, operationFullPathsToBeDeleted.size() / 2)) {
                // retry_interval * (random value in range [1 - rand_factor, 1 + rand_factor])
                ExponentialBackOff exponentialBackOff = new ExponentialBackOff.Builder().setInitialIntervalMillis(60000).setMaxElapsedTimeMillis(300000).setRandomizationFactor(0.5).setMultiplier(1.1).setMaxIntervalMillis(80000).build();
                // delete unused operations.
                try {
                    operationsClient.deleteOperation(operationFullPath);
                } catch (ResourceExhaustedException ex) {
                    // exponential back off and retry.
                    long backOffInMillis = exponentialBackOff.nextBackOffMillis();
                    System.out.printf("Backing off for %d milliseconds " + "due to Resource exhaustion.\n", backOffInMillis);
                    if (backOffInMillis < 0) {
                        break;
                    }
                    System.out.println("Backing off" + backOffInMillis);
                    TimeUnit.MILLISECONDS.sleep(backOffInMillis);
                } catch (Exception ex) {
                    throw ex;
                }
            }
        } else {
            // Clear the list since we wont anything with the list.
            operationFullPathsToBeDeleted.clear();
        }
    }
}
Also used : PrintStream(java.io.PrintStream) ArrayList(java.util.ArrayList) OperationsClient(com.google.longrunning.OperationsClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Operation(com.google.longrunning.Operation) ExponentialBackOff(com.google.api.client.util.ExponentialBackOff) IOException(java.io.IOException) ResourceExhaustedException(com.google.api.gax.rpc.ResourceExhaustedException) LocationName(com.google.cloud.automl.v1.LocationName) ResourceExhaustedException(com.google.api.gax.rpc.ResourceExhaustedException) ListOperationsRequest(com.google.longrunning.ListOperationsRequest) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient) Before(org.junit.Before)

Example 3 with ListOperationsRequest

use of com.google.longrunning.ListOperationsRequest in project java-automl by googleapis.

the class ListOperationStatus method listOperationStatus.

// Get the status of an operation
static void listOperationStatus(String projectId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (AutoMlClient client = AutoMlClient.create()) {
        // A resource that represents Google Cloud Platform location.
        LocationName projectLocation = LocationName.of(projectId, "us-central1");
        // Create list operations request.
        ListOperationsRequest listrequest = ListOperationsRequest.newBuilder().setName(projectLocation.toString()).build();
        // List all the operations names available in the region by applying filter.
        for (Operation operation : client.getOperationsClient().listOperations(listrequest).iterateAll()) {
            System.out.println("Operation details:");
            System.out.format("\tName: %s\n", operation.getName());
            System.out.format("\tMetadata Type Url: %s\n", operation.getMetadata().getTypeUrl());
            System.out.format("\tDone: %s\n", operation.getDone());
            if (operation.hasResponse()) {
                System.out.format("\tResponse Type Url: %s\n", operation.getResponse().getTypeUrl());
            }
            if (operation.hasError()) {
                System.out.println("\tResponse:");
                System.out.format("\t\tError code: %s\n", operation.getError().getCode());
                System.out.format("\t\tError message: %s\n\n", operation.getError().getMessage());
            }
        }
    }
}
Also used : ListOperationsRequest(com.google.longrunning.ListOperationsRequest) Operation(com.google.longrunning.Operation) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient) LocationName(com.google.cloud.automl.v1.LocationName)

Aggregations

ListOperationsRequest (com.google.longrunning.ListOperationsRequest)3 Operation (com.google.longrunning.Operation)3 AutoMlClient (com.google.cloud.automl.v1.AutoMlClient)2 LocationName (com.google.cloud.automl.v1.LocationName)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintStream (java.io.PrintStream)2 Before (org.junit.Before)2 ExponentialBackOff (com.google.api.client.util.ExponentialBackOff)1 ResourceExhaustedException (com.google.api.gax.rpc.ResourceExhaustedException)1 AutoMlClient (com.google.cloud.automl.v1beta1.AutoMlClient)1 LocationName (com.google.cloud.automl.v1beta1.LocationName)1 OperationsClient (com.google.longrunning.OperationsClient)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1