Search in sources :

Example 1 with WaitForOption

use of com.google.cloud.WaitForOption in project google-cloud-java by GoogleCloudPlatform.

the class Operation method waitFor.

/**
   * Blocks until this operation completes its execution, either failing or succeeding. This method
   * returns current operation's latest information. If the operation no longer exists, this method
   * returns {@code null}. By default, the operation status is checked every 500 milliseconds, to
   * configure this value use {@link WaitForOption#checkEvery(long, TimeUnit)}. Use
   * {@link WaitForOption#timeout(long, TimeUnit)} to set the maximum time to wait.
   *
   * <p>Example usage of {@code waitFor()}:
   * <pre> {@code
   * Operation completedOperation = operation.waitFor();
   * if (completedOperation == null) {
   *   // operation no longer exists
   * } else if (completedOperation.errors() != null) {
   *   // operation failed, handle error
   * } else {
   *   // operation completed successfully
   * }}</pre>
   *
   * <p>Example usage of {@code waitFor()} with checking period and timeout:
   * <pre> {@code
   * Operation completedOperation =
   *     operation.waitFor(WaitForOption.checkEvery(1, TimeUnit.SECONDS),
   *         WaitForOption.timeout(60, TimeUnit.SECONDS));
   * if (completedOperation == null) {
   *   // operation no longer exists
   * } else if (completedOperation.errors() != null) {
   *   // operation failed, handle error
   * } else {
   *   // operation completed successfully
   * }}</pre>
   *
   * @param waitOptions options to configure checking period and timeout
   * @throws ComputeException upon failure
   * @throws InterruptedException if the current thread gets interrupted while waiting for the
   *     operation to complete
   * @throws TimeoutException if the timeout provided with
   *     {@link WaitForOption#timeout(long, TimeUnit)} is exceeded. If no such option is provided
   *     this exception is never thrown.
   */
public Operation waitFor(WaitForOption... waitOptions) throws InterruptedException, TimeoutException {
    WaitForOption.Timeout timeout = WaitForOption.Timeout.getOrDefault(waitOptions);
    CheckingPeriod checkingPeriod = CheckingPeriod.getOrDefault(waitOptions);
    long timeoutMillis = timeout.getTimeoutMillis();
    ApiClock clock = options.getClock();
    long startTime = clock.millisTime();
    while (!isDone()) {
        if (timeoutMillis != -1 && (clock.millisTime() - startTime) >= timeoutMillis) {
            throw new TimeoutException();
        }
        checkingPeriod.sleep();
    }
    return reload();
}
Also used : ApiClock(com.google.api.core.ApiClock) WaitForOption(com.google.cloud.WaitForOption) CheckingPeriod(com.google.cloud.WaitForOption.CheckingPeriod) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

ApiClock (com.google.api.core.ApiClock)1 WaitForOption (com.google.cloud.WaitForOption)1 CheckingPeriod (com.google.cloud.WaitForOption.CheckingPeriod)1 TimeoutException (java.util.concurrent.TimeoutException)1