DeploymentExpirer extends ControllerMaintainer { @Override protected boolean maintain() { boolean success = true; for (Application application : controller().applications().readable()) for (Instance instance : application.instances().values()) for (Deployment deployment : instance.deployments().values()) { if ( ! isExpired(deployment)) continue; try { log.log(Level.INFO, "Expiring deployment of " + instance.id() + " in " + deployment.zone()); controller().applications().deactivate(instance.id(), deployment.zone()); } catch (Exception e) { success = false; log.log(Level.WARNING, "Could not expire " + deployment + " of " + instance + ": " + Exceptions.toMessageString(e) + ". Retrying in " + interval()); } } return success; } DeploymentExpirer(Controller controller, Duration interval);   }
@Test public void testDeploymentExpiry() { tester.controllerTester().zoneRegistry().setDeploymentTimeToLive( ZoneId.from(Environment.dev, RegionName.from("us-east-1")), Duration.ofDays(14) ); DeploymentExpirer expirer = new DeploymentExpirer(tester.controller(), Duration.ofDays(10) ); var devApp = tester.newDeploymentContext("tenant1", "app1", "default"); var prodApp = tester.newDeploymentContext("tenant2", "app2", "default"); ApplicationPackage appPackage = new ApplicationPackageBuilder() .region("us-west-1") .build(); devApp.runJob(JobType.devUsEast1, appPackage); prodApp.submit(appPackage).deploy(); assertEquals(1, permanentDeployments(devApp.instance()).size()); assertEquals(1, permanentDeployments(prodApp.instance()).size()); expirer.maintain(); assertEquals(1, permanentDeployments(devApp.instance()).size()); assertEquals(1, permanentDeployments(prodApp.instance()).size()); tester.clock().advance(Duration.ofDays(15)); expirer.maintain(); assertEquals(0, permanentDeployments(devApp.instance()).size()); assertEquals(1, permanentDeployments(prodApp.instance()).size()); }