Skip to content

Commit

Permalink
[FIXED JENKINS-17626] Stores retry counts in NaginatorAction and resc…
Browse files Browse the repository at this point in the history
…hedules build exact configured times.
  • Loading branch information
ikedam committed Sep 5, 2015
1 parent d692788 commit eb87736
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
Expand Up @@ -7,6 +7,23 @@
* @author: <a hef="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
*/
public class NaginatorAction implements BuildBadgeAction {
private final int retryCount;

/**
* @deprecated use {@link NaginatorAction#NaginatorAction(int)}
*/
@Deprecated
public NaginatorAction() {
this(0);
}

/**
* @param retryCount the number of retry this build is rescheduled for.
* @since 1.16
*/
public NaginatorAction(int retryCount) {
this.retryCount = retryCount;
}

public String getIconFileName() {
return null;
Expand All @@ -19,4 +36,16 @@ public String getDisplayName() {
public String getUrlName() {
return null;
}

/**
* Returns the number of retry this build is rescheduled for.
* This may be <code>0</code> for builds rescheduled with
* older versions of naginator-plugin.
*
* @return the number of retry this build is rescheduled for.
* @since 1.16
*/
public int getRetryCount() {
return retryCount;
}
}
Expand Up @@ -59,9 +59,9 @@ public void onCompleted(AbstractBuild<?, ?> build, @Nonnull TaskListener listene

if (!combsToRerun.isEmpty()) {
LOGGER.log(Level.FINE, "schedule matrix rebuild");
scheduleMatrixBuild(build, combsToRerun, n);
scheduleMatrixBuild(build, combsToRerun, n, retryCount + 1);
} else {
scheduleBuild(build, n);
scheduleBuild(build, n, retryCount + 1);
}
}
}
Expand All @@ -87,7 +87,16 @@ public boolean canSchedule(Run build, NaginatorPublisher naginator) {
return n < max;
}

private int calculateRetryCount(@Nonnull Run<?, ?> r) {
public static int calculateRetryCount(@Nonnull Run<?, ?> r) {
NaginatorAction naginatorAction = r.getAction(NaginatorAction.class);
if (naginatorAction == null) {
return 0;
}
if (naginatorAction.getRetryCount() > 0) {
return naginatorAction.getRetryCount();
}

// fallback for build made by older versions.
int n = 0;

while (r != null && r.getAction(NaginatorAction.class) != null) {
Expand All @@ -97,8 +106,16 @@ private int calculateRetryCount(@Nonnull Run<?, ?> r) {
return n;
}

/**
* @deprecated use {@link NaginatorScheduleAction} to make a build rescheduled.
*/
@Deprecated
public boolean scheduleMatrixBuild(AbstractBuild<?, ?> build, List<Combination> combinations, int n) {
NaginatorMatrixAction nma = new NaginatorMatrixAction();
return scheduleMatrixBuild(build, combinations, n, NaginatorListener.calculateRetryCount(build));
}

private boolean scheduleMatrixBuild(AbstractBuild<?, ?> build, List<Combination> combinations, int n, int retryCount) {
NaginatorMatrixAction nma = new NaginatorMatrixAction(retryCount);
for (Combination c : combinations) {
nma.addCombinationToRerun(c);
}
Expand All @@ -107,9 +124,16 @@ public boolean scheduleMatrixBuild(AbstractBuild<?, ?> build, List<Combination>

/**
* Wrapper method for mocking purposes.
*
* @deprecated use {@link NaginatorScheduleAction} to make a build rescheduled.
*/
@Deprecated
public boolean scheduleBuild(AbstractBuild<?, ?> build, int n) {
return NaginatorRetryAction.scheduleBuild(build, n);
return scheduleBuild(build, n, NaginatorListener.calculateRetryCount(build));
}

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

private static final Logger LOGGER = Logger.getLogger(NaginatorListener.class.getName());
Expand Down
Expand Up @@ -12,7 +12,20 @@
public class NaginatorMatrixAction extends NaginatorAction {
private List<Combination> combsToRerun;

/**
* @deprecated use {@link NaginatorMatrixAction#NaginatorMatrixAction(int)}.
*/
@Deprecated
public NaginatorMatrixAction() {
this(0);
}

/**
* @param retryCount the number of retry this build is rescheduled for.
* @since 1.16
*/
public NaginatorMatrixAction(int retryCount) {
super(retryCount);
this.combsToRerun = new ArrayList<Combination>();
}

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

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

static boolean scheduleBuild(final AbstractBuild<?, ?> build, final int delay) {
return scheduleBuild(build, delay, new NaginatorAction());
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, final NaginatorAction action) {
Expand Down
Expand Up @@ -55,7 +55,7 @@ public void testComputeScheduleDelayNoMax() {
private static AbstractBuild createBuild(final boolean hasNaginatorAction, final AbstractBuild previousBuild) {
final AbstractBuild build = mock(AbstractBuild.class);
when(build.getPreviousBuild()).thenReturn(previousBuild);
when(build.getAction(NaginatorAction.class)).thenReturn(hasNaginatorAction ? new NaginatorAction() : null);
when(build.getAction(NaginatorAction.class)).thenReturn(hasNaginatorAction ? new NaginatorAction(0) : null);
return build;
}
}

0 comments on commit eb87736

Please sign in to comment.