use of software.amazon.awssdk.services.iot.model.JobExecutionStatus in project aws-greengrass-nucleus by aws-greengrass.
the class IotJobsUtils method waitForJobExecutionStatusToSatisfy.
public static void waitForJobExecutionStatusToSatisfy(IotClient client, String jobId, String thingName, Duration timeout, Predicate<JobExecutionStatus> condition) throws TimeoutException {
Instant deadline = Instant.now().plusMillis(timeout.toMillis());
JobExecutionStatus status = null;
ResourceNotFoundException lastException = null;
while (deadline.isAfter(Instant.now())) {
try {
status = client.describeJobExecution(DescribeJobExecutionRequest.builder().jobId(jobId).thingName(thingName).build()).execution().status();
// which means we can stop querying
if (JobExecutionStatus.SUCCEEDED.ordinal() <= status.ordinal() || condition.test(status)) {
if (condition.test(status)) {
return;
}
} else if (JobExecutionStatus.SUCCEEDED.ordinal() <= status.ordinal()) {
throw new AssertionError("Job ended in state: " + status);
}
} catch (ResourceNotFoundException e) {
lastException = e;
}
// Wait a little bit before checking again
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
}
throw new TimeoutException(status == null && lastException != null ? lastException.getMessage() : "Job execution status is " + status);
}
Aggregations