}
private void killInstance(final String nameToKill) {
Set<? extends Instance> instances = connection.listInstances();
try {
Instance instance = Iterables.find(instances, new Predicate<Instance>() {
@Override
public boolean apply(Instance input) {
return input.getName().equals(nameToKill);
}
});
if (instance.getStatus() != Instance.Status.DEPROVISIONING
&& instance.getStatus() != Instance.Status.DEPROVISION_PENDING) {
System.out.println("deleting instance: " + instance);
int timeout = (instance.getStatus() == Instance.Status.NEW || instance.getStatus() == Instance.Status.PROVISIONING) ? 300
: 30;
assert new RetryablePredicate<Instance>(new InstanceActiveOrFailed(connection), timeout, 2,
TimeUnit.SECONDS).apply(instance) : instance;
connection.deleteInstance(instance.getId());
}
assert new RetryablePredicate<Instance>(new InstanceRemovedOrNotFound(connection), 120, 2, TimeUnit.SECONDS)
.apply(instance) : instance;
} catch (NoSuchElementException ex) {
}