-
Notifications
You must be signed in to change notification settings - Fork 107
/
PeriodicFolderTrigger.java
250 lines (232 loc) · 8.46 KB
/
PeriodicFolderTrigger.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* The MIT License
*
* Copyright (c) 2013, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.cloudbees.hudson.plugins.folder.computed;
import antlr.ANTLRException;
import hudson.Extension;
import hudson.model.Cause;
import hudson.model.Item;
import hudson.model.Items;
import hudson.triggers.TimerTrigger;
import hudson.triggers.Trigger;
import hudson.triggers.TriggerDescriptor;
import hudson.util.ListBoxModel;
import hudson.util.TimeUnit2;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
/**
* A {@link Trigger} for {@link ComputedFolder}s that will only trigger a recomputation if none has been
* triggered within a specified timeframe.
*
* @author Stephen Connolly
*/
public class PeriodicFolderTrigger extends Trigger<ComputedFolder<?>> {
private static final Logger LOGGER = Logger.getLogger(PeriodicFolderTrigger.class.getName());
/**
* The interval between successive indexings.
*/
private final long interval;
/**
* Timestamp when we last {@link #run}.
* Normally we rely on {@link FolderComputation#getTimestamp} but there is a slight delay
* between {@link ComputedFolder#scheduleBuild(int, Cause)} and {@link ComputedFolder#createExecutable}.
*/
private transient long lastTriggered; // could be volatile or AtomicLong, but FolderCron will not run >1 concurrently anyway
/**
* Constructor.
*
* @param interval the interval.
* @throws ANTLRException impossible but we cannot suppress it
*/
@DataBoundConstructor
public PeriodicFolderTrigger(String interval) throws ANTLRException {
super(toCrontab(interval));
long intervalMillis = toIntervalMillis(interval);
this.interval = intervalMillis;
}
/**
* Turns an interval into a suitable crontab.
*
* @param interval the interval.
* @return the crontab.
*/
private static String toCrontab(String interval) {
long millis = toIntervalMillis(interval);
if (millis < TimeUnit2.MINUTES.toMillis(5)) {
return "* * * * *";
}
if (millis < TimeUnit2.MINUTES.toMillis(10)) {
return "*/12 * * * *";
}
if (millis < TimeUnit2.MINUTES.toMillis(30)) {
return "*/6 * * * *";
}
if (millis < TimeUnit2.HOURS.toMillis(1)) {
return "*/2 * * * *";
}
if (millis < TimeUnit2.HOURS.toMillis(8)) {
return "H * * * *";
}
return "H H * * *";
}
/**
* Turns an interval into milliseconds./
*
* @param interval the interval.
* @return the milliseconds.
*/
private static long toIntervalMillis(String interval) {
TimeUnit2 units = TimeUnit2.MINUTES;
interval = interval.toLowerCase();
if (interval.endsWith("h")) {
units = TimeUnit2.HOURS;
interval = StringUtils.removeEnd(interval, "h");
}
if (interval.endsWith("m")) {
interval = StringUtils.removeEnd(interval, "m");
} else if (interval.endsWith("d")) {
units = TimeUnit2.DAYS;
interval = StringUtils.removeEnd(interval, "d");
} else if (interval.endsWith("ms")) {
units = TimeUnit2.SECONDS;
interval = StringUtils.removeEnd(interval, "ms");
} else if (interval.endsWith("s")) {
units = TimeUnit2.SECONDS;
interval = StringUtils.removeEnd(interval, "s");
}
long value = 0;
try {
value = Long.parseLong(interval);
} catch (NumberFormatException e) {
value = 1;
}
return Math.min(TimeUnit2.DAYS.toMillis(30),
Math.max(TimeUnit2.MINUTES.toMillis(1), units.toMillis(value)));
}
/**
* Returns the number of milliseconds between indexing.
*
* @return the number of milliseconds between indexing.
*/
@SuppressWarnings("unused") // used by Jelly EL
public long getIntervalMillis() {
return interval;
}
/**
* Returns the interval between indexing.
*
* @return the interval between indexing.
*/
@SuppressWarnings("unused") // used by Jelly EL
public String getInterval() {
if (interval < TimeUnit2.SECONDS.toMillis(1)) {
return Long.toString(interval) + "ms";
}
if (interval < TimeUnit2.MINUTES.toMillis(1)) {
return Long.toString(TimeUnit2.MILLISECONDS.toSeconds(interval)) + "s";
}
if (interval < TimeUnit2.HOURS.toMillis(1)) {
return Long.toString(TimeUnit2.MILLISECONDS.toMinutes(interval)) + "m";
}
if (interval < TimeUnit2.DAYS.toMillis(1)) {
return Long.toString(TimeUnit2.MILLISECONDS.toHours(interval)) + "h";
}
return Long.toString(TimeUnit2.MILLISECONDS.toDays(interval)) + "d";
}
/**
* {@inheritDoc}
*/
@Override
public void run() {
long now = System.currentTimeMillis();
FolderComputation<?> computation = job.getComputation();
if (computation != null) {
long delay = now - computation.getTimestamp().getTimeInMillis();
if (delay < interval) {
LOGGER.log(Level.FINE, "Too early to reschedule {0} based on last computation", job);
return;
}
}
if (now - lastTriggered < interval) {
LOGGER.log(Level.FINE, "Too early to reschedule {0} based on last triggering", job);
return;
}
if (job.scheduleBuild(0, new TimerTrigger.TimerTriggerCause())) {
lastTriggered = now;
} else {
LOGGER.log(Level.WARNING, "Queue refused to schedule {0}", job);
}
}
/**
* Our {@link hudson.model.Descriptor}
*/
@Extension
@SuppressWarnings("unused") // instantiated by Jenkins
public static class DescriptorImpl extends TriggerDescriptor {
/**
* {@inheritDoc}
*/
public boolean isApplicable(Item item) {
return item instanceof ComputedFolder;
}
/**
* {@inheritDoc}
*/
public String getDisplayName() {
return Messages.PeriodicFolderTrigger_DisplayName();
}
/**
* Fills the interval list box.
*
* @return the interval list box.
*/
@SuppressWarnings("unused") // used by Jelly
public ListBoxModel doFillIntervalItems() {
ListBoxModel model = new ListBoxModel();
model.add("1 minute", "1m");
model.add("2 minutes", "2m");
model.add("5 minutes", "5m");
model.add("10 minutes", "10m");
model.add("15 minutes", "15m");
model.add("20 minutes", "20m");
model.add("25 minutes", "25m");
model.add("30 minutes", "30m");
model.add("1 hour", "1h");
model.add("2 hours", "2h");
model.add("4 hours", "4h");
model.add("8 hours", "8h");
model.add("12 hours", "12h");
model.add("1 day", "1d");
model.add("2 days", "2d");
model.add("1 week", "7d");
model.add("2 weeks", "14d");
model.add("4 weeks", "28d");
return model;
}
static {
Items.XSTREAM2.addCompatibilityAlias("jenkins.branch.IndexAtLeastTrigger", PeriodicFolderTrigger.class);
}
}
}