Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JENKINS-23368 - Enforce start interval/delay between running builds. #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class ThrottleJobProperty extends JobProperty<AbstractProject<?,?>> {

private Integer maxConcurrentPerNode;
private Integer maxConcurrentTotal;
private Long startInterval;
private List<String> categories;
private boolean throttleEnabled;
private String throttleOption;
Expand All @@ -57,10 +58,12 @@ public ThrottleJobProperty(Integer maxConcurrentPerNode,
List<String> categories,
boolean throttleEnabled,
String throttleOption,
@CheckForNull ThrottleMatrixProjectOptions matrixOptions
@CheckForNull ThrottleMatrixProjectOptions matrixOptions,
Long startInterval
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An old constructor is also required for the backward compatibility

) {
this.maxConcurrentPerNode = maxConcurrentPerNode == null ? 0 : maxConcurrentPerNode;
this.maxConcurrentTotal = maxConcurrentTotal == null ? 0 : maxConcurrentTotal;
this.startInterval = startInterval == null ? 0L : startInterval;
this.categories = categories;
this.throttleEnabled = throttleEnabled;
this.throttleOption = throttleOption;
Expand Down Expand Up @@ -100,6 +103,10 @@ public Object readResolve() {
matrixOptions = new ThrottleMatrixProjectOptions(false, true);
}

if (startInterval == null) {
startInterval = 0L;
}

return this;
}

Expand Down Expand Up @@ -146,6 +153,13 @@ public Integer getMaxConcurrentTotal() {
return maxConcurrentTotal;
}

public Long getStartInterval() {
if (startInterval == null)
startInterval = 0L;

return startInterval;
}

@CheckForNull
public ThrottleMatrixProjectOptions getMatrixOptions() {
return matrixOptions;
Expand Down Expand Up @@ -276,6 +290,9 @@ public FormValidation doCheckMaxConcurrentTotal(@QueryParameter String value) {
return checkNullOrInt(value);
}

public FormValidation doCheckStartInterval(@QueryParameter String value) {
return checkNullOrInt(value);
}

public ThrottleCategory getCategoryByName(String categoryName) {
ThrottleCategory category = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import hudson.model.Executor;
import hudson.model.Hudson;
import hudson.model.Node;
import hudson.model.Run;
import hudson.model.Queue;
import hudson.model.Queue.Task;
import hudson.model.labels.LabelAtom;
Expand Down Expand Up @@ -139,6 +140,26 @@ public CauseOfBlockage canRun(Task task, ThrottleJobProperty tjp) {
return CauseOfBlockage.fromMessage(Messages._ThrottleQueueTaskDispatcher_MaxCapacityTotal(totalRunCount));
}
}

// check start interval between runs
if (tjp.getStartInterval().longValue() > 0) {
if (task instanceof AbstractProject) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may support other Job types, needs to be checked

AbstractProject<?,?> p = (AbstractProject<?,?>) task;
Run<?,?> lastBuild = p.getLastBuild();
if (null != lastBuild && lastBuild.isBuilding()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lastBuild is not helpful for concurrent runs, because there may be other running builds. 🐜


// Probably better to use lastBuild.getStartTimeInMillis() which was introduced in Jenkins 1.494.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The core dependency has been bumped, hence it can be updated now

// We are building against 1.424 so getTimeInMillis() is an OK approximation
long timeSinceLastBuildStartedInSeconds = (System.currentTimeMillis() - lastBuild.getTimeInMillis()) / 1000;
long remainingIntervalSeconds = tjp.getStartInterval().longValue() - timeSinceLastBuildStartedInSeconds;
if(remainingIntervalSeconds > 0) {
long minutes = remainingIntervalSeconds / 60;
long seconds = remainingIntervalSeconds % 60;
return CauseOfBlockage.fromMessage(Messages._ThrottleQueueTaskDispatcher_StartIntervalLimit(minutes, seconds));
}
}
}
}
}
// If the project is in one or more categories...
else if (tjp.getThrottleOption().equals("category")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ThrottleQueueTaskDispatcher.MaxCapacityOnNode=Already running {0} builds on node
ThrottleQueueTaskDispatcher.MaxCapacityTotal=Already running {0} builds across all nodes
ThrottleQueueTaskDispatcher.BuildPending=A build is pending launch

ThrottleMatrixProjectOptions.DisplayName=Additional options for Matrix projects
ThrottleQueueTaskDispatcher.StartIntervalLimit=Waiting for required start interval. [{0}m {1}s]
ThrottleMatrixProjectOptions.DisplayName=Additional options for Matrix projects
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
field="maxConcurrentPerNode">
<f:textbox />
</f:entry>
<f:entry title="${%Minimum Start Interval between Builds in seconds}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about introducing an "Advanced" section for such option?

field="startInterval">
<f:textbox />
</f:entry>
<j:if test="${!empty(descriptor.categories)}">
<f:entry title="${%Multi-Project Throttle Category}">
<j:forEach var="cat" items="${descriptor.categories}">
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, 0L));
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, 0L));
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, 0L));
// 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