Skip to content
Permalink
Browse files
Retries DescribeInstance calls a few times if it fails.
Addresses [JENKINS-15319].
  • Loading branch information
glasser committed Mar 11, 2013
1 parent 64a311d commit c4a24a9bfb7993e0723953d0f155761cb10f733d
Showing 1 changed file with 30 additions and 9 deletions.
@@ -31,6 +31,7 @@
import org.kohsuke.stapler.HttpRedirect;
import org.kohsuke.stapler.HttpResponse;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.model.DescribeInstancesRequest;
import com.amazonaws.services.ec2.model.GetConsoleOutputRequest;
@@ -77,7 +78,7 @@ public String getConsoleOutput() throws AmazonClientException {
*
* The cache can be flushed using {@link #updateInstanceDescription()}
*/
public Instance describeInstance() throws AmazonClientException {
public Instance describeInstance() throws AmazonClientException, InterruptedException {
if(ec2InstanceDescription==null)
ec2InstanceDescription = _describeInstance();
return ec2InstanceDescription;
@@ -86,7 +87,7 @@ public Instance describeInstance() throws AmazonClientException {
/**
* This will flush any cached description held by {@link #describeInstance()}.
*/
public Instance updateInstanceDescription() throws AmazonClientException {
public Instance updateInstanceDescription() throws AmazonClientException, InterruptedException {
return ec2InstanceDescription = _describeInstance();
}

@@ -96,31 +97,51 @@ public Instance updateInstanceDescription() throws AmazonClientException {
* <p>
* Unlike {@link #describeInstance()}, this method always return the current status by calling EC2.
*/
public InstanceState getState() throws AmazonClientException {
public InstanceState getState() throws AmazonClientException, InterruptedException {
ec2InstanceDescription=_describeInstance();
return InstanceState.find(ec2InstanceDescription.getState().getName());
}

/**
* Number of milli-secs since the instance was started.
*/
public long getUptime() throws AmazonClientException {
public long getUptime() throws AmazonClientException, InterruptedException {
return System.currentTimeMillis()-describeInstance().getLaunchTime().getTime();
}

/**
* Returns uptime in the human readable form.
*/
public String getUptimeString() throws AmazonClientException {
public String getUptimeString() throws AmazonClientException, InterruptedException {
return Util.getTimeSpanString(getUptime());
}

private Instance _describeInstance() throws AmazonClientException {
DescribeInstancesRequest request = new DescribeInstancesRequest();
request.setInstanceIds(Collections.<String>singletonList(getNode().getInstanceId()));
return EC2Cloud.get().connect().describeInstances(request).getReservations().get(0).getInstances().get(0);
private Instance _describeInstance() throws AmazonClientException, InterruptedException {
// Sometimes even after a successful RunInstances, DescribeInstances
// returns an error for a few seconds. We do a few retries instead of
// failing instantly. See [JENKINS-15319].
for (int i = 0; i < 5; i++) {
try {
return _describeInstanceOnce();
} catch (AmazonServiceException e) {
if (e.getErrorCode().equals("InvalidInstanceID.NotFound")) {
// retry in 5 seconds.
Thread.sleep(5000);
continue;
}
throw e;
}
}
// Last time, throw on any error.
return _describeInstanceOnce();
}

private Instance _describeInstanceOnce() throws AmazonClientException {
DescribeInstancesRequest request = new DescribeInstancesRequest();
request.setInstanceIds(Collections.<String>singletonList(getNode().getInstanceId()));
return EC2Cloud.get().connect().describeInstances(request).getReservations().get(0).getInstances().get(0);
}

/**
* When the slave is deleted, terminate the instance.
*/

0 comments on commit c4a24a9

Please sign in to comment.