Skip to content

Commit

Permalink
Merge pull request #26 from ikedam/feature/JENKINS-21241_CountVariable
Browse files Browse the repository at this point in the history
[JENKINS-21241] NAGINATOR_COUNT, NAGINATOR_MAXCOUNT, NAGINATOR_BUILD_NUMBER
  • Loading branch information
ikedam committed Feb 8, 2016
2 parents cf96b13 + cc1b67d commit 1abccf7
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 15 deletions.
@@ -1,13 +1,17 @@
package com.chikli.hudson.plugin.naginator;

import hudson.model.Action;
import javax.annotation.CheckForNull;

import hudson.model.BuildBadgeAction;
import hudson.model.Run;

/**
* @author: <a hef="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
*/
public class NaginatorAction implements BuildBadgeAction {
private final int retryCount;
private final int maxRetryCount;
private final Integer parentBuildNumber;

/**
* @deprecated use {@link NaginatorAction#NaginatorAction(int)}
Expand All @@ -20,9 +24,23 @@ public NaginatorAction() {
/**
* @param retryCount the number of retry this build is rescheduled for.
* @since 1.16
* @deprecated use {@link #NaginatorAction(Run, int, int)} instead.
*/
@Deprecated
public NaginatorAction(int retryCount) {
this(null, retryCount, 0);
}

/**
* @param parentBuild the build to retry.
* @param retryCount the number of retry this build is rescheduled for.
* @param maxRetryCount the maximum number to retry. Can be 0 for indeterminable cases.
* @since 1.17
*/
public NaginatorAction(@CheckForNull Run<?, ?> parentBuild, int retryCount, int maxRetryCount) {
this.parentBuildNumber = (parentBuild != null) ? parentBuild.getNumber() : null;
this.retryCount = retryCount;
this.maxRetryCount = maxRetryCount;
}

public String getIconFileName() {
Expand All @@ -48,4 +66,31 @@ public String getUrlName() {
public int getRetryCount() {
return retryCount;
}

/**
* Returns the maximum number to reschedule.
* This may be <code>0</code> for builds rescheduled with
* older versions of naginator-plugin
* for cases that the build is rescheduled manually,
* or for cases the maximum number is indeterminable.
*
* @return the maximum number to retry.
* @since 1.17
*/
public int getMaxRetryCount() {
return maxRetryCount;
}

/**
* Returns the maximum number to reschedule.
* This may be <code>null</code> for builds rescheduled with
* older versions of naginator-plugin
*
* @return the build number of the build to reschedule.
* @since 1.17
*/
@CheckForNull
public Integer getParentBuildNumber() {
return parentBuildNumber;
}
}
@@ -1,16 +1,20 @@
package com.chikli.hudson.plugin.naginator;

import hudson.Launcher;
import hudson.matrix.Combination;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixRun;
import hudson.model.*;
import hudson.model.Run.RunnerAbortedException;
import hudson.model.listeners.RunListener;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Nonnull;

Expand Down Expand Up @@ -59,14 +63,38 @@ public void onCompleted(AbstractBuild<?, ?> build, @Nonnull TaskListener listene

if (!combsToRerun.isEmpty()) {
LOGGER.log(Level.FINE, "schedule matrix rebuild");
scheduleMatrixBuild(build, combsToRerun, n, retryCount + 1);
scheduleMatrixBuild(build, combsToRerun, n, retryCount + 1, action.getMaxSchedule());
} else {
scheduleBuild(build, n, retryCount + 1);
scheduleBuild(build, n, retryCount + 1, action.getMaxSchedule());
}
}
}
}

/**
* {@inheritDoc}
*/
@Override
public Environment setUpEnvironment(@SuppressWarnings("rawtypes") AbstractBuild build, Launcher launcher, BuildListener listener)
throws IOException, InterruptedException, RunnerAbortedException
{
final NaginatorAction action = build.getRootBuild().getAction(NaginatorAction.class);
if (action == null) {
return null;
}

return new Environment() {
@Override
public void buildEnvVars(Map<String, String> env) {
env.put("NAGINATOR_COUNT", Integer.toString(action.getRetryCount()));
env.put("NAGINATOR_MAXCOUNT", Integer.toString(action.getMaxRetryCount()));
if (action.getParentBuildNumber() != null) {
env.put("NAGINATOR_BUILD_NUMBER", action.getParentBuildNumber().toString());
}
}
};
}

/**
* @deprecated use {@link NaginatorScheduleAction#shouldSchedule(Run, TaskListener, int)}
* to control scheduling.
Expand Down Expand Up @@ -111,15 +139,15 @@ public static int calculateRetryCount(@Nonnull Run<?, ?> r) {
*/
@Deprecated
public boolean scheduleMatrixBuild(AbstractBuild<?, ?> build, List<Combination> combinations, int n) {
return scheduleMatrixBuild(build, combinations, n, NaginatorListener.calculateRetryCount(build));
return scheduleMatrixBuild(build, combinations, n, NaginatorListener.calculateRetryCount(build), 0);
}

