You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2025/07/28 07:56:41 UTC

(camel) branch ib created (now f063c97065a)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a change to branch ib
in repository https://gitbox.apache.org/repos/asf/camel.git


      at f063c97065a CAMEL-22279: camel-jbang - infra run to have --background option

This branch includes the following new commits:

     new f063c97065a CAMEL-22279: camel-jbang - infra run to have --background option

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(camel) 01/01: CAMEL-22279: camel-jbang - infra run to have --background option

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch ib
in repository https://gitbox.apache.org/repos/asf/camel.git

commit f063c97065a48d019621ab0c8cbda9c46d3cff6f
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jul 28 09:56:22 2025 +0200

    CAMEL-22279: camel-jbang - infra run to have --background option
---
 .../camel/dsl/jbang/core/commands/Debug.java       |  2 +-
 .../apache/camel/dsl/jbang/core/commands/Run.java  | 11 +--
 .../camel/dsl/jbang/core/commands/RunHelper.java   | 12 +++
 .../dsl/jbang/core/commands/infra/InfraRun.java    | 92 +++++++++++++++++++---
 .../dsl/jbang/core/commands/infra/InfraTest.java   |  3 +-
 5 files changed, 95 insertions(+), 25 deletions(-)

diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java
index ba169769506..a1de3e1db54 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java
@@ -259,7 +259,7 @@ public class Debug extends Run {
         cmds.add("--prop=camel.debug.loggingLevel=DEBUG");
         cmds.add("--prop=camel.debug.singleStepIncludeStartEnd=true");
 
-        addCamelCommand(cmds);
+        RunHelper.addCamelJBangCommand(cmds);
 
         ProcessBuilder pb = new ProcessBuilder();
         pb.command(cmds);
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index a7a54a0e398..6773f050d4f 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -1455,7 +1455,7 @@ public class Run extends CamelCommand {
         cmds.remove("--background-wait=true");
         cmds.remove("--background-wait");
 
-        addCamelCommand(cmds);
+        RunHelper.addCamelJBangCommand(cmds);
 
         ProcessBuilder pb = new ProcessBuilder();
         pb.command(cmds);
@@ -2146,15 +2146,6 @@ public class Run extends CamelCommand {
         }
     }
 
-    protected static void addCamelCommand(List<String> cmds) {
-        if (FileUtil.isWindows()) {
-            String jbangDir = System.getenv().getOrDefault("JBANG_DIR", System.getProperty("user.home") + "\\.jbang");
-            cmds.add(0, jbangDir + "\\bin\\camel.cmd");
-        } else {
-            cmds.add(0, "camel");
-        }
-    }
-
     private JsonObject loadStatus(long pid) {
         try {
             Path p = getStatusFile(Long.toString(pid));
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/RunHelper.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/RunHelper.java
index afd2a4022c7..d9ccd971fd6 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/RunHelper.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/RunHelper.java
@@ -302,4 +302,16 @@ public final class RunHelper {
         }
     }
 
+    /**
+     * Adds camel to the start of the list of commands to make it possible to run camel-jbang using a spawned process
+     * (to run in background).
+     */
+    public static void addCamelJBangCommand(List<String> cmds) {
+        if (FileUtil.isWindows()) {
+            String jbangDir = System.getenv().getOrDefault("JBANG_DIR", System.getProperty("user.home") + "\\.jbang");
+            cmds.add(0, jbangDir + "\\bin\\camel.cmd");
+        } else {
+            cmds.add(0, "camel");
+        }
+    }
 }
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraRun.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraRun.java
index da1c9ff58dd..c6da665a4b3 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraRun.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraRun.java
@@ -17,14 +17,17 @@
 package org.apache.camel.dsl.jbang.core.commands.infra;
 
 import java.io.Console;
+import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.Method;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
@@ -35,10 +38,15 @@ import org.apache.camel.main.download.MavenDependencyDownloader;
 import org.apache.camel.tooling.maven.MavenArtifact;
 import picocli.CommandLine;
 
+import static org.apache.camel.dsl.jbang.core.commands.RunHelper.addCamelJBangCommand;
+
 @CommandLine.Command(name = "run",
                      description = "Run an external service")
 public class InfraRun extends InfraBaseCommand {
 
+    @CommandLine.Spec
+    CommandLine.Model.CommandSpec spec;
+
     @CommandLine.Parameters(description = "Service name", arity = "1")
     private List<String> serviceName;
 
@@ -46,6 +54,9 @@ public class InfraRun extends InfraBaseCommand {
                         description = "Log container output to console")
     boolean logToStdout;
 
+    @CommandLine.Option(names = { "--background" }, defaultValue = "false", description = "Run in the background")
+    boolean background;
+
     public InfraRun(CamelJBangMain main) {
         super(main);
     }
@@ -92,6 +103,38 @@ public class InfraRun extends InfraBaseCommand {
             return 1;
         }
 
+        if (background) {
+            return runBackground(testService);
+        } else {
+            return doRun(testService, testServiceImplementation, testInfraService);
+        }
+    }
+
+    protected int runBackground(String testService) throws Exception {
+        List<String> cmds;
+        if (spec != null) {
+            cmds = new ArrayList<>(spec.commandLine().getParseResult().originalArgs());
+        } else {
+            cmds = new ArrayList<>();
+            cmds.add("run");
+        }
+
+        cmds.remove("--background=true");
+        cmds.remove("--background");
+
+        addCamelJBangCommand(cmds);
+
+        ProcessBuilder pb = new ProcessBuilder();
+        pb.command(cmds);
+
+        Process p = pb.start();
+        printer().println(
+                "Running " + testService + " in background with PID: " + p.pid());
+        return 0;
+    }
+
+    protected Integer doRun(String testService, String testServiceImplementation, TestInfraService testInfraService)
+            throws Exception {
         DependencyDownloaderClassLoader cl = getDependencyDownloaderClassLoader(testInfraService);
 
         // Update the class loader
@@ -100,14 +143,6 @@ public class InfraRun extends InfraBaseCommand {
         String serviceInterface = testInfraService.service();
         String serviceImpl = testInfraService.implementation();
 
-        if (!jsonOutput) {
-            String prefix = "";
-            if (testServiceImplementation != null) {
-                prefix = " with implementation " + testServiceImplementation;
-            }
-            printer().println("Starting service " + testService + prefix);
-        }
-
         Object actualService = cl.loadClass(serviceImpl).newInstance();
 
         // Make sure the actualService can be run with initialize method
@@ -125,6 +160,13 @@ public class InfraRun extends InfraBaseCommand {
             return 1;
         }
 
+        if (!jsonOutput) {
+            String prefix = "";
+            if (testServiceImplementation != null) {
+                prefix = " with implementation " + testServiceImplementation;
+            }
+            printer().println("Starting service " + testService + prefix);
+        }
         actualService.getClass().getMethod("initialize").invoke(actualService);
 
         Method[] serviceMethods = cl.loadClass(serviceInterface).getDeclaredMethods();
@@ -161,16 +203,16 @@ public class InfraRun extends InfraBaseCommand {
                     .invoke(actualService, containerLogConsumer);
         }
 
-        if (!jsonOutput) {
-            printer().println("Press ENTER to stop the execution");
-        }
-
         AtomicBoolean closed = new AtomicBoolean();
         // use shutdown hook as fallback to shut-down and delete files
         Runtime.getRuntime().addShutdownHook(new Thread(() -> shutdownInfra(closed, logFile, jsonFile, actualService)));
 
+        // running in foreground then wait for user to exit
         final Console c = System.console();
         if (c != null) {
+            if (!jsonOutput) {
+                printer().println("Press ENTER to stop the execution");
+            }
             boolean quit = false;
             do {
                 String line = c.readLine();
@@ -178,6 +220,31 @@ public class InfraRun extends InfraBaseCommand {
                     quit = true;
                 }
             } while (!quit);
+        } else {
+            final CountDownLatch latch = new CountDownLatch(1);
+            // headless (running in background so wait until being signalled to stop)
+            Thread t = new Thread(() -> {
+                while (latch.getCount() > 0) {
+                    try {
+                        Thread.sleep(1000);
+                    } catch (Exception e) {
+                        // ignore
+                    }
+                    File f = jsonFile.toFile();
+                    if (!f.exists()) {
+                        latch.countDown();
+                    }
+                }
+            }, "InfraWait");
+            t.start();
+
+            // wait for this process to be stopped
+            printer().println("Running (use camel infra stop " + testService + " to stop the execution)");
+            try {
+                latch.await();
+            } catch (Exception e) {
+                // ignore
+            }
         }
 
         shutdownInfra(closed, logFile, jsonFile, actualService);
@@ -203,7 +270,6 @@ public class InfraRun extends InfraBaseCommand {
                 // ignore
             }
         }
-
     }
 
     private static Path createFile(String name) throws IOException {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraTest.java b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraTest.java
index 2ef06f7cb49..d74bace585a 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraTest.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraTest.java
@@ -49,7 +49,8 @@ public class InfraTest extends CamelCommandBaseTest {
         Awaitility.await().untilAsserted(() -> {
             List<String> lines = printer.getLines();
             Assertions.assertThat(lines).contains("Starting service ftp");
-            Assertions.assertThat(lines).contains("Press ENTER to stop the execution");
+            // because we run headless unit test then you would see this message instead of press ENTER to stop
+            Assertions.assertThat(lines).contains("Running (use camel infra stop ftp to stop the execution)");
         });
 
         thread.interrupt();