use of com.microsoft.azure.vmagent.retry.LinearRetryForAllExceptions in project azure-vm-agents-plugin by jenkinsci.
the class AzureVMCloudRetensionStrategy method check.
protected long check(final AzureVMComputer agentNode, ExecutionEngine executionEngine) {
// Determine whether we can recycle this machine.
// The CRS is the way that nodes that are currently operating "correctly"
// can be retained/reclaimed. Any failure modes need to be dealt with through
// the clean up task.
boolean canRecycle = true;
// Node must be idle
canRecycle &= agentNode.isIdle();
// The node must also be online. This also implies not temporarily disconnected
// (like by a user).
canRecycle &= agentNode.isOnline();
// The configured idle time must be > 0 (which means leave forever)
canRecycle &= idleTerminationMillis > 0;
// The number of ms it's been idle must be greater than the current idle time.
canRecycle &= idleTerminationMillis < (System.currentTimeMillis() - agentNode.getIdleStartMilliseconds());
if (agentNode.getNode() == null) {
return 1;
}
final AzureVMAgent agent = agentNode.getNode();
if (canRecycle) {
LOGGER.log(Level.INFO, "Idle timeout reached for agent: {0}, action: {1}", new Object[] { agentNode.getName(), agent.isShutdownOnIdle() ? "shutdown" : "delete" });
Callable<Void> task = () -> {
// Block cleanup while we execute so the cleanup task doesn't try to take it
// away (node will go offline). Also blocks cleanup in case of shutdown.
agent.blockCleanUpAction();
if (agent.isShutdownOnIdle()) {
LOGGER.log(Level.INFO, "Going to idleTimeout agent: {0}", agentNode.getName());
agent.shutdown(Messages._Idle_Timeout_Shutdown());
} else {
agent.deprovision(Messages._Idle_Timeout_Delete());
}
return null;
};
try {
final int maxRetries = 3;
final int waitInterval = 30;
final int defaultTimeoutInSeconds = 30 * 60;
executionEngine.executeAsync(task, new LinearRetryForAllExceptions(maxRetries, waitInterval, defaultTimeoutInSeconds));
} catch (AzureCloudException ae) {
LOGGER.log(Level.WARNING, String.format("Could not terminate or shutdown %s", agentNode.getName()), ae);
// If we have an exception, set the agent for deletion.
// It's unlikely we'll be able to shut it down properly ever.
AzureVMAgent node = agentNode.getNode();
if (node != null) {
node.setCleanUpAction(CleanUpAction.DELETE, Messages._Failed_Initial_Shutdown_Or_Delete());
}
} catch (Exception e) {
LOGGER.log(Level.WARNING, String.format("Exception occurred while calling timeout on node %s", agentNode.getName()), e);
// If we have an exception, set the agent for deletion.
// It's unlikely we'll be able to shut it down properly ever.
AzureVMAgent node = agentNode.getNode();
if (node != null) {
node.setCleanUpAction(CleanUpAction.DELETE, Messages._Failed_Initial_Shutdown_Or_Delete());
}
}
}
return 1;
}
Aggregations