private void autoscaleCleanup() {
System.out.format("Cleanup autoscale %n");
// Remove ALL policies and groups with that name
for (GroupState g : groupApi.listGroupStates()) {
PolicyApi pa = autoscaleApi.getPolicyApiForZoneAndGroup(ZONE, g.getId());
for(ScalingPolicy p : pa.list()) {
if(p.getName().equals(NAME)) {
System.out.format("Found matching policy: %s with cooldown %s%n", p.getId(), p.getCooldown());
String policyId = p.getId();
if (!(p.getTarget().equals("0") && p.getTargetType().equals(ScalingPolicyTargetType.DESIRED_CAPACITY))) {
System.out.format("Removing servers %n");
// Update policy to 0 servers
CreateScalingPolicy scalingPolicy = CreateScalingPolicy.builder()
.cooldown(3)
.type(ScalingPolicyType.WEBHOOK)
.name(NAME)
.targetType(ScalingPolicyTargetType.DESIRED_CAPACITY)
.target("0")
.build();
pa.update(policyId, scalingPolicy);
Uninterruptibles.sleepUninterruptibly(5, TimeUnit.SECONDS);
try {
pa.execute(policyId);
} catch (Exception e) {
// This will fail to execute when the number of servers is already zero (no change).
}
}
Uninterruptibles.sleepUninterruptibly(5, TimeUnit.SECONDS);
pa.delete(policyId);
groupApi.delete(g.getId());
} else {
System.out.format("Found another policy: %s - %s with cooldown %s%n", p.getName(), p.getId(), p.getCooldown());
}
}