public long check()

in src/main/java/com/amazon/jenkins/ec2fleet/IdleRetentionStrategy.java [28:73]


    public long check(final SlaveComputer computer) {
        final EC2FleetNodeComputer fc = (EC2FleetNodeComputer) computer;
        final EC2FleetCloud cloud = fc.getCloud();

        LOGGER.log(Level.INFO, "Check if node idle " + computer.getName());

        // in some multi-thread edge cases cloud could be null for some time, just be ok with that
        if (cloud == null) {
            LOGGER.warning("Edge case cloud is null for computer " + fc.getDisplayName()
                    + " should be autofixed in a few minutes, if no please create issue for plugin");
            return RE_CHECK_IN_MINUTE;
        }

        // Ensure that the EC2FleetCloud cannot be mutated from under us while
        // we're doing this check
        // Ensure nobody provisions onto this node until we've done
        // checking
        boolean shouldAcceptTasks = fc.isAcceptingTasks();
        boolean justTerminated = false;
        fc.setAcceptingTasks(false);
        try {
            if (fc.isIdle() && isIdleForTooLong(cloud, fc)) {
                // Find instance ID
                Node compNode = fc.getNode();
                if (compNode == null) {
                    return 0;
                }

                final String instanceId = compNode.getNodeName();
                if (cloud.scheduleToTerminate(instanceId)) {
                    // Instance successfully terminated, so no longer accept tasks
                    shouldAcceptTasks = false;
                    justTerminated = true;
                }
            }

            if (cloud.isAlwaysReconnect() && !justTerminated && fc.isOffline() && !fc.isConnecting() && fc.isLaunchSupported()) {
                LOGGER.log(Level.INFO, "Reconnecting to instance: " + fc.getDisplayName());
                fc.tryReconnect();
            }
        } finally {
            fc.setAcceptingTasks(shouldAcceptTasks);
        }

        return RE_CHECK_IN_MINUTE;
    }