/
ProgressiveDelay.java
64 lines (50 loc) · 1.66 KB
/
ProgressiveDelay.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.chikli.hudson.plugin.naginator;
import static java.lang.Math.min;
import org.kohsuke.stapler.DataBoundConstructor;
import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.Run;
/**
* @author: <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
*/
public class ProgressiveDelay extends ScheduleDelay {
private int increment;
private int max;
@DataBoundConstructor
public ProgressiveDelay(int increment, int max) {
this.increment = increment;
this.max = max;
}
public int getIncrement() {
return increment;
}
public int getMax() {
return max;
}
@Override
public int computeScheduleDelay(AbstractBuild failedBuild) {
// if a build fails for a reason that cannot be immediately fixed,
// immediate rescheduling may cause a very tight loop.
// combined with publishers like e-mail, IM, this could flood the users.
//
// so to avoid this problem, progressively introduce delay until the next build
int n = getRetryCount(failedBuild);
int factor = (n + 1) * (n + 2) / 2;
int delay = increment * factor;
return max <= 0 ? delay : min(delay, max);
}
private int getRetryCount(AbstractBuild<?, ?> failedBuild) {
NaginatorAction action = failedBuild.getAction(NaginatorAction.class);
if (action == null) {
return 0;
}
return action.getRetryCount();
}
@Extension
public static class DescriptorImpl extends ScheduleDelayDescriptor {
@Override
public String getDisplayName() {
return "Progressive";
}
}
}