Skip to content

Commit

Permalink
Fix starting agent after idle shutdown (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
timja authored Dec 20, 2024
1 parent 45bbb66 commit 7288787
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
21 changes: 15 additions & 6 deletions src/main/java/com/microsoft/azure/vmagent/AzureVMAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ private void setCleanUpReason(Localizable cleanUpReason) {
public void clearCleanUpAction() {
setCleanUpAction(CleanUpAction.DEFAULT);
setCleanUpReason(null);

Computer computer = toComputer();
if (computer != null) {
computer.setTemporaryOfflineCause(null);
}
}

/**
Expand All @@ -424,7 +429,11 @@ public void setCleanUpAction(CleanUpAction action, Localizable reason) {
AzureVMComputer computer = (AzureVMComputer) this.toComputer();
if (computer != null) {
// Set the machine temporarily offline machine with an offline reason.
computer.setTemporarilyOffline(true, OfflineCause.create(reason));
if (action == CleanUpAction.SHUTDOWN) {
computer.setTemporaryOfflineCause(new OfflineCause.IdleOfflineCause());
} else {
computer.setTemporaryOfflineCause(OfflineCause.create(reason));
}
}
setCleanUpAction(action);
setCleanUpReason(reason);
Expand Down Expand Up @@ -570,7 +579,7 @@ public AzureVMCloud getCloud() {

public synchronized void shutdown(Localizable reason) {
if (isEligibleForReuse()) {
LOGGER.log(Level.INFO, "Agent {0} is always shut down", this.
LOGGER.log(Level.INFO, "Agent {0} is shut down", this.
getDisplayName());
return;
}
Expand All @@ -583,7 +592,7 @@ public synchronized void shutdown(Localizable reason) {
return;
}
computer.setAcceptingTasks(false);
computer.disconnect(OfflineCause.create(reason));
computer.setTemporaryOfflineCause(new OfflineCause.IdleOfflineCause());
LOGGER.log(Level.INFO, "Shutting down agent {0}", this.getDisplayName());

AzureVMManagementServiceDelegate serviceDelegate = getServiceDelegate();
Expand Down Expand Up @@ -615,8 +624,7 @@ public synchronized void deprovision(Localizable reason) throws Exception {

ComputerLauncher launcher = computer.getLauncher();

if ((launcher instanceof AzureVMAgentSSHLauncher)) {
AzureVMAgentSSHLauncher azureLauncher = (AzureVMAgentSSHLauncher) launcher;
if ((launcher instanceof AzureVMAgentSSHLauncher azureLauncher)) {
PrintStream terminateStream = new LogTaskListener(LOGGER, Level.INFO).getLogger();

final boolean isUnix = this.getOsType().equals(OperatingSystemTypes.LINUX);
Expand Down Expand Up @@ -649,7 +657,8 @@ public synchronized void deprovision(Localizable reason) throws Exception {
}
if (!skipTerminateScript
&& azureLauncher.executeRemoteCommand(this, command, terminateStream, isUnix) != 0) {
LOGGER.info("Terminate script is not null, " + "preparing to execute script remotely");
LOGGER.info("Terminate script present for " + computer.getName()

Check warning on line 660 in src/main/java/com/microsoft/azure/vmagent/AzureVMAgent.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 407-660 are not covered by tests
+ " preparing to execute script remotely");
if (isUnix) {
azureLauncher.copyFileToRemote(
this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,15 +484,20 @@ private void cleanVMs() {
private void cleanVMs(ExecutionEngine executionEngine) {
LOGGER.log(getNormalLoggingLevel(), "Beginning");
for (Computer computer : Jenkins.get().getComputers()) {
if (computer instanceof AzureVMComputer) {
AzureVMComputer azureComputer = (AzureVMComputer) computer;
if (computer instanceof AzureVMComputer azureComputer) {
final AzureVMAgent agentNode = azureComputer.getNode();

// If the machine is not offline, then don't do anything.
if (!azureComputer.isOffline()) {
continue;
}

if (agentNode == null) {
LOGGER.log(getNormalLoggingLevel(), "Node {0} is missing, skipping",
azureComputer.getDisplayName());
continue;

Check warning on line 498 in src/main/java/com/microsoft/azure/vmagent/AzureVMAgentCleanUpTask.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 487-498 are not covered by tests
}

// Even if offline, a machine that has been temporarily marked offline
// should stay (this could be for investigation).
if (azureComputer.isSetOfflineByUser()) {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/microsoft/azure/vmagent/AzureVMCloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -657,10 +657,8 @@ public Collection<PlannedNode> provision(CloudState cloudState, int workLoad) {
if (numberOfAgents == 0) {
break;
}
if (agentComputer instanceof AzureVMComputer && agentComputer.isOffline()) {
final AzureVMComputer azureComputer = (AzureVMComputer) agentComputer;
if (agentComputer instanceof AzureVMComputer azureComputer && agentComputer.isOffline()) {

Check warning on line 660 in src/main/java/com/microsoft/azure/vmagent/AzureVMCloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 660 is not covered by tests
final AzureVMAgent agentNode = azureComputer.getNode();

if (agentNode != null && isNodeEligibleForReuse(agentNode, template)) {
LOGGER.log(Level.FINE, "Agent computer eligible for reuse {0}", agentComputer.getName());

Expand Down

0 comments on commit 7288787

Please sign in to comment.