Search in sources :

Example 1 with CheckingPeriod

use of com.google.cloud.WaitForOption.CheckingPeriod 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)

Example 2 with CheckingPeriod

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

the class Job method waitFor.

/**
   * Blocks until this job completes its execution, either failing or succeeding. This method
   * returns current job's latest information. If the job no longer exists, this method returns
   * {@code null}. By default, the job 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
   * Job completedJob = job.waitFor();
   * if (completedJob == null) {
   *   // job no longer exists
   * } else if (completedJob.getStatus().getError() != null) {
   *   // job failed, handle error
   * } else {
   *   // job completed successfully
   * }
   * }</pre>
   *
   * <p>Example usage of {@code waitFor()} with checking period and timeout.
   * <pre> {@code
   * Job completedJob =
   *     job.waitFor(
   *         WaitForOption.checkEvery(1, TimeUnit.SECONDS),
   *         WaitForOption.timeout(60, TimeUnit.SECONDS));
   * if (completedJob == null) {
   *   // job no longer exists
   * } else if (completedJob.getStatus().getError() != null) {
   *   // job failed, handle error
   * } else {
   *   // job completed successfully
   * }
   * }</pre>
   *
   * @param waitOptions options to configure checking period and timeout
   * @throws BigQueryException upon failure
   * @throws InterruptedException if the current thread gets interrupted while waiting for the job
   *     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 Job waitFor(WaitForOption... waitOptions) throws InterruptedException, TimeoutException {
    Timeout timeout = 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) Timeout(com.google.cloud.WaitForOption.Timeout) CheckingPeriod(com.google.cloud.WaitForOption.CheckingPeriod) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with CheckingPeriod

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

the class Operation method waitFor.

/**
   * Blocks till the operation is complete or maximum time, if specified, has elapsed.
   *
   * @return null if operation is not found otherwise the current operation.
   */
public Operation<R, M> waitFor(WaitForOption... options) throws SpannerException {
    if (isDone) {
        return this;
    }
    long timeoutMillis = Timeout.getOrDefault(options).getTimeoutMillis();
    boolean hasTimeout = timeoutMillis != -1;
    CheckingPeriod period = CheckingPeriod.getOrDefault(options);
    long startMillis = clock.millisTime();
    while (true) {
        try {
            com.google.longrunning.Operation proto = rpc.getOperation(name);
            if (proto.getDone()) {
                return Operation.<R, M>create(rpc, proto, parser);
            }
            long elapsed = clock.millisTime() - startMillis;
            if (hasTimeout && elapsed >= timeoutMillis) {
                throw SpannerExceptionFactory.newSpannerException(ErrorCode.DEADLINE_EXCEEDED, "Operation did not complete in the given time");
            }
        } catch (SpannerException e) {
            if (e.getErrorCode() == ErrorCode.NOT_FOUND) {
                return null;
            }
            if (!e.isRetryable()) {
                throw e;
            }
        }
        try {
            period.getUnit().sleep(period.getPeriod());
        } catch (InterruptedException e) {
            throw SpannerExceptionFactory.propagateInterrupt(e);
        }
    }
}
Also used : CheckingPeriod(com.google.cloud.WaitForOption.CheckingPeriod)

Example 4 with CheckingPeriod

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

the class WaitForOptionTest method testGetOrDefault.

@Test
public void testGetOrDefault() {
    assertEquals(CHECKING_PERIOD_OPTION, CheckingPeriod.getOrDefault(CHECKING_PERIOD_OPTION, TIMEOUT_OPTION));
    assertEquals(TIMEOUT_OPTION, Timeout.getOrDefault(CHECKING_PERIOD_OPTION, TIMEOUT_OPTION));
    CheckingPeriod checkingPeriod = CheckingPeriod.getOrDefault(TIMEOUT_OPTION);
    assertEquals(500, checkingPeriod.getPeriod());
    assertEquals(TimeUnit.MILLISECONDS, checkingPeriod.getUnit());
    Timeout timeout = Timeout.getOrDefault(CHECKING_PERIOD_OPTION);
    assertEquals(-1, timeout.getTimeoutMillis());
}
Also used : CheckingPeriod(com.google.cloud.WaitForOption.CheckingPeriod) Timeout(com.google.cloud.WaitForOption.Timeout) Test(org.junit.Test)

Aggregations

CheckingPeriod (com.google.cloud.WaitForOption.CheckingPeriod)4 ApiClock (com.google.api.core.ApiClock)2 Timeout (com.google.cloud.WaitForOption.Timeout)2 TimeoutException (java.util.concurrent.TimeoutException)2 WaitForOption (com.google.cloud.WaitForOption)1 Test (org.junit.Test)1