google-code-export / typica

Automatically exported from code.google.com/p/typica
Apache License 2.0
1 stars 1 forks source link

Problem Monitoring Instance State #76

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. J want to capture the change of state of an instance

2. This is the Java code : 

Jec2 ec2 = new Jec2(awsAccessId, awsSecretKey);
ReservationDescription rd  = ec2.runInstances("ami-id",1,1,new
ArrayList<String>(),"","gsg-keypair");

List<Instance> activeInstances = rd.getInstances();
for(int i=0; i<activeInstances.size(); i++) {
    Instance instance = activeInstances.get(i);
    while(instance.getState().equals("pending")) {
        System.out.println("Instance Id:" + instance.getInstanceId());
    System.out.println("Instance State:" + instance.getState());
    Thread.sleep(10000);
}
System.out.println("Instance : "+instance.getInstanceId()+"is Running");
} 

What is the expected output? What do you see instead?

I expect that once the application goes into running a country leaves the
loop and print "Instance ... is Running".

With this code, you do not exit the loop even when the state of an instance
is running.

Original issue reported on code.google.com by rufinida...@gmail.com on 8 Jan 2010 at 9:38

Attachments:

GoogleCodeExporter commented 9 years ago
Well, you never actually fetch the instance state again. You'll keep looking at 
the 
same state value, which will never change.

Try this;
        while (!runInst.getInstances().get(0).isRunning()) {
            List<ReservationDescription> tmp = ec2.describeInstances(new String 
[] {instanceId});
            if (tmp.size() > 0) {
                runInst = tmp.get(0);
                logger.info("instance state: 
"+runInst.getInstances().get(0).getState());
                logger.info("public IP = 
"+runInst.getInstances().get(0).getIpAddress());
                try { Thread.sleep(5000); } catch (InterruptedException ex) {}
            }
        }

Though, I'd suggest passing your instance ID into the getInstances() call, so 
you get 
status on only the instance in question.

Original comment by dkavan...@gmail.com on 23 Mar 2010 at 3:57