Search in sources :

Example 1 with JobExecutionStatus

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);
}
Also used : JobExecutionStatus(software.amazon.awssdk.services.iot.model.JobExecutionStatus) Instant(java.time.Instant) ResourceNotFoundException(software.amazon.awssdk.services.iot.model.ResourceNotFoundException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

Instant (java.time.Instant)1 TimeoutException (java.util.concurrent.TimeoutException)1 JobExecutionStatus (software.amazon.awssdk.services.iot.model.JobExecutionStatus)1 ResourceNotFoundException (software.amazon.awssdk.services.iot.model.ResourceNotFoundException)1