Skip to content

Commit

Permalink
Merge pull request #624 from jglick/SupportLogHandler
Browse files Browse the repository at this point in the history
Stop agents before exiting test cases, and reliably close `all_*.log` when agents exit
  • Loading branch information
jglick authored Feb 19, 2025
2 parents 3435ec4 + d576db6 commit 49b01a4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
<!-- https://www.jenkins.io/doc/developer/plugin-development/choosing-jenkins-baseline/ -->
<jenkins.baseline>2.479</jenkins.baseline>
<jenkins.version>${jenkins.baseline}.1</jenkins.version>
<!-- TODO until in parent -->
<jenkins-test-harness.version>2403.v256947ecb_c8a_</jenkins-test-harness.version>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
<spotless.check.skip>false</spotless.check.skip>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ private void setFile(File file) throws FileNotFoundException {
parentFile.mkdirs();
}

StreamUtils.closeQuietly(null); // ensure class is loaded so close() can succeed
boolean success = false;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/cloudbees/jenkins/support/SupportPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,15 @@ public Void call() {
// avoid double installation of the handler. JNLP agents can reconnect to the controller multiple times
// and each connection gets a different RemoteClassLoader, so we need to evict them by class name,
// not by their identity.
closeAll();
Runtime.getRuntime().addShutdownHook(new Thread(LogInitializer::closeAll, "close log handlers"));
LogHolder.AGENT_LOG_HANDLER.setLevel(level);
LogHolder.AGENT_LOG_HANDLER.setDirectory(new File(rootPath.getRemote(), SUPPORT_DIRECTORY_NAME), "all");
ROOT_LOGGER.addHandler(LogHolder.AGENT_LOG_HANDLER);
return null;

Check warning on line 870 in src/main/java/com/cloudbees/jenkins/support/SupportPlugin.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 865-870 are not covered by tests
}

private static void closeAll() {
for (Handler h : ROOT_LOGGER.getHandlers()) {
if (h.getClass()
.getName()
Expand All @@ -874,10 +883,6 @@ public Void call() {
}
}
}
LogHolder.AGENT_LOG_HANDLER.setLevel(level);
LogHolder.AGENT_LOG_HANDLER.setDirectory(new File(rootPath.getRemote(), SUPPORT_DIRECTORY_NAME), "all");
ROOT_LOGGER.addHandler(LogHolder.AGENT_LOG_HANDLER);
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void cleanUp() throws Exception {
}

assertEquals(cacheDir.list().length, 2);
agent2.toComputer().disconnect(null).get();
j.getInstance().removeNode(agent2);

generateBundle();
Expand Down

0 comments on commit 49b01a4

Please sign in to comment.