gnodet-bot commented on code in PR #27516:
URL: https://github.com/apache/camel/pull/27516#discussion_r4212271791
##########
dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/CamelMonitor.java:
##########
@@ -560,6 +560,22 @@ public void explainLogError(String error) {
* session with confirmations off quit at once. The F2 menu's Quit entry
and the {@code tui_action} MCP tool go
* through here too, so quitting behaves the same however it is asked for.
*/
+ /**
+ * Deletes the folders of the examples that no longer run; an example that
still runs keeps its folder and the files
+ * it reads (CAMEL-25425).
+ */
+ private void deleteExampleDirs() {
+ try {
+ List<Path> runningDirs = ctx.data.get().stream()
+ .filter(i -> !i.vanishing && i.directory != null)
+ .map(i -> Path.of(i.directory))
+ .toList();
+ actionsPopup.getLaunchManager().deleteExampleDirs(runningDirs);
+ } catch (Exception e) {
+ // best effort: what is left is removed by a later session
+ }
+ }
+
Review Comment:
⚠️ **Javadoc misattachment:** `deleteExampleDirs()` is inserted between the
`quitTui` Javadoc block (ending a few lines above) and `quitTui(boolean
confirm)` itself. The Javadoc now documents the wrong method — `quitTui` loses
its documentation.
Please move `deleteExampleDirs()` to a different location in the file (e.g.
after `quitTui`), so the existing Javadoc stays attached to `quitTui`.
##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/ExampleHelper.java:
##########
@@ -476,15 +499,60 @@ public static Path downloadGithubExample(JsonObject
entry) throws Exception {
Path targetFile = tempDir.resolve(fileName);
Files.createDirectories(targetFile.getParent());
Files.writeString(targetFile, res.body());
- targetFile.toFile().deleteOnExit();
- targetFile.getParent().toFile().deleteOnExit();
}
}
- tempDir.toFile().deleteOnExit();
+ if (deleteOnExit) {
+ deleteOnExit(tempDir);
+ }
return tempDir;
}
+ /**
+ * Deletes the folder of an example, with what the example wrote in it
(out/, a done folder), when this JVM exits.
+ * File.deleteOnExit leaves the folder behind once anything else is in it.
+ */
+ private static void deleteOnExit(Path dir) {
+ Runtime.getRuntime().addShutdownHook(new Thread(() ->
FileUtil.removeDir(dir.toFile()), "CamelExampleCleanup"));
+ }
+
+ /**
+ * The folders of examples in the temporary directory that are older than
the given age and that none of the given
+ * folders (those of running integrations) is in: left behind by runs that
ended, or by a process that was killed.
+ */
+ public static List<Path> staleExampleDirs(Collection<Path> inUse, Duration
minAge) {
+ return staleExampleDirs(Path.of(System.getProperty("java.io.tmpdir")),
inUse, minAge);
+ }
+
+ static List<Path> staleExampleDirs(Path tmp, Collection<Path> inUse,
Duration minAge) {
+ List<Path> answer = new ArrayList<>();
+ List<Path> used = inUse.stream().map(ExampleHelper::realPath).toList();
+ long cutoff = System.currentTimeMillis() - minAge.toMillis();
+ try (var dirs = Files.newDirectoryStream(tmp, EXAMPLE_DIR_PREFIX +
"*")) {
+ for (Path dir : dirs) {
+ if (!Files.isDirectory(dir) ||
Files.getLastModifiedTime(dir).toMillis() > cutoff) {
+ continue;
+ }
+ Path real = realPath(dir);
+ if (used.stream().noneMatch(u -> u.startsWith(real))) {
+ answer.add(dir);
+ }
+ }
+ } catch (Exception e) {
+ // the temporary directory cannot be listed: nothing to clean
Review Comment:
🔵 **NIT:** Silent catch swallows all exceptions from
`Files.newDirectoryStream()`. This is consistent with the pre-existing style in
this class and the failure mode is benign (best-effort cleanup — leftovers are
removed by a later session). Fine as-is.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]