use of com.google.cloud.WaitForOption.Timeout 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();
}
use of com.google.cloud.WaitForOption.Timeout 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());
}
Aggregations