Skip to content

Commit

Permalink
Stream output rather than dump it all out at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
goodj136disney committed May 22, 2019
1 parent 68b41a7 commit a625e4b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
Expand Up @@ -21,6 +21,9 @@ public class ConnectionResponse
@Nullable @CheckForNull
private final JSONObject body;

@Nullable @CheckForNull
private final String rawBody;

@Nonnull
private final int responseCode;

Expand All @@ -29,13 +32,23 @@ public ConnectionResponse(@Nonnull Map<String, List<String>> header, @Nullable J
{
this.header = header;
this.body = body;
this.rawBody = null;
this.responseCode = responseCode;
}

public ConnectionResponse(@Nonnull Map<String, List<String>> header, @Nullable String rawBody, @Nonnull int responseCode)
{
this.header = header;
this.body = null;
this.rawBody = rawBody;
this.responseCode = responseCode;
}

public ConnectionResponse(@Nonnull Map<String, List<String>> header, @Nonnull int responseCode)
{
this.header = header;
this.body = null;
this.rawBody = null;
this.responseCode = responseCode;
}

Expand All @@ -48,6 +61,10 @@ public JSONObject getBody() {
return body;
}

public String getRawBody() {
return rawBody;
}

public int getResponseCode() {
return responseCode;
}
Expand Down
Expand Up @@ -713,30 +713,32 @@ public void performWaitForBuild(BuildContext context, Handle handle) throws IOEx
context.logger.println("Waiting for remote build to finish ...");
}

String consoleOffset = "0";
if (this.getEnhancedLogging()) {
context.logger.println("--------------------------------------------------------------------------------");
context.logger.println();
context.logger.println("Console output of remote job:");
consoleOffset = printOffsetConsoleOutput(context, consoleOffset, buildInfo);
}
while (buildInfo.isRunning()) {
context.logger.println(" Waiting for " + this.pollInterval + " seconds until next poll.");
if (this.getEnhancedLogging()) {
consoleOffset = printOffsetConsoleOutput(context, consoleOffset, buildInfo);
} else {
context.logger.println(" Waiting for " + this.pollInterval + " seconds until next poll.");
}
Thread.sleep(this.pollInterval * 1000);
buildInfo = updateBuildInfo(buildInfo, context);
handle.setBuildInfo(buildInfo);
}
if (this.getEnhancedLogging()) {
context.logger.println("--------------------------------------------------------------------------------");
}

context.logger.println("Remote build finished with status " + buildInfo.getResult().toString() + ".");
if (context.run != null)
RemoteBuildInfoExporterAction.addBuildInfoExporterAction(context.run, jobName, jobNumber, jobURL,
buildInfo);

if (this.getEnhancedLogging()) {
String consoleOutput = getConsoleOutput(jobURL, context);

context.logger.println();
context.logger.println("Console output of remote job:");
context.logger
.println("--------------------------------------------------------------------------------");
context.logger.println(consoleOutput);
context.logger
.println("--------------------------------------------------------------------------------");
}

// If build did not finish with 'success' or 'unstable' then fail build step.
if (buildInfo.getResult() != Result.SUCCESS && buildInfo.getResult() != Result.UNSTABLE) {
// failBuild will check if the 'shouldNotFailBuild' parameter is set or not, so
Expand Down Expand Up @@ -856,10 +858,24 @@ public RemoteBuildInfo updateBuildInfo(@Nonnull RemoteBuildInfo buildInfo, @Nonn
return buildInfo;
}

private String getConsoleOutput(URL url, BuildContext context) throws IOException, InterruptedException {
URL buildUrl = new URL(url, "consoleText");
return HttpHelper.tryGetRawResp(buildUrl.toString(), context, this.getPollInterval(),
this.getConnectionRetryLimit(), this.getAuth2(), getLock(buildUrl.toString()));
private String printOffsetConsoleOutput(BuildContext context, String offset, RemoteBuildInfo buildInfo) throws IOException, InterruptedException {
if(offset.equals("-1")) {
return "-1";
}
String buildUrlString = String.format("%slogText/progressiveText?start=%s", buildInfo.getBuildURL(), offset);
ConnectionResponse response = doGet(buildUrlString, context);

String rawBody = response.getRawBody();
if(rawBody != null && !rawBody.equals("")) {
context.logger.println(rawBody);
}

Map<String,List<String>> header = response.getHeader();
if(header.containsKey("X-More-Data") && header.containsKey("X-Text-Size")){
return header.get("X-Text-Size").get(0);
} else {
return "-1";
}
}

/**
Expand Down
Expand Up @@ -492,7 +492,7 @@ private static ConnectionResponse sendHTTPCall(String urlString, String requestT
// But in newer versions of Jenkins, it just returns an empty response.
// So we need to compensate and check for both.
if (responseCode >= 400 || JSONUtils.mayBeJSON(response) == false) {
return new ConnectionResponse(responseHeader, responseCode);
return new ConnectionResponse(responseHeader, response, responseCode);
} else {
responseObject = (JSONObject) JSONSerializer.toJSON(response);
}
Expand Down

0 comments on commit a625e4b

Please sign in to comment.