LordDoskias
LordDoskias

Reputation: 3191

Blocking until all EC2 instances are in a particular state?

I wrote a functions which is supposed to block until all instances in a list are not in a particular state, so here is what I have:

private void waitForInstanceState(List<String> instancesId, InstanceStateName state) {
    int numAchievedState = 0;

    while (numAchievedState != instancesId.size()) {

        numAchievedState = 0;

        DescribeInstanceStatusRequest describeRequest = new DescribeInstanceStatusRequest().withInstanceIds(instancesId);
        DescribeInstanceStatusResult instanceStatus = ec2.describeInstanceStatus(describeRequest);

        for (InstanceStatus status : instanceStatus.getInstanceStatuses()) {
            if (status.getInstanceState().getName().equals(state.toString())) {
                numAchievedState++;
            }
        }

        try {
            Thread.sleep(15000);
        } catch (InterruptedException ex) {
            Logger.getLogger(AmazonLibrary.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

The code that invokes the above function is like that:

public void startInstace(List<String> instancesId) {

    StartInstancesRequest startRequest = new StartInstancesRequest(instancesId);
    ec2.startInstances(startRequest);

    waitForInstanceState(instancesId, InstanceStateName.Running);

    System.out.println("The instance has been started");


}

The thing is sometimes the describe instance request returns null for InstanceStatus which I think should never happen since an instance in amazon always have a state? Also when used in the instanceRun function it works, when run with InstanceStateName set to Stopped it doesn't. No exceptions or errors are thrown.

Upvotes: 0

Views: 622

Answers (1)

LordDoskias
LordDoskias

Reputation: 3191

The following did the job:

 private void waitForInstanceState(List<String> instancesId, InstanceStateName state) {
        int numAchievedState = 0;

        while (numAchievedState != instancesId.size()) {

            try {
                Thread.sleep(15000);
            } catch (InterruptedException ex) {
                Logger.getLogger(AmazonLibrary.class.getName()).log(Level.SEVERE, null, ex);
            }

            numAchievedState = 0;

            DescribeInstancesRequest describeInstance = new DescribeInstancesRequest().withInstanceIds(instancesId);
            DescribeInstancesResult describeResult = ec2.describeInstances(describeInstance);
            List<Reservation> reservations = describeResult.getReservations();

            //different instances might be in different reservation requests
            //so we need to traverse those
            for (Reservation reservation : reservations) {
                List<Instance> instances = reservation.getInstances();
                for (Instance instance : instances) {
                    if (instance.getState().getName().equals(state.toString())) {
                        numAchievedState++;
                    }
                }
            }
        }
    }

Upvotes: 1

Related Questions