Skip to content

Commit

Permalink
[JENKINS-43199] Reliably close build log file (#2954)
Browse files Browse the repository at this point in the history
* [JENKINS-43199] Reliably close build log file.

* Try harder to ensure that the logger is closed at the end of the build.
  • Loading branch information
jglick authored and oleg-nenashev committed Aug 3, 2017
1 parent 45aa332 commit a0a55d1
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions core/src/main/java/hudson/model/Run.java
Expand Up @@ -1698,6 +1698,7 @@ protected final void execute(@Nonnull RunExecution job) {
if(result!=null)
return; // already built.

OutputStream logger = null;
StreamBuildListener listener=null;

runner = job;
Expand All @@ -1716,7 +1717,8 @@ protected final void execute(@Nonnull RunExecution job) {
charset = computer.getDefaultCharset();
this.charset = charset.name();
}
listener = createBuildListener(job, listener, charset);
logger = createLogger();
listener = createBuildListener(job, logger, charset);
listener.started(getCauses());

Authentication auth = Jenkins.getAuthentication();
Expand Down Expand Up @@ -1798,23 +1800,32 @@ protected final void execute(@Nonnull RunExecution job) {
try {
getParent().logRotate();
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Failed to rotate log",e);
}
LOGGER.log(Level.SEVERE, "Failed to rotate log",e);
}
} finally {
onEndBuilding();
if (logger != null) {
try {
logger.close();
} catch (IOException x) {
LOGGER.log(Level.WARNING, "failed to close log for " + Run.this, x);
}
}
}
}

private StreamBuildListener createBuildListener(@Nonnull RunExecution job, StreamBuildListener listener, Charset charset) throws IOException, InterruptedException {
private OutputStream createLogger() throws IOException {
// don't do buffering so that what's written to the listener
// gets reflected to the file immediately, which can then be
// served to the browser immediately
OutputStream logger;
try {
logger = Files.newOutputStream(getLogFile().toPath(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
return Files.newOutputStream(getLogFile().toPath(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
} catch (InvalidPathException e) {
throw new IOException(e);
}
}

private StreamBuildListener createBuildListener(@Nonnull RunExecution job, OutputStream logger, Charset charset) throws IOException, InterruptedException {
RunT build = job.getBuild();

// Global log filters
Expand All @@ -1830,8 +1841,7 @@ private StreamBuildListener createBuildListener(@Nonnull RunExecution job, Strea
}
}

listener = new StreamBuildListener(logger,charset);
return listener;
return new StreamBuildListener(logger,charset);
}

/**
Expand Down

0 comments on commit a0a55d1

Please sign in to comment.