Skip to content

Commit

Permalink
Add option for single-use agents in pool retention strategy (#491)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Jacomb <timjacomb1@gmail.com>
Co-authored-by: Tim Jacomb <21194782+timja@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 2, 2024
1 parent e1a9be8 commit 76424ae
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,34 @@
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.model.Executor;
import hudson.model.ExecutorListener;
import hudson.model.Computer;
import hudson.model.Queue;
import hudson.model.Descriptor;
import hudson.model.DescriptorVisibilityFilter;
import hudson.slaves.Cloud;
import hudson.slaves.AbstractCloudComputer;
import hudson.slaves.RetentionStrategy;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AzureVMCloudPoolRetentionStrategy extends AzureVMCloudBaseRetentionStrategy {
public class AzureVMCloudPoolRetentionStrategy extends AzureVMCloudBaseRetentionStrategy implements ExecutorListener {
private static final long serialVersionUID = 1577788691L;

private final long retentionMillis;

private final int poolSize;

private boolean singleUseAgents;

private static final long IDLE_LIMIT_MILLIS = TimeUnit.MINUTES.toMillis(1);

private static final Logger LOGGER = Logger.getLogger(AzureVMManagementServiceDelegate.class.getName());
Expand Down Expand Up @@ -161,6 +168,58 @@ public int getPoolSize() {
return poolSize;
}

@Override
public void taskAccepted(Executor executor, Queue.Task task) {

}

@Override
public void taskCompleted(Executor executor, Queue.Task task, long durationMS) {
done(executor);
}

@Override
public void taskCompletedWithProblems(Executor executor, Queue.Task task, long durationMS, Throwable problems) {
done(executor);
}

public void done(Executor executor) {
final AbstractCloudComputer<?> computer = (AbstractCloudComputer) executor.getOwner();
if (!(computer instanceof AzureVMComputer)) {
return;
}
done((AzureVMComputer) computer);
}
public void done(AzureVMComputer computer) {
if (!isSingleUseAgents()) {
return;
}

final AzureVMAgent agent = computer.getNode();
if (agent == null) {
return;
}

computer.setAcceptingTasks(false);
if (agent.isShutdownOnIdle()) {
LOGGER.log(Level.FINE, "Tagging VM to shutdown when idle: {0}",
computer.getName());
agent.setCleanUpAction(CleanUpAction.SHUTDOWN, Messages._Build_Action_Shutdown_Agent());
} else {
LOGGER.log(Level.FINE, "Tagging VM to delete when idle: {0}", computer.getName());
agent.setCleanUpAction(CleanUpAction.DELETE, Messages._Build_Action_Delete_Agent());
}
}

public boolean isSingleUseAgents() {
return this.singleUseAgents;
}

@DataBoundSetter
public void setSingleUseAgents(boolean singleUseAgents) {
this.singleUseAgents = singleUseAgents;
}

Check warning on line 221 in src/main/java/com/microsoft/azure/vmagent/AzureVMCloudPoolRetentionStrategy.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 174-221 are not covered by tests

@Override
public void start(AzureVMComputer azureComputer) {
//TODO: check when this method is getting called and add code accordingly
Expand All @@ -178,7 +237,6 @@ public DescriptorImpl getDescriptor() {
public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();

public static class DescriptorImpl extends Descriptor<RetentionStrategy<?>> {

@Override @NonNull
public String getDisplayName() {
return "Azure VM Pool Retention Strategy";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,12 @@ public T addNewIdleRetentionStrategy(String retentionTime) {
return (T) this;
}

public T addNewPoolRetentionStrategy(String retentionTime, String poolSize) {
this.retentionStrategy = new AzureVMCloudPoolRetentionStrategy(Integer.parseInt(retentionTime),
public T addNewPoolRetentionStrategy(String retentionTime, String poolSize, boolean singleUseAgents) {
AzureVMCloudPoolRetentionStrategy retentionStrategy1 =
new AzureVMCloudPoolRetentionStrategy(Integer.parseInt(retentionTime),
Integer.parseInt(poolSize));
retentionStrategy1.setSingleUseAgents(singleUseAgents);
this.retentionStrategy = retentionStrategy1;

Check warning on line 171 in src/main/java/com/microsoft/azure/vmagent/builders/AzureVMTemplateFluent.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 167-171 are not covered by tests
return (T) this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
<f:textbox default="3" />
</f:entry>

<f:entry title="${%Single_Use_Agents}" field="singleUseAgents">
<f:checkbox/>
</f:entry>

</j:jelly>
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
RetentionTimeInHours=Retention Time In Hour
Pool_Size=Pool Size
Single_Use_Agents=Single Use Agents
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
If enabled, agents are deleted after a single job is run. It is suggested to set retention time to 0 if this is enabled.
</div>

0 comments on commit 76424ae

Please sign in to comment.