Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<properties>
<revision>0.10.1</revision>
<changelist>-SNAPSHOT</changelist>
<jenkins.version>2.375</jenkins.version>
<jenkins.version>2.407</jenkins.version>
<json-path-assert.version>2.8.0</json-path-assert.version>
</properties>

Expand Down
27 changes: 26 additions & 1 deletion src/main/java/org/jenkinsci/plugins/nomad/NomadCloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,14 @@ public Collection<NodeProvisioner.PlannedNode> provision(Label label, int excess
if (prune)
pruneOrphanedWorkers(template);

int existingNodes = 0;
// Let's avoid an useless request to nomad api if no MaxConcurrentJobs is configured
if(template.getMaxConcurrentJobs() >= 0) {
existingNodes = this.nomad.getRunningWorkers(template.getPrefix()).length;
}

try {
while (excessWorkload > 0) {
while (excessWorkload > 0 && checkExcessJobs(template,existingNodes,nodes.size())) {
LOGGER.log(Level.INFO, "Excess workload of " + excessWorkload + ", provisioning new Jenkins worker on Nomad cluster");

final String workerName = template.createWorkerName();
Expand All @@ -156,6 +162,25 @@ public Collection<NodeProvisioner.PlannedNode> provision(Label label, int excess
return Collections.emptyList();
}

/**
* Determine if we reached the maximum number of job we can create for a specific template.
* If the maximum configured is negative, we consider this configuration as unlimited.
* @param template
* @param existingNodes
* @param created
* @return false if the template maximum configured jobs isn't reached, otherwise, return true
*/
private boolean checkExcessJobs(NomadWorkerTemplate template,int existingNodes, int created) {
int maxAllowed = template.getMaxConcurrentJobs();
int maxAllowedLeft = maxAllowed - existingNodes - created;

if (maxAllowed >= 0 && created >= maxAllowedLeft) {
LOGGER.log((Level.INFO), "Maximum jobs for template prefix: "+template.getPrefix()+" excedeed | Maximum: "+maxAllowed);
return false;
}
return true;
}


/**
* Determines if some nomad worker needs to be stopped.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class NomadWorkerTemplate implements Describable<NomadWorkerTemplate> {

// persistent fields
private final String prefix;
private final int maxConcurrentJobs;
private final int idleTerminationInMinutes;
private final boolean reusable;
private final int numExecutors;
Expand Down Expand Up @@ -98,13 +99,15 @@ public class NomadWorkerTemplate implements Describable<NomadWorkerTemplate> {
public NomadWorkerTemplate(
String prefix,
String labels,
int maxConcurrentJobs,
int idleTerminationInMinutes,
boolean reusable,
int numExecutors,
String remoteFs,
String jobTemplate
) {
this.prefix = prefix.isEmpty() ? SLAVE_PREFIX : prefix;
this.maxConcurrentJobs = maxConcurrentJobs;
this.idleTerminationInMinutes = idleTerminationInMinutes;
this.reusable = reusable;
this.numExecutors = numExecutors;
Expand All @@ -127,6 +130,10 @@ public String getPrefix() {
return prefix;
}

public int getMaxConcurrentJobs() {
return maxConcurrentJobs;
}

public int getIdleTerminationInMinutes() {
return idleTerminationInMinutes;
}
Expand Down Expand Up @@ -201,6 +208,7 @@ public FormValidation doValidation(
NomadWorkerTemplate template = new NomadWorkerTemplate(
"validate-template",
null,
-1,
0,
false,
1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<f:textbox />
</f:entry>

<f:entry title="Maximum Number of concurrent Job (-1 for unmimited)" field="maxConcurrentJobs">
<f:number default="-1" />
</f:entry>

<f:entry title="Idle termination time" field="idleTerminationInMinutes">
<f:number default="10" />
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ private NomadWorkerTemplate createTemplate(String labels) {
return new NomadWorkerTemplate(
"jenkins",
labels,
-1,
1,
true,
1,
Expand Down