-
Notifications
You must be signed in to change notification settings - Fork 110
/
FolderComputation.java
488 lines (441 loc) · 16.2 KB
/
FolderComputation.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* The MIT License
*
* Copyright 2015 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 com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import com.jcraft.jzlib.GZIPInputStream;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.AbortException;
import hudson.BulkChange;
import hudson.Functions;
import hudson.Util;
import hudson.XmlFile;
import hudson.console.AnnotatedLargeText;
import hudson.console.PlainTextConsoleOutputStream;
import hudson.model.Actionable;
import hudson.model.BallColor;
import hudson.model.Cause;
import hudson.model.CauseAction;
import hudson.model.Executor;
import hudson.model.Items;
import hudson.model.Queue;
import hudson.model.Result;
import hudson.model.Saveable;
import hudson.model.StreamBuildListener;
import hudson.model.TaskListener;
import hudson.model.TopLevelItem;
import hudson.model.listeners.SaveableListener;
import hudson.model.queue.QueueTaskDispatcher;
import hudson.util.AlternativeUiTextProvider;
import hudson.util.AlternativeUiTextProvider.Message;
import hudson.util.HttpResponses;
import hudson.util.StreamTaskListener;
import hudson.util.io.RewindableRotatingFileOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.servlet.ServletException;
import net.jcip.annotations.GuardedBy;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.jelly.XMLOutput;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.framework.io.ByteBuffer;
import org.kohsuke.stapler.interceptor.RequirePOST;
/**
* A particular “run” of {@link ComputedFolder}.
* @since 4.11-beta-1
*/
public class FolderComputation<I extends TopLevelItem> extends Actionable implements Queue.Executable, Saveable {
/**
* Our logger.
*/
private static final Logger LOGGER = Logger.getLogger(FolderComputation.class.getName());
/** If defined, a number of backup log files to keep. */
@SuppressWarnings("FieldMayBeFinal") // let this be set dynamically by system Groovy script
private static @CheckForNull Integer BACKUP_LOG_COUNT = Integer.getInteger(FolderComputation.class.getName() + ".BACKUP_LOG_COUNT");
/** If defined, a number of kB that the event log can grow to before rotation. */
@SuppressWarnings("FieldMayBeFinal") // let this be set dynamically by system Groovy script
@Nonnull
private static int EVENT_LOG_MAX_SIZE = Math.max(1,Integer.getInteger(FolderComputation.class.getName() + ".EVENT_LOG_MAX_SIZE", 150));
/** The associated folder. */
@Nonnull
private transient final ComputedFolder<I> folder;
/** The previous run result, if any. */
@CheckForNull
private transient final Result previousResult;
/** The events output stream handler */
@CheckForNull
@GuardedBy("this")
private transient EventOutputStreams eventStreams;
/** The result of the build, if finished. */
@CheckForNull
private volatile Result result;
/** The past few durations for purposes of estimation. */
@CheckForNull
private List<Long> durations;
/** Start time. */
private long timestamp;
/** Run time. */
private long duration;
protected FolderComputation(@Nonnull ComputedFolder<I> folder, @CheckForNull FolderComputation<I> previous) {
this.folder = folder;
this.previousResult = previous == null ? null : previous.getResult();
}
/**
* {@inheritDoc}
*/
@Override
public void run() {
StreamBuildListener listener;
try {
File logFile = getLogFile();
FileUtils.forceMkdir(logFile.getParentFile());
OutputStream os;
if (BACKUP_LOG_COUNT != null) {
os = new RewindableRotatingFileOutputStream(logFile, BACKUP_LOG_COUNT);
((RewindableRotatingFileOutputStream) os).rewind();
} else {
os = new FileOutputStream(logFile);
}
listener = new StreamBuildListener(os, StandardCharsets.UTF_8);
} catch (IOException x) {
LOGGER.log(Level.WARNING, null, x);
result = Result.FAILURE;
return;
}
timestamp = System.currentTimeMillis();
// TODO print start time
listener.started(getCauses());
Result _result = Result.NOT_BUILT; // cf. isLogUpdated, do not set this.result until listener closed
try {
folder.updateChildren(listener);
_result = Result.SUCCESS;
} catch (InterruptedException x) {
LOGGER.log(Level.FINE, "recomputation of " + folder.getFullName() + " was aborted", x);
listener.getLogger().println("Aborted");
_result = Result.ABORTED;
} catch (Exception x) {
LOGGER.log(Level.FINE, "recomputation of " + folder.getFullName() + " failed", x);
if (x instanceof AbortException) {
listener.fatalError(x.getMessage());
} else {
Functions.printStackTrace(x, listener.fatalError("Failed to recompute children of " + folder.getFullDisplayName()));
}
_result = Result.FAILURE;
} finally {
duration = System.currentTimeMillis() - timestamp;
if (durations == null) {
durations = new ArrayList<Long>();
}
while (durations.size() > 32) {
durations.remove(0);
}
durations.add(duration);
listener.finished(_result);
listener.closeQuietly();
result = _result;
try {
save();
} catch (IOException x) {
LOGGER.log(Level.WARNING, null, x);
}
}
}
/**
* {@inheritDoc}
*/
@Override
public void save() throws IOException {
if (BulkChange.contains(this)) {
return;
}
XmlFile dataFile = getDataFile();
dataFile.write(this);
SaveableListener.fireOnChange(this, dataFile);
}
@Nonnull
public File getLogFile() {
return new File(folder.getComputationDir(), "computation.log");
}
@Nonnull
public File getEventsFile() {
return new File(folder.getComputationDir(), "events.log");
}
@WithBridgeMethods(TaskListener.class)
@Nonnull
public synchronized StreamTaskListener createEventsListener() {
File eventsFile = getEventsFile();
if (!eventsFile.getParentFile().isDirectory() && !eventsFile.getParentFile().mkdirs()) {
LOGGER.log(Level.WARNING, "Could not create directory {0} for {1}",
new Object[]{eventsFile.getParentFile(), folder.getFullName()});
// TODO return a StreamTaskListener sending output to a log, for now this will just try and fail to write
}
if (eventStreams == null) {
eventStreams = new EventOutputStreams(new EventOutputStreams.OutputFile() {
@NonNull
@Override
public File get() {
return getEventsFile();
}
@Override
public boolean canWriteNow() {
// TODO rework once JENKINS-42248 is solved
GregorianCalendar timestamp = new GregorianCalendar();
timestamp.setTimeInMillis(System.currentTimeMillis() - 10000L);
Queue.Item probe = new Queue.WaitingItem(timestamp, folder, Collections.emptyList());
for (QueueTaskDispatcher d: QueueTaskDispatcher.all()) {
if (d.canRun(probe) != null) {
return false;
}
}
return true;
}
},
250, TimeUnit.MILLISECONDS,
1024,
true,
EVENT_LOG_MAX_SIZE * 1024,
BACKUP_LOG_COUNT == null ? 0 : Math.max(0, BACKUP_LOG_COUNT)
);
}
return new StreamTaskListener(eventStreams.get(), StandardCharsets.UTF_8);
}
@Nonnull
protected XmlFile getDataFile() {
return new XmlFile(Items.XSTREAM, new File(folder.getComputationDir(), "computation.xml"));
}
public List<Cause> getCauses() {
CauseAction a = getAction(CauseAction.class);
if (a == null) {
return Collections.emptyList();
}
return Collections.unmodifiableList(a.getCauses());
}
/**
* {@inheritDoc}
*/
@Override
public String getDisplayName() {
return AlternativeUiTextProvider.get(DISPLAY_NAME, this, Messages.FolderComputation_DisplayName());
}
/**
* {@inheritDoc}
*/
@Override
public String getSearchUrl() {
return "computation/";
}
/**
* {@inheritDoc}
*/
@Override
@Nonnull
public ComputedFolder<I> getParent() {
return folder;
}
/**
* {@inheritDoc}
*/
@Override
public long getEstimatedDuration() {
if (durations == null || durations.isEmpty()) {
return -1;
}
long total = 0;
for (Long d : durations) {
total += d;
}
return total / durations.size();
}
public boolean isBuilding() {
return Executor.of(this) != null;
}
public boolean isLogUpdated() {
return result == null;
}
@Nonnull
public AnnotatedLargeText<FolderComputation<I>> getLogText() {
return new AnnotatedLargeText<FolderComputation<I>>(getLogFile(), StandardCharsets.UTF_8, !isLogUpdated(), this);
}
@Nonnull
public AnnotatedLargeText<FolderComputation<I>> getEventsText() {
File eventsFile = getEventsFile();
if (eventsFile.length() <= 0) {
ByteBuffer buffer = new ByteBuffer();
try {
buffer.write(
String.format("No events as of %tc, waiting for events...%n", new Date())
.getBytes(StandardCharsets.UTF_8)
);
return new AnnotatedLargeText<FolderComputation<I>>(buffer, StandardCharsets.UTF_8, false, this);
} catch (IOException e) {
// ignore and fall through
}
}
return new AnnotatedLargeText<FolderComputation<I>>(eventsFile, StandardCharsets.UTF_8, false, this);
}
@SuppressFBWarnings(value = "RV_RETURN_VALUE_IGNORED", justification = "Only one page is ever written here")
public void writeLogTo(long offset, XMLOutput out) throws IOException {
getLogText().writeHtmlTo(offset, out.asWriter());
}
public void writeWholeLogTo(@Nonnull OutputStream out) throws IOException, InterruptedException {
long pos = 0;
AnnotatedLargeText<?> logText;
do {
logText = getLogText();
pos = logText.writeLogTo(pos, out);
} while (!logText.isComplete());
}
/**
* Sends out the raw console output.
*
* @param req the request
* @param rsp the response.
* @throws IOException if things go wrong.
*/
public void doConsoleText(StaplerRequest req, StaplerResponse rsp) throws IOException {
rsp.setContentType("text/plain;charset=UTF-8");
PlainTextConsoleOutputStream out = new PlainTextConsoleOutputStream(rsp.getCompressedOutputStream(req));
InputStream input = getLogInputStream();
try {
IOUtils.copy(input, out);
out.flush();
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(out);
}
}
/**
* Stops this build if it's still going.
*
* @return the Http response.
*/
@RequirePOST
public synchronized HttpResponse doStop() throws IOException, ServletException {
Executor e = Executor.of(this);
if (e != null) {
return e.doStop();
} else {
// nothing is building
return HttpResponses.forwardToPreviousPage();
}
}
/**
* Returns an input stream that reads from the log file.
* It will use a gzip-compressed log file (log.gz) if that exists.
*
* @return An input stream from the log file.
* If the log file does not exist, the error message will be returned to the output.
* @throws IOException if things go wrong
*/
@Nonnull
public InputStream getLogInputStream() throws IOException {
File logFile = getLogFile();
if (logFile.exists()) {
// Checking if a ".gz" file was return
FileInputStream fis = new FileInputStream(logFile);
if (logFile.getName().endsWith(".gz")) {
return new GZIPInputStream(fis);
} else {
return fis;
}
}
String message = "No such file: " + logFile;
return new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8));
}
@CheckForNull
public Result getResult() {
return result;
}
@Nonnull
public Calendar getTimestamp() {
GregorianCalendar c = new GregorianCalendar();
c.setTimeInMillis(timestamp);
return c;
}
@Nonnull
public String getDurationString() {
if (isBuilding()) {
return Messages.Run_InProgressDuration(Util.getTimeSpanString(System.currentTimeMillis() - timestamp));
} else {
return Util.getTimeSpanString(duration);
}
}
@Nonnull
public String getUrl() {
return folder.getUrl() + "computation/";
}
@CheckForNull
public Result getPreviousResult() {
return previousResult;
}
@Nonnull
public BallColor getIconColor() {
Result _result = result;
if (_result != null) {
return _result.color;
}
Result previousResult = getPreviousResult();
if (previousResult == null) {
return isBuilding() ? BallColor.GREY_ANIME : BallColor.GREY;
}
return isBuilding() ? previousResult.color.anime() : previousResult.color;
}
public String getBuildStatusIconClassName() {
return getIconColor().getIconClassName();
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return getClass().getSimpleName() + "[" + folder.getFullName() + "]";
}
static {
Items.XSTREAM.alias("folder-computation", FolderComputation.class);
}
/**
* Allow other code to override the display name for {@link FolderComputation}.
*/
public static final Message<FolderComputation> DISPLAY_NAME = new Message<FolderComputation>();
}