private boolean scheduleMatrixBuild(AbstractBuild<?, ?> build, List<Combination> combinations, int n, int retryCount) {
NaginatorMatrixAction nma = new NaginatorMatrixAction(retryCount);
private boolean scheduleMatrixBuild(AbstractBuild<?, ?> build, List<Combination> combinations, int delay, int retryCount, int maxRetryCount) {
NaginatorMatrixAction nma = new NaginatorMatrixAction(build, retryCount, maxRetryCount);
for (Combination c : combinations) {
nma.addCombinationToRerun(c);
}
return NaginatorRetryAction.scheduleBuild(build, n, nma);
return NaginatorRetryAction.scheduleBuild(build, delay, nma);
}

/**
Expand All @@ -129,11 +157,11 @@ private boolean scheduleMatrixBuild(AbstractBuild<?, ?> build, List<Combination>
*/
@Deprecated
public boolean scheduleBuild(AbstractBuild<?, ?> build, int n) {
return scheduleBuild(build, n, NaginatorListener.calculateRetryCount(build));
return scheduleBuild(build, n, NaginatorListener.calculateRetryCount(build), 0);
}

private boolean scheduleBuild(AbstractBuild<?, ?> build, int n, int retryCount) {
return NaginatorRetryAction.scheduleBuild(build, n, retryCount);
private boolean scheduleBuild(AbstractBuild<?, ?> build, int n, int retryCount, int maxRetryCount) {
return NaginatorRetryAction.scheduleBuild(build, n, retryCount, maxRetryCount);
}

private static final Logger LOGGER = Logger.getLogger(NaginatorListener.class.getName());
Expand Down
@@ -1,9 +1,13 @@
package com.chikli.hudson.plugin.naginator;

import hudson.matrix.Combination;
import hudson.model.Run;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.CheckForNull;

/**
* This is an extention for the NaginatorAction class which used to store the
* combinations to rerun.
Expand All @@ -23,9 +27,21 @@ public NaginatorMatrixAction() {
/**
* @param retryCount the number of retry this build is rescheduled for.
* @since 1.16
* @deprecated use {@link #NaginatorMatrixAction(Run, int, int)} instead.
*/
@Deprecated
public NaginatorMatrixAction(int retryCount) {
super(retryCount);
this(null, retryCount, 0);
}

/**
* @param parentBuild the build to be rescheduled.
* @param retryCount the number of retry this build is rescheduled for.
* @param maxRetryCount the maximum number to retry. Can be 0 for indeterminable cases.
* @since 1.17
*/
public NaginatorMatrixAction(@CheckForNull Run<?, ?> parentBuild, int retryCount, int maxRetryCount) {
super(parentBuild, retryCount, maxRetryCount);
this.combsToRerun = new ArrayList<Combination>();
}

Expand Down
Expand Up @@ -49,12 +49,12 @@ public String getUrlName() {

public void doIndex(StaplerResponse res, @AncestorInPath AbstractBuild build) throws IOException {
build.getACL().checkPermission(Item.BUILD);
NaginatorRetryAction.scheduleBuild(build, 0, NaginatorListener.calculateRetryCount(build));
NaginatorRetryAction.scheduleBuild(build, 0, NaginatorListener.calculateRetryCount(build), 0);
res.sendRedirect2(build.getUpUrl());
}

static boolean scheduleBuild(final AbstractBuild<?, ?> build, final int delay, int retryCount) {
return scheduleBuild(build, delay, new NaginatorAction(retryCount));
static boolean scheduleBuild(final AbstractBuild<?, ?> build, final int delay, int retryCount, int maxRetryCount) {
return scheduleBuild(build, delay, new NaginatorAction(build, retryCount, maxRetryCount));
}

static boolean scheduleBuild(final AbstractBuild<?, ?> build, final int delay, final NaginatorAction action) {
Expand Down
Expand Up @@ -3,4 +3,14 @@
<p>
Note: immediate rescheduling of failed builds, combined with publishers such as e-mail or IM, will flood the
recipients. Take care to configure a suitable delay between builds or limit the maximum retries.
</div>
<p>
Following variables are defined for rescheduled builds.
<dl>
<dt>NAGINATOR_COUNT</dt>
<dd>How many times the build was rescheduled.</dd>
<dt>NAGINATOR_MAXCOUNT</dt>
<dd>How many times the build can be rescheduled. This can be 0 if manually rescheduled.</dd>
<dt>NAGINATOR_BUILD_NUMBER</dt>
<dd>The build number of the failed build causing the reschedule.</dd>
</dl>
</div>

0 comments on commit 1abccf7

Please sign in to comment.