This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 89d63befd8aebd7d3bf04607c7e774be1d5d052b Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat Jan 7 11:42:54 2023 +0100 CAMEL-18674: camel-jbang - Run in background --- .../apache/camel/dsl/jbang/core/commands/Run.java | 63 ++++--------- .../dsl/jbang/core/commands/RunBackground.java | 101 +++++---------------- 2 files changed, 39 insertions(+), 125 deletions(-) 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 ce7d3fe78d1..fef0d52321c 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 @@ -39,7 +39,6 @@ import java.util.Properties; import java.util.Set; import java.util.Stack; import java.util.StringJoiner; -import java.util.concurrent.Callable; import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -93,7 +92,6 @@ class Run extends CamelCommand { private boolean silentRun; private boolean pipeRun; - private boolean backgroundRun; private File logFile; @@ -227,11 +225,6 @@ class Run extends CamelCommand { return run(); } - protected Integer runBackground() throws Exception { - backgroundRun = true; - return run(); - } - private void writeSetting(KameletMain main, Properties existing, String key, String value) { String val = existing != null ? existing.getProperty(key, value) : value; if (val != null) { @@ -272,11 +265,9 @@ class Run extends CamelCommand { } private int run() throws Exception { - if (!backgroundRun) { - File work = new File(WORK_DIR); - removeDir(work); - work.mkdirs(); - } + File work = new File(WORK_DIR); + removeDir(work); + work.mkdirs(); Properties profileProperties = null; File profilePropertiesFile = new File(getProfile() + ".properties"); @@ -318,7 +309,7 @@ class Run extends CamelCommand { String routes = profileProperties != null ? profileProperties.getProperty("camel.main.routesIncludePattern") : null; if (routes == null) { if (!silentRun) { - System.out.println("Cannot run because " + getProfile() + ".properties file does not exist"); + System.out.println("Cannot run because " + getProfile() + ".properties file does not exist or camel.main.routesIncludePattern is not configured"); return 1; } else { // silent-run then auto-detect all files (except properties as they are loaded explicit or via profile) @@ -572,17 +563,19 @@ class Run extends CamelCommand { writeSettings("camel.component.properties.location", loc); } - RunMainTask run = new RunMainTask(main, logFile); - if (background) { - // run as background - ProcessBuilder pb = new ProcessBuilder(); - pb.command("camel", "run-background"); - Process p = pb.start(); - System.out.println("Running Camel integration: " + name + " in background with PID: " + p.pid()); - return 0; - } else { - return run.call(); + return runKameletMain(main); + } + + protected int runKameletMain(KameletMain main) throws Exception { + main.start(); + main.run(); + + // cleanup and delete log file + if (logFile != null) { + FileUtil.deleteFile(logFile); } + + return main.getExitCode(); } private String loadFromCode(String code) throws IOException { @@ -936,28 +929,4 @@ class Run extends CamelCommand { } } - static class RunMainTask implements Callable<Integer> { - - private final KameletMain main; - private final File logFile; - - public RunMainTask(KameletMain main, File logFile) { - this.main = main; - this.logFile = logFile; - } - - @Override - public Integer call() throws Exception { - main.start(); - main.run(); - - // cleanup and delete log file - if (logFile != null) { - FileUtil.deleteFile(logFile); - } - - return main.getExitCode(); - } - } - } diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/RunBackground.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/RunBackground.java index 0ed5ec865b1..bf99638f5cb 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/RunBackground.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/RunBackground.java @@ -16,96 +16,41 @@ */ package org.apache.camel.dsl.jbang.core.commands; -import java.io.File; -import java.util.Properties; -import java.util.StringJoiner; +import java.util.Arrays; +import java.util.List; -import org.apache.camel.dsl.jbang.core.common.RuntimeUtil; -import org.apache.camel.util.CamelCaseOrderedProperties; +import org.apache.camel.main.KameletMain; +import org.apache.camel.util.StringHelper; import picocli.CommandLine; @CommandLine.Command(name = "run-background", description = "Run Camel integration in background") -public class RunBackground extends CamelCommand { +public class RunBackground extends Run { public RunBackground(CamelJBangMain main) { super(main); } @Override - public Integer call() throws Exception { - return run(); - } - - private int run() throws Exception { - File source = new File(Run.WORK_DIR + "/" + Run.RUN_SETTINGS_FILE); - Properties settings = loadProfileProperties(source); - if (settings.isEmpty()) { - System.err.println("Cannot load settings. Use camel run --background instead!"); - return 1; - } - - Run run = new Run(getMain()); - return runBackground(run, settings); - } - - private Properties loadProfileProperties(File source) throws Exception { - Properties prop = new CamelCaseOrderedProperties(); - RuntimeUtil.loadProperties(prop, source); - - // special for routes include pattern that we need to "fix" after reading from properties - // to make this work in run command - String value = prop.getProperty("camel.main.routesIncludePattern"); - if (value != null) { - // if not scheme then must use file: as this is what run command expects - StringJoiner sj = new StringJoiner(","); - for (String part : value.split(",")) { - if (!part.contains(":")) { - part = "file:" + part; - } - sj.add(part); - } - value = sj.toString(); - prop.setProperty("camel.main.routesIncludePattern", value); + protected int runKameletMain(KameletMain main) throws Exception { + // instead of running main we run camel again as background process + String cmd = ProcessHandle.current().info().commandLine().orElse(null); + if (cmd != null) { + cmd = StringHelper.after(cmd, "main.CamelJBang "); } - - return prop; - } - - protected Integer runBackground(Run cmd, Properties settings) throws Exception { - cmd.profile = Run.WORK_DIR + "/camel-jbang-run"; - cmd.loggingLevel = settings.getProperty("loggingLevel", "INFO"); - cmd.loggingColor = "true".equals(settings.getProperty("loggingColor", "true")); - cmd.loggingJson = "true".equals(settings.getProperty("loggingJson", "false")); - cmd.loggingLevel = settings.getProperty("loggingLevel"); - cmd.name = settings.getProperty("camel.main.name", "CamelJBang"); - cmd.dev = "true".equals(settings.getProperty("camel.main.sourceLocationEnabled")); - cmd.trace = "true".equals(settings.getProperty("camel.main.tracing")); - cmd.modeline = "true".equals(settings.getProperty("camel.main.modeline")); - cmd.openapi = settings.getProperty("camel.jbang.openApi"); - cmd.repos = settings.getProperty("camel.jbang.repos"); - cmd.health = "true".equals(settings.getProperty("camel.jbang.health")); - cmd.console = "true".equals(settings.getProperty("camel.jbang.console")); - String s = settings.getProperty("camel.main.durationMaxMessages"); - if (s != null) { - cmd.maxMessages = Integer.parseInt(s); - } - s = settings.getProperty("camel.main.durationMaxSeconds"); - if (s != null) { - cmd.maxSeconds = Integer.parseInt(s); - } - s = settings.getProperty("camel.main.durationMaxIdleSeconds"); - if (s != null) { - cmd.maxIdleSeconds = Integer.parseInt(s); - } - s = settings.getProperty("camel.jbang.platform-http.port"); - if (s != null) { - cmd.port = Integer.parseInt(s); + if (cmd == null) { + System.err.println("No Camel integration files to run"); + return 1; } - cmd.jfr = "jfr".equals(settings.getProperty("camel.jbang.jfr-profile")); - cmd.jfrProfile = settings.getProperty("camel.jbang.jfr-profile"); - cmd.propertiesFiles = settings.getProperty("camel.component.properties.location"); - - return cmd.runBackground(); + cmd = cmd.replaceFirst("run-background", "run"); + cmd = "camel " + cmd; + + ProcessBuilder pb = new ProcessBuilder(); + String[] arr = cmd.split("\\s+"); + List<String> args = Arrays.asList(arr); + pb.command(args); + Process p = pb.start(); + System.out.println("Running Camel integration: " + name + " in background with PID: " + p.pid()); + return 0; } }