Skip to content
Permalink
Browse files

[JENKINS-43199] Reliably close build log file (#2954)

* [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 a0a55d1da6882e8a56d90885e7cd55a22accbfe1
Showing with 18 additions and 8 deletions.
  1. +18 −8 core/src/main/java/hudson/model/Run.java
@@ -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;
@@ -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();
@@ -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
@@ -1830,8 +1841,7 @@ private StreamBuildListener createBuildListener(@Nonnull RunExecution job, Strea
}
}

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

/**

0 comments on commit a0a55d1

Please sign in to comment.
You can’t perform that action at this time.