Search in sources :

Example 1 with DefaultRetryStrategy

use of com.microsoft.azure.vmagent.retry.DefaultRetryStrategy in project azure-vm-agents-plugin by jenkinsci.

the class AzureVMAgentCleanUpTask method cleanVMs.

private void cleanVMs(ExecutionEngine executionEngine) {
    LOGGER.log(getNormalLoggingLevel(), "Beginning");
    for (Computer computer : Jenkins.get().getComputers()) {
        if (computer instanceof AzureVMComputer) {
            AzureVMComputer azureComputer = (AzureVMComputer) computer;
            final AzureVMAgent agentNode = azureComputer.getNode();
            // If the machine is not offline, then don't do anything.
            if (!azureComputer.isOffline()) {
                continue;
            }
            // builds.
            if (!azureComputer.isIdle()) {
                continue;
            }
            // should stay (this could be for investigation).
            if (azureComputer.isSetOfflineByUser()) {
                LOGGER.log(getNormalLoggingLevel(), "Node {0} was set offline by user, skipping", agentNode.getDisplayName());
                continue;
            }
            // If the machine is in "keep" state, skip
            if (agentNode.isCleanUpBlocked()) {
                LOGGER.log(getNormalLoggingLevel(), "Node {0} blocked to cleanup", agentNode.getDisplayName());
                continue;
            }
            // deleted in the background.  Remove from Jenkins if that is the case.
            if (!AzureVMManagementServiceDelegate.virtualMachineExists(agentNode)) {
                LOGGER.log(getNormalLoggingLevel(), "Node {0} doesn't exist, removing", agentNode.getDisplayName());
                try {
                    Jenkins.get().removeNode(agentNode);
                } catch (IOException e) {
                    LOGGER.log(Level.WARNING, "Node {0} could not be removed: {1}", new Object[] { agentNode.getDisplayName(), e.getMessage() });
                }
                continue;
            }
            // Machine exists but is in either DELETE or SHUTDOWN state.
            // Execute that action.
            Callable<Void> task = () -> {
                // Depending on the cleanup action, run the appropriate
                if (agentNode.getCleanUpAction() == CleanUpAction.DELETE) {
                    LOGGER.log(getNormalLoggingLevel(), "Deleting {0}", agentNode.getDisplayName());
                    agentNode.deprovision(agentNode.getCleanUpReason());
                } else if (agentNode.getCleanUpAction() == CleanUpAction.SHUTDOWN) {
                    LOGGER.log(getNormalLoggingLevel(), "Shutting down {0}", agentNode.getDisplayName());
                    agentNode.shutdown(agentNode.getCleanUpReason());
                    // We shut down the agent properly.  Mark the agent
                    // as "KEEP" so that it doesn't get deleted.
                    agentNode.blockCleanUpAction();
                } else {
                    throw new IllegalStateException("Unknown cleanup action");
                }
                return null;
            };
            try {
                final int maxRetries = 3;
                final int waitInterval = 10;
                final int defaultTimeOutInSeconds = 30 * 60;
                executionEngine.executeAsync(task, new DefaultRetryStrategy(maxRetries, waitInterval, defaultTimeOutInSeconds));
            } catch (AzureCloudException exception) {
                // No need to throw exception back, just log and move on.
                LOGGER.log(Level.WARNING, "Failed to shutdown/delete " + agentNode.getDisplayName(), exception);
                // In case the node had a non-delete cleanup action before,
                // set the cleanup action to delete
                agentNode.setCleanUpAction(CleanUpAction.DELETE, Messages._Failed_Initial_Shutdown_Or_Delete());
            }
        }
    }
    LOGGER.log(getNormalLoggingLevel(), "Completed");
}
Also used : Computer(hudson.model.Computer) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) DefaultRetryStrategy(com.microsoft.azure.vmagent.retry.DefaultRetryStrategy) IOException(java.io.IOException)

Aggregations

AzureCloudException (com.microsoft.azure.vmagent.exceptions.AzureCloudException)1 DefaultRetryStrategy (com.microsoft.azure.vmagent.retry.DefaultRetryStrategy)1 Computer (hudson.model.Computer)1 IOException (java.io.IOException)1