davsclaus commented on code in PR #27930:
URL: https://github.com/apache/camel/pull/27930#discussion_r4290242995


##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java:
##########
@@ -3011,72 +3038,63 @@ protected int runBackground(KameletMain main) throws 
Exception {
     }
 
     protected int runBackgroundProcess(ProcessBuilder pb, String kind) throws 
Exception {
-        Path logPath = null;
-        if (backgroundWait) {
-            // store background output in a log file to capture any error on 
startup
-            logPath = getRunBackgroundLogFile("" + new Random().nextLong()); 
// NOSONAR
-            try {
-                Path logDir = CommandLineHelper.getCamelDir();
-                Files.createDirectories(logDir); //make sure the parent dir 
exists
-                Files.createFile(logPath);
-                logPath.toFile().deleteOnExit();
-            } catch (IOException e) {
-                // Ignore
-            }
-            pb.redirectErrorStream(true);
-            pb.redirectOutput(logPath.toFile());
-        }
-
-        Process p = pb.start();
+        // the output goes to ~/.camel/<pid>-run.log, as nobody reads the 
output of a process in the background
+        BackgroundLog.Started started = BackgroundLog.start(pb);
+        Process p = started.process();
         this.spawnPid = p.pid();
-        if (!exportRun && !transformRun && !transformMessageRun) {
+        Path logPath = started.log();
+
+        // the commands that run Camel for themselves (export, transform) keep 
quiet, and need no log afterwards
+        boolean internal = exportRun || transformRun || transformMessageRun;
+        if (!internal) {
             printer().println(
                     "Running " + kind + ": " + name + " in background with 
PID: " + p.pid()
-                              + (backgroundWait ? " (waiting to startup)" : 
""));
+                              + (backgroundWait > 0 ? " (waiting to startup)" 
: ""));
+            printer().println("Log: " + logPath);
         }
-
-        int ec = 0;
-        if (logPath != null) {
-            StopWatch watch = new StopWatch();
+        try {
+            if (backgroundWait <= 0) {
+                return 0;
+            }
+            long deadline = System.currentTimeMillis() + backgroundWait;
             int state = 0; // state 5 is running
-            while (p.isAlive() && watch.taken() < 20000 && state < 5) {
+            while (p.isAlive() && System.currentTimeMillis() < deadline && 
state < 5) {
                 JsonObject root = loadStatus(p.pid());
-                if (root != null) {
-                    JsonObject context = (JsonObject) root.get("context");
-                    if (context != null) {
-                        state = context.getInteger("phase");
-                    }
+                if (root != null && root.get("context") instanceof JsonObject 
context) {
+                    state = context.getIntegerOrDefault("phase", 0);
                 }
                 if (state < 5) {
                     try {
                         Thread.sleep(500);
-                    } catch (Exception e) {
-                        // we want to exit
+                    } catch (InterruptedException e) {
+                        Thread.currentThread().interrupt();
                         break;
                     }
                 }
             }
             if (!p.isAlive()) {
-                ec = p.exitValue();
+                int ec = p.exitValue();
                 if (ec != 0) {
                     printer().printErr(kind + ": " + name + " startup 
failure");
                     printer().println("");
-                    String text = Files.readString(logPath);
-                    printer().print(text);
+                    printer().print(Files.readString(logPath));

Review Comment:
   Fixed in the next commit: the log of a failed startup is printed with the 
new `BackgroundLog.read(log)`, which reads it leniently like `tail`. I did not 
use `tail(logPath, Integer.MAX_VALUE)`: `tail` sized its deque with the number 
of lines, so that would have allocated an array of `Integer.MAX_VALUE`; the 
deque now grows as needed. Test: a log with a Latin-1 byte, read and tailed.
   
   _Claude Code on behalf of davsclaus_



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