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);
}
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();
}
}
}
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());
}
}
}
}
Aggregations