gnodet-bot commented on code in PR #27516:
URL: https://github.com/apache/camel/pull/27516#discussion_r4212569062


##########
dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/LaunchManager.java:
##########
@@ -414,6 +509,49 @@ private void notify(String msg, boolean error) {
      */
     static final long WATCH_MS = 5 * 60_000;
 
+    // how the latest launch of each name went, for an agent that asks 
(tui_run_example waits for it)
+    private final Map<String, LaunchOutcome> outcomes = new 
ConcurrentHashMap<>();
+
+    /**
+     * How a launch went: started (Camel said so), or failed with the end of 
its output; null while it is starting.
+     */
+    record LaunchOutcome(boolean ok, String log) {
+
+        private static final Pattern ANSI = 
Pattern.compile("\u001B\\[[0-9;]*m");
+
+        static LaunchOutcome started() {
+            return new LaunchOutcome(true, null);
+        }
+
+        static LaunchOutcome failed(Path outputFile) {
+            String tail = null;
+            try {
+                // without colors and stack frames: the messages and their 
causes are what tell why
+                List<String> lines = Files.readAllLines(outputFile, 
StandardCharsets.UTF_8).stream()
+                        .map(l -> ANSI.matcher(l).replaceAll(""))
+                        .filter(l -> !l.stripLeading().startsWith("at ") && 
!l.stripLeading().startsWith("... "))
+                        .toList();
+                tail = String.join("\n", lines.subList(Math.max(0, 
lines.size() - 30), lines.size()));
+            } catch (Exception e) {
+                // no output to show
+            }
+            return new LaunchOutcome(false, tail);
+        }

Review Comment:
   ⚠️ **Bounded tail read:** `Files.readAllLines()` materialises the entire 
output file in memory before `subList` takes the last 30 lines. The output file 
captures all stdout/stderr of the spawned process (including Maven export 
builds), which can grow to tens of MB before start-failure detection kicks in. 
Use `Files.lines()` to stream lazily:
   
   ```suggestion
           static LaunchOutcome failed(Path outputFile) {
               String tail = null;
               try {
                   // without colors and stack frames: the messages and their 
causes are what tell why
                   // Files.lines streams lazily — avoids loading the full 
output file into heap
                   List<String> lines;
                   try (var stream = Files.lines(outputFile, 
StandardCharsets.UTF_8)) {
                       lines = stream
                               .map(l -> ANSI.matcher(l).replaceAll(""))
                               .filter(l -> !l.stripLeading().startsWith("at ") 
&& !l.stripLeading().startsWith("... "))
                               .collect(java.util.stream.Collectors.toList());
                   }
                   tail = String.join("\n", lines.subList(Math.max(0, 
lines.size() - 30), lines.size()));
               } catch (Exception e) {
                   // no output to show
               }
               return new LaunchOutcome(false, tail);
           }
   ```



-- 
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]

Reply via email to