|
9 | 9 | import com.google.common.base.Predicate;
|
10 | 10 | import com.google.common.base.Strings;
|
11 | 11 | import com.google.common.collect.ImmutableMap;
|
| 12 | +import com.offbytwo.jenkins.helper.BuildConsoleStreamListener; |
| 13 | +import org.apache.http.Header; |
| 14 | +import org.apache.http.HttpResponse; |
| 15 | +import org.apache.http.NameValuePair; |
| 16 | +import org.apache.http.message.BasicNameValuePair; |
| 17 | +import org.apache.http.util.EntityUtils; |
| 18 | +import org.slf4j.Logger; |
| 19 | +import org.slf4j.LoggerFactory; |
12 | 20 |
|
13 | 21 | import java.io.IOException;
|
14 | 22 | import java.io.InputStream;
|
15 | 23 | import java.net.URI;
|
16 | 24 | import java.net.URISyntaxException;
|
17 |
| -import java.util.*; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Collection; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Objects; |
18 | 32 |
|
19 | 33 | import static com.google.common.collect.Collections2.filter;
|
20 | 34 |
|
|
25 | 39 | */
|
26 | 40 | public class BuildWithDetails extends Build {
|
27 | 41 |
|
| 42 | + private final Logger LOGGER = LoggerFactory.getLogger(getClass()); |
| 43 | + |
| 44 | + public final static String TEXT_SIZE_HEADER = "x-text-size"; |
| 45 | + public final static String MORE_DATA_HEADER = "x-more-data"; |
| 46 | + |
28 | 47 | /**
|
29 | 48 | * This will be returned by the API in cases where the build has never run.
|
30 | 49 | * For example {@link Build#BUILD_HAS_NEVER_RUN}
|
@@ -350,24 +369,104 @@ public boolean apply(Map<String, Object> action) {
|
350 | 369 | }
|
351 | 370 |
|
352 | 371 | /**
|
353 |
| - * @return The console output of the build. The line separation is done by |
| 372 | + * @return The full console output of the build. The line separation is done by |
354 | 373 | * {@code CR+LF}.
|
| 374 | + * |
| 375 | + * @see streamConsoleOutput method for obtaining logs for running build |
| 376 | + * |
355 | 377 | * @throws IOException in case of a failure.
|
356 | 378 | */
|
357 | 379 | public String getConsoleOutputText() throws IOException {
|
358 | 380 | return client.get(getUrl() + "/logText/progressiveText");
|
359 | 381 | }
|
360 | 382 |
|
361 | 383 | /**
|
362 |
| - * The console output with HTML. |
363 |
| - * |
| 384 | + * The full console output with HTML. |
| 385 | + * |
| 386 | + * @see streamConsoleOutput method for obtaining logs for running build |
| 387 | + * |
364 | 388 | * @return The console output as HTML.
|
365 | 389 | * @throws IOException in case of an error.
|
366 | 390 | */
|
367 | 391 | public String getConsoleOutputHtml() throws IOException {
|
368 | 392 | return client.get(getUrl() + "/logText/progressiveHtml");
|
369 | 393 | }
|
370 | 394 |
|
| 395 | + |
| 396 | + /** |
| 397 | + * Stream build console output log as text using BuildConsoleStreamListener |
| 398 | + * Method can be used to asynchronously obtain logs for running build. |
| 399 | + * |
| 400 | + * @param listener interface used to asynchronously obtain logs |
| 401 | + * @param poolingInterval interval (seconds) used to pool jenkins for logs |
| 402 | + * @param poolingTimeout pooling timeout (seconds) used to break pooling in case build stuck |
| 403 | + * |
| 404 | + */ |
| 405 | + public void streamConsoleOutput(final BuildConsoleStreamListener listener, final int poolingInterval, final int poolingTimeout) throws InterruptedException, IOException { |
| 406 | + // Calculate start and timeout |
| 407 | + final long startTime = System.currentTimeMillis(); |
| 408 | + final long timeoutTime = startTime + (poolingTimeout * 1000); |
| 409 | + |
| 410 | + int bufferOffset = 0; |
| 411 | + while (true) { |
| 412 | + Thread.sleep(poolingInterval * 1000); |
| 413 | + |
| 414 | + ConsoleLog consoleLog = null; |
| 415 | + consoleLog = getConsoleOutputText(bufferOffset); |
| 416 | + String logString = consoleLog.getConsoleLog(); |
| 417 | + if (logString != null && !logString.isEmpty()) { |
| 418 | + listener.onData(logString); |
| 419 | + } |
| 420 | + if (consoleLog.getHasMoreData()) { |
| 421 | + bufferOffset = consoleLog.getCurrentBufferSize(); |
| 422 | + } else { |
| 423 | + listener.finished(); |
| 424 | + break; |
| 425 | + } |
| 426 | + long currentTime = System.currentTimeMillis(); |
| 427 | + |
| 428 | + if (currentTime > timeoutTime) { |
| 429 | + LOGGER.warn("Pooling for build {0} for {2} timeout! Check if job stuck in jenkins", |
| 430 | + BuildWithDetails.this.getDisplayName(), BuildWithDetails.this.getNumber()); |
| 431 | + break; |
| 432 | + } |
| 433 | + } |
| 434 | + } |
| 435 | + |
| 436 | + /** |
| 437 | + * Get build console output log as text. |
| 438 | + * Use this method to periodically obtain logs from jenkins and skip chunks that were already received |
| 439 | + * |
| 440 | + * @param bufferOffset offset in console lo |
| 441 | + * @return ConsoleLog object containing console output of the build. The line separation is done by |
| 442 | + * {@code CR+LF}. |
| 443 | + * @throws IOException in case of a failure. |
| 444 | + */ |
| 445 | + public ConsoleLog getConsoleOutputText(int bufferOffset) throws IOException { |
| 446 | + List<NameValuePair> formData = new ArrayList<>(); |
| 447 | + formData.add(new BasicNameValuePair("start", Integer.toString(bufferOffset))); |
| 448 | + String path = getUrl() + "logText/progressiveText"; |
| 449 | + HttpResponse httpResponse = client.post_form_with_result(path, formData, false); |
| 450 | + |
| 451 | + Header moreDataHeader = httpResponse.getFirstHeader(MORE_DATA_HEADER); |
| 452 | + Header textSizeHeader = httpResponse.getFirstHeader(TEXT_SIZE_HEADER); |
| 453 | + String response = EntityUtils.toString(httpResponse.getEntity()); |
| 454 | + boolean hasMoreData = false; |
| 455 | + if (moreDataHeader != null) { |
| 456 | + hasMoreData = Boolean.TRUE.toString().equals(moreDataHeader.getValue()); |
| 457 | + } |
| 458 | + Integer currentBufferSize = bufferOffset; |
| 459 | + if (textSizeHeader != null) { |
| 460 | + try { |
| 461 | + currentBufferSize = Integer.parseInt(textSizeHeader.getValue()); |
| 462 | + } catch (NumberFormatException e) { |
| 463 | + LOGGER.warn("Cannot parse buffer size for job {0} build {1}. Using current offset!", this.getDisplayName(), this.getNumber()); |
| 464 | + } |
| 465 | + } |
| 466 | + return new ConsoleLog(response, hasMoreData, currentBufferSize); |
| 467 | + } |
| 468 | + |
| 469 | + |
371 | 470 | public BuildChangeSet getChangeSet() {
|
372 | 471 | return changeSet;
|
373 | 472 | }
|
|
0 commit comments