Skip to content

Commit

Permalink
JENKINS-12092 Block job by category
Browse files Browse the repository at this point in the history
  • Loading branch information
fluffy88 committed Jan 26, 2015
1 parent 880acd2 commit fef46c5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,30 @@ public class ThrottleJobProperty extends JobProperty<AbstractProject<?,?>> {
private String throttleOption;
private transient boolean throttleConfiguration;
private @CheckForNull ThrottleMatrixProjectOptions matrixOptions;
private boolean blockedByCategory;

/**
* Store a config version so we're able to migrate config on various
* functionality upgrades.
*/
private Long configVersion;

@DataBoundConstructor
public ThrottleJobProperty(Integer maxConcurrentPerNode,
Integer maxConcurrentTotal,
List<String> categories,
boolean throttleEnabled,
String throttleOption,
@CheckForNull ThrottleMatrixProjectOptions matrixOptions
@CheckForNull ThrottleMatrixProjectOptions matrixOptions,
boolean blockedByCategory
) {
this.maxConcurrentPerNode = maxConcurrentPerNode == null ? 0 : maxConcurrentPerNode;
this.maxConcurrentTotal = maxConcurrentTotal == null ? 0 : maxConcurrentTotal;
this.categories = categories;
this.throttleEnabled = throttleEnabled;
this.throttleOption = throttleOption;
this.matrixOptions = matrixOptions;
this.blockedByCategory = blockedByCategory;
}


Expand Down Expand Up @@ -131,7 +134,11 @@ public String getThrottleOption() {
public List<String> getCategories() {
return categories;
}


public boolean isBlockedByCategory() {
return blockedByCategory;
}

public Integer getMaxConcurrentPerNode() {
if (maxConcurrentPerNode == null)
maxConcurrentPerNode = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hudson.plugins.throttleconcurrents;


import hudson.Extension;
import hudson.matrix.MatrixConfiguration;
import hudson.matrix.MatrixProject;
Expand All @@ -18,6 +19,7 @@
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

Expand Down Expand Up @@ -153,6 +155,11 @@ else if (tjp.getThrottleOption().equals("category")) {

// Double check category itself isn't null
if (category != null) {
// Check if this job is blocked by category
if (isBlockedByCategory(task, categoryProjects)) {
return CauseOfBlockage.fromMessage(Messages._ThrottleQueueTaskDispatcher_BlockedByCategory(catNm));
}

if (category.getMaxConcurrentTotal().intValue() > 0) {
int maxConcurrentTotal = category.getMaxConcurrentTotal().intValue();
int totalRunCount = 0;
Expand All @@ -178,6 +185,35 @@ else if (tjp.getThrottleOption().equals("category")) {
return null;
}

private boolean isBlockedByCategory(Task task, List<AbstractProject<?, ?>> categoryProjects) {
ThrottleJobProperty buildProperty = getThrottleJobProperty(task);
if (buildProperty.isBlockedByCategory()) {
return areOtherProjectsInCategoryBuilding(task, categoryProjects);
}
return false;
}

private boolean areOtherProjectsInCategoryBuilding(Task task, List<AbstractProject<?, ?>> categoryProjects) {
for (AbstractProject<?, ?> catProj : categoryProjects) {
if (!task.equals(catProj)) {
if (isBlockingProjectBuilding(catProj)) {
return true;
}
}
}
return false;
}

private boolean isBlockingProjectBuilding(AbstractProject<?, ?> catProj) {
ThrottleJobProperty catProjThrottleProperty = catProj.getProperty(ThrottleJobProperty.class);
if (!catProjThrottleProperty.isBlockedByCategory()) {
if (catProj.isBuilding() || catProj.isInQueue()) {
return true;
}
}
return false;
}

@CheckForNull
private ThrottleJobProperty getThrottleJobProperty(Task task) {
if (task instanceof AbstractProject) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ThrottleQueueTaskDispatcher.MaxCapacityOnNode=Already running {0} builds on node
ThrottleQueueTaskDispatcher.MaxCapacityTotal=Already running {0} builds across all nodes
ThrottleQueueTaskDispatcher.BuildPending=A build is pending launch
ThrottleQueueTaskDispatcher.BlockedByCategory=Build is blocked by other jobs in category {0}

ThrottleMatrixProjectOptions.DisplayName=Additional options for Matrix projects
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<st:nbsp/>
</j:forEach>
</f:entry>
<f:entry title="${%Let this Job be blocked by selected Categories}">
<f:checkbox name="blockedByCategory" checked="${instance.blockedByCategory}" />
</f:entry>
</j:if>
<!--Specific options for Matrix projects-->
<j:if test="${descriptor.isMatrixProject(it)}">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public void testGetCategoryProjects() throws Exception {
String alpha = "alpha", beta = "beta", gamma = "gamma"; // category names
FreeStyleProject p1 = createFreeStyleProject("p1");
FreeStyleProject p2 = createFreeStyleProject("p2");
p2.addProperty(new ThrottleJobProperty(1, 1, Arrays.asList(alpha), false, THROTTLE_OPTION_CATEGORY, ThrottleMatrixProjectOptions.DEFAULT));
p2.addProperty(new ThrottleJobProperty(1, 1, Arrays.asList(alpha), false, THROTTLE_OPTION_CATEGORY, ThrottleMatrixProjectOptions.DEFAULT, false));
FreeStyleProject p3 = createFreeStyleProject("p3");
p3.addProperty(new ThrottleJobProperty(1, 1, Arrays.asList(alpha, beta), true, THROTTLE_OPTION_CATEGORY, ThrottleMatrixProjectOptions.DEFAULT));
p3.addProperty(new ThrottleJobProperty(1, 1, Arrays.asList(alpha, beta), true, THROTTLE_OPTION_CATEGORY, ThrottleMatrixProjectOptions.DEFAULT, false));
FreeStyleProject p4 = createFreeStyleProject("p4");
p4.addProperty(new ThrottleJobProperty(1, 1, Arrays.asList(beta, gamma), true, THROTTLE_OPTION_CATEGORY, ThrottleMatrixProjectOptions.DEFAULT));
p4.addProperty(new ThrottleJobProperty(1, 1, Arrays.asList(beta, gamma), true, THROTTLE_OPTION_CATEGORY, ThrottleMatrixProjectOptions.DEFAULT, false));
// TODO when core dep ≥1.480.3, add cloudbees-folder as a test dependency so we can check jobs inside folders
assertProjects(alpha, p3);
assertProjects(beta, p3, p4);
Expand Down

0 comments on commit fef46c5

Please sign in to comment.