Search in sources :

Example 1 with ApiClock

use of com.google.api.core.ApiClock 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 ApiClock

use of com.google.api.core.ApiClock 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 ApiClock

use of com.google.api.core.ApiClock in project google-cloud-java by GoogleCloudPlatform.

the class JobTest method testWaitForWithTimeout.

@Test
public void testWaitForWithTimeout() throws InterruptedException, TimeoutException {
    initializeExpectedJob(2);
    BigQuery.JobOption[] expectedOptions = { BigQuery.JobOption.fields(BigQuery.JobField.STATUS) };
    TimeUnit timeUnit = createStrictMock(TimeUnit.class);
    timeUnit.sleep(1);
    EasyMock.expectLastCall();
    ApiClock clock = createStrictMock(ApiClock.class);
    expect(clock.millisTime()).andReturn(0L);
    expect(clock.millisTime()).andReturn(1L);
    expect(clock.millisTime()).andReturn(3L);
    JobStatus status = createStrictMock(JobStatus.class);
    expect(status.getState()).andReturn(JobStatus.State.RUNNING);
    expect(status.getState()).andReturn(JobStatus.State.RUNNING);
    expect(bigquery.getOptions()).andReturn(mockOptions);
    expect(mockOptions.getClock()).andReturn(clock);
    Job runningJob = expectedJob.toBuilder().setStatus(status).build();
    expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(runningJob);
    expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(runningJob);
    replay(status, bigquery, timeUnit, clock, mockOptions);
    initializeJob();
    thrown.expect(TimeoutException.class);
    job.waitFor(WaitForOption.checkEvery(1, timeUnit), WaitForOption.timeout(3, TimeUnit.MILLISECONDS));
    verify(status, timeUnit, clock, mockOptions);
}
Also used : ApiClock(com.google.api.core.ApiClock) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.Test)

Example 4 with ApiClock

use of com.google.api.core.ApiClock in project google-cloud-java by GoogleCloudPlatform.

the class OperationTest method testWaitForWithTimeout.

@Test
public void testWaitForWithTimeout() throws InterruptedException, TimeoutException {
    initializeExpectedOperation(4);
    Compute.OperationOption[] expectedOptions = { Compute.OperationOption.fields(Compute.OperationField.STATUS) };
    TimeUnit timeUnit = createStrictMock(TimeUnit.class);
    timeUnit.sleep(1);
    EasyMock.expectLastCall();
    ApiClock clock = createStrictMock(ApiClock.class);
    expect(clock.millisTime()).andReturn(0L);
    expect(clock.millisTime()).andReturn(1L);
    expect(clock.millisTime()).andReturn(3L);
    Operation runningOperation = Operation.fromPb(serviceMockReturnsOptions, globalOperation.toPb().setError(null).setStatus("RUNNING"));
    expect(compute.getOptions()).andReturn(mockOptions);
    expect(mockOptions.getClock()).andReturn(clock);
    expect(compute.getOperation(GLOBAL_OPERATION_ID, expectedOptions)).andReturn(runningOperation);
    expect(compute.getOperation(GLOBAL_OPERATION_ID, expectedOptions)).andReturn(runningOperation);
    replay(compute, timeUnit, clock, mockOptions);
    initializeOperation();
    thrown.expect(TimeoutException.class);
    operation.waitFor(WaitForOption.checkEvery(1, timeUnit), WaitForOption.timeout(3, TimeUnit.MILLISECONDS));
    verify(compute, timeUnit, clock, mockOptions);
}
Also used : ApiClock(com.google.api.core.ApiClock) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.Test)

Aggregations

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