|
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 | + |
28 | 44 | /**
|
29 | 45 | * This will be returned by the API in cases where the build has never run.
|
30 | 46 | * For example {@link Build#BUILD_HAS_NEVER_RUN}
|
@@ -350,24 +366,100 @@ public boolean apply(Map<String, Object> action) {
|
350 | 366 | }
|
351 | 367 |
|
352 | 368 | /**
|
353 |
| - * @return The console output of the build. The line separation is done by |
| 369 | + * @return The full console output of the build. The line separation is done by |
354 | 370 | * {@code CR+LF}.
|
| 371 | + * |
| 372 | + * @see streamConsoleOutput method for obtaining logs for running build |
| 373 | + * |
355 | 374 | * @throws IOException in case of a failure.
|
356 | 375 | */
|
357 | 376 | public String getConsoleOutputText() throws IOException {
|
358 | 377 | return client.get(getUrl() + "/logText/progressiveText");
|
359 | 378 | }
|
360 | 379 |
|
361 | 380 | /**
|
362 |
| - * The console output with HTML. |
363 |
| - * |
| 381 | + * The full console output with HTML. |
| 382 | + * |
| 383 | + * @see streamConsoleOutput method for obtaining logs for running build |
| 384 | + * |
364 | 385 | * @return The console output as HTML.
|
365 | 386 | * @throws IOException
|
366 | 387 | */
|
367 | 388 | public String getConsoleOutputHtml() throws IOException {
|
368 | 389 | return client.get(getUrl() + "/logText/progressiveHtml");
|
369 | 390 | }
|
370 | 391 |
|
| 392 | + |
| 393 | + /** |
| 394 | + * Stream build console output log as text using BuildConsoleStreamListener |
| 395 | + * Method can be used to asynchronously obtain logs for running build. |
| 396 | + * |
| 397 | + * @param listener interface used to asynchronously obtain logs |
| 398 | + * @param poolingInterval interval (seconds) used to pool jenkins for logs |
| 399 | + * @param poolingTimeout pooling timeout (seconds) used to break pooling in case build stuck |
| 400 | + * |
| 401 | + */ |
| 402 | + public void streamConsoleOutput(BuildConsoleStreamListener listener, int poolingInterval, int poolingTimeout) throws IOException, InterruptedException { |
| 403 | + int bufferOffset = 0; |
| 404 | + // Calculate start and timeout |
| 405 | + long startTime = System.currentTimeMillis(); |
| 406 | + long timeoutTime = startTime + (poolingTimeout * 1000); |
| 407 | + // Pool for logs |
| 408 | + while (true) { |
| 409 | + Thread.sleep(poolingInterval); |
| 410 | + ConsoleLog consoleLog = getConsoleOutputText(bufferOffset); |
| 411 | + String logString = consoleLog.getConsoleLog(); |
| 412 | + if (logString != null && !logString.isEmpty()) { |
| 413 | + listener.onData(logString); |
| 414 | + } |
| 415 | + if (consoleLog.getHasMoreData()) { |
| 416 | + bufferOffset = consoleLog.getCurrentBufferSize(); |
| 417 | + } else { |
| 418 | + listener.finished(); |
| 419 | + break; |
| 420 | + } |
| 421 | + long currentTime = System.currentTimeMillis(); |
| 422 | + |
| 423 | + if(currentTime> timeoutTime){ |
| 424 | + LOGGER.warn("Pooling for build {0} for {2} timeout! Check if job stuck in jenkins", this.getDisplayName(), this.getNumber()); |
| 425 | + break; |
| 426 | + } |
| 427 | + } |
| 428 | + } |
| 429 | + |
| 430 | + /** |
| 431 | + * Get build console output log as text. |
| 432 | + * Use this method to periodically obtain logs from jenkins and skip chunks that were already received |
| 433 | + * |
| 434 | + * @param bufferOffset offset in console lo |
| 435 | + * @return ConsoleLog object containing console output of the build. The line separation is done by |
| 436 | + * {@code CR+LF}. |
| 437 | + * @throws IOException in case of a failure. |
| 438 | + */ |
| 439 | + public ConsoleLog getConsoleOutputText(int bufferOffset) throws IOException { |
| 440 | + List<NameValuePair> formData = new ArrayList<>(); |
| 441 | + formData.add(new BasicNameValuePair("start", Integer.toString(bufferOffset))); |
| 442 | + String path = getUrl() + "logText/progressiveText"; |
| 443 | + HttpResponse httpResponse = client.post_form_with_result(path, formData, false); |
| 444 | + Header moreDataHeader = httpResponse.getFirstHeader("x-more-data"); |
| 445 | + Header textSizeHeader = httpResponse.getFirstHeader("x-text-size"); |
| 446 | + String response = EntityUtils.toString(httpResponse.getEntity()); |
| 447 | + boolean hasMoreData = false; |
| 448 | + if (moreDataHeader != null) { |
| 449 | + hasMoreData = Boolean.TRUE.toString().equals(moreDataHeader.getValue()); |
| 450 | + } |
| 451 | + Integer currentBufferSize = bufferOffset; |
| 452 | + if (textSizeHeader != null) { |
| 453 | + try { |
| 454 | + currentBufferSize = Integer.parseInt(textSizeHeader.getValue()); |
| 455 | + } catch (NumberFormatException e) { |
| 456 | + LOGGER.warn("Cannot parse buffer size for job {0} build {1}. Using current offset!", this.getDisplayName(), this.getNumber()); |
| 457 | + } |
| 458 | + } |
| 459 | + return new ConsoleLog(response, hasMoreData, currentBufferSize); |
| 460 | + } |
| 461 | + |
| 462 | + |
371 | 463 | public BuildChangeSet getChangeSet() {
|
372 | 464 | return changeSet;
|
373 | 465 | }
|
|
0 commit comments