JCloudsRetentionStrategy extends RetentionStrategy<JCloudsComputer> { @Override public long check(@Nonnull JCloudsComputer c) { if (disabled) { LOGGER.fine("Skipping check - disabled"); return 1; } LOGGER.finest("Checking"); if (!checkLock.tryLock()) { LOGGER.info("Failed to acquire retention lock - skipping"); return 1; } try { doCheck(c); } finally { checkLock.unlock(); } return 1; } @DataBoundConstructor  JCloudsRetentionStrategy(); @Override long check(@Nonnull JCloudsComputer c); @Override void start(JCloudsComputer c);  }
@Test public void scheduleSlaveDelete() throws Exception { int retentionTime = 1; JCloudsSlaveTemplate template = new JCloudsSlaveTemplate( "template", "label", SlaveOptions.builder().retentionTime(retentionTime).build() ); JCloudsCloud cloud = j.configureSlaveLaunchingWithFloatingIP(j.dummyCloud(template)); JCloudsSlave slave = j.provision(cloud, "label"); JCloudsComputer computer = slave.getComputer(); assertEquals(1, (int) slave.getSlaveOptions().getRetentionTime()); JCloudsRetentionStrategy strategy = (JCloudsRetentionStrategy) slave.getRetentionStrategy(); strategy.check(computer); assertFalse("Slave should not be scheduled for deletion right away", computer.isPendingDelete()); checkAfter(computer, 1000 * 61); assertTrue("Slave should be scheduled for deletion", computer.isPendingDelete()); }
@Test public void doNotDeleteSlavePutOfflineByUser() throws Exception { JCloudsCloud cloud = j.configureSlaveLaunchingWithFloatingIP(j.dummyCloud(j.dummySlaveTemplate( j.defaultSlaveOptions().getBuilder().retentionTime(1).build(), "label" ))); JCloudsSlave slave = j.provision(cloud, "label"); JCloudsComputer computer = slave.getComputer(); computer.waitUntilOnline(); OfflineCause.UserCause userCause = new OfflineCause.UserCause(User.current(), "Offline"); computer.setTemporarilyOffline(true, userCause); computer.getRetentionStrategy().check(computer); assertFalse(computer.isPendingDelete()); assertEquals(userCause, computer.getOfflineCause()); computer.setTemporarilyOffline(false, null); checkAfter(computer, 61*1000); assertTrue(computer.isPendingDelete()); }
@Test public void doNotScheduleForTerminationDuringLaunch() throws Exception { Assume.assumeFalse(Functions.isWindows()); LauncherFactory launcherFactory = new TestCommandLauncherFactory("bash -c \"sleep 70 && java -jar '%s'\""); JCloudsCloud cloud = j.configureSlaveProvisioningWithFloatingIP(j.dummyCloud(j.dummySlaveTemplate( j.defaultSlaveOptions().getBuilder().retentionTime(1).launcherFactory(launcherFactory).build(), "label" ))); j.provision(cloud, "label"); while (true) { JCloudsComputer computer = waitForProvisionedComputer(); computer.getRetentionStrategy().check(computer); computer.waitUntilOnline(); if (computer.getChannel() != null) { break; } assertNull(computer.getOfflineCause()); Thread.sleep(5000); } }
@Test public void doNotRemoveSlaveShortlyAfterConnection() throws Exception { Assume.assumeFalse(Functions.isWindows()); LauncherFactory launcherFactory = new TestCommandLauncherFactory("bash -c \"sleep 70 && java -jar '%s'\""); JCloudsCloud cloud = j.configureSlaveProvisioningWithFloatingIP(j.dummyCloud(j.dummySlaveTemplate( j.defaultSlaveOptions().getBuilder().retentionTime(1).launcherFactory(launcherFactory).build(), "label" ))); JCloudsSlave slave = j.provision(cloud, "label"); JCloudsComputer computer = (JCloudsComputer) slave.toComputer(); while(computer.getChannel() == null){ Thread.sleep(1000); } computer.getRetentionStrategy().check(computer); assertFalse(computer.isPendingDelete()); }