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


The following commit(s) were added to refs/heads/main by this push:
     new 592e18ef6575 CAMEL-23640: camel-jbang - add restart command to infra 
service (#24005)
592e18ef6575 is described below

commit 592e18ef6575d74b3512a230eedab4f34aba2361
Author: Shashi Kumar Reddy <[email protected]>
AuthorDate: Mon Jun 15 14:03:19 2026 +0530

    CAMEL-23640: camel-jbang - add restart command to infra service (#24005)
    
    * CAMEL-23640: camel-jbang - add restart command to infra service
    
    * CAMEL-23640: camel-jbang - add missing fixes and docs for infra restart 
command
    
    * CAMEL-23640: remove upgrade guide entry for infra restart (new feature, 
not a change)
    
    * CAMEL-23640: narrow IOException to NoSuchFileException and delegate 
background to InfraRun
---
 .../ROOT/pages/camel-jbang-dev-services.adoc       |  17 ++++
 .../jbang-commands/camel-jbang-infra-restart.adoc  |  30 ++++++
 .../pages/jbang-commands/camel-jbang-infra.adoc    |   1 +
 .../META-INF/camel-jbang-commands-metadata.json    |   2 +-
 .../dsl/jbang/core/commands/CamelJBangMain.java    |   2 +
 .../core/commands/infra/InfraBaseCommand.java      |   3 +
 .../jbang/core/commands/infra/InfraRestart.java    | 103 +++++++++++++++++++++
 .../dsl/jbang/core/commands/infra/InfraRun.java    |  11 +++
 .../core/commands/infra/InfraRestartTest.java      |  91 ++++++++++++++++++
 9 files changed, 259 insertions(+), 1 deletion(-)

diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang-dev-services.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-jbang-dev-services.adoc
index fb5844125621..7c26f560eb46 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang-dev-services.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang-dev-services.adoc
@@ -71,6 +71,23 @@ Starting service kafka with implementation redpanda
 camel infra stop arangodb
 ----
 
+== Restarting a service
+
+Stops a running service and starts it again. This is handy after changing 
configuration or to
+recover a service into a clean state:
+
+[source,bash]
+----
+camel infra restart kafka
+----
+
+Use `--background` to restart the service detached from the terminal:
+
+[source,bash]
+----
+camel infra restart kafka --background
+----
+
 == Listing running services
 
 [source,bash]
diff --git 
a/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-infra-restart.adoc
 
b/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-infra-restart.adoc
new file mode 100644
index 000000000000..b83054fbacee
--- /dev/null
+++ 
b/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-infra-restart.adoc
@@ -0,0 +1,30 @@
+
+// AUTO-GENERATED by camel-package-maven-plugin - DO NOT EDIT THIS FILE
+= camel infra restart
+
+Restarts a running external service
+
+
+== Usage
+
+[source,bash]
+----
+camel infra restart [options]
+----
+
+
+
+== Options
+
+[cols="2,5,1,2",options="header"]
+|===
+| Option | Description | Default | Type
+| `--background` | Run in the background | false | boolean
+| `--json` | Output in JSON Format |  | boolean
+| `--kill` | To force killing the process (SIGKILL) when stopping |  | boolean
+| `--log` | Log container output to console |  | boolean
+| `--port` | Override the default port for the service |  | Integer
+| `-h,--help` | Display the help and sub-commands |  | boolean
+|===
+
+
diff --git 
a/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-infra.adoc 
b/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-infra.adoc
index 0c31a2050f12..7b71aa2a5ee3 100644
--- a/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-infra.adoc
+++ b/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-infra.adoc
@@ -22,6 +22,7 @@ camel infra [options]
 | xref:jbang-commands/camel-jbang-infra-list.adoc[list] | Displays available 
external services
 | xref:jbang-commands/camel-jbang-infra-log.adoc[log] | Displays external 
service logs
 | xref:jbang-commands/camel-jbang-infra-ps.adoc[ps] | Displays running services
+| xref:jbang-commands/camel-jbang-infra-restart.adoc[restart] | Restarts a 
running external service
 | xref:jbang-commands/camel-jbang-infra-run.adoc[run] | Run an external service
 | xref:jbang-commands/camel-jbang-infra-stop.adoc[stop] | Shuts down running 
external services
 |===
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/generated/resources/META-INF/camel-jbang-commands-metadata.json
 
b/dsl/camel-jbang/camel-jbang-core/src/generated/resources/META-INF/camel-jbang-commands-metadata.json
index 5011be663c66..8ea30e87078f 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/generated/resources/META-INF/camel-jbang-commands-metadata.json
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/generated/resources/META-INF/camel-jbang-commands-metadata.json
@@ -18,7 +18,7 @@
     { "name": "get", "fullName": "get", "description": "Get status of Camel 
integrations", "sourceClass": 
"org.apache.camel.dsl.jbang.core.commands.process.CamelStatus", "options": [ { 
"names": "--watch", "description": "Execute periodically and showing output 
fullscreen", "javaType": "boolean", "type": "boolean" }, { "names": 
"-h,--help", "description": "Display the help and sub-commands", "javaType": 
"boolean", "type": "boolean" } ], "subcommands": [ { "name": "bean", 
"fullName": "get  [...]
     { "name": "harden", "fullName": "harden", "description": "Suggest security 
hardening for Camel routes using AI\/LLM", "sourceClass": 
"org.apache.camel.dsl.jbang.core.commands.Harden", "options": [ { "names": 
"--api-key", "description": "API key for authentication. Also reads 
OPENAI_API_KEY or LLM_API_KEY env vars", "javaType": "java.lang.String", 
"type": "string" }, { "names": "--api-type", "description": "API type: 'ollama' 
or 'openai' (OpenAI-compatible)", "defaultValue": "ollama", [...]
     { "name": "hawtio", "fullName": "hawtio", "description": "Launch Hawtio 
web console", "sourceClass": 
"org.apache.camel.dsl.jbang.core.commands.process.Hawtio", "options": [ { 
"names": "--host", "description": "Hostname to bind the Hawtio web console to", 
"defaultValue": "127.0.0.1", "javaType": "java.lang.String", "type": "string" 
}, { "names": "--openUrl", "description": "To automatic open Hawtio web console 
in the web browser", "defaultValue": "true", "javaType": "boolean", "type": 
[...]
-    { "name": "infra", "fullName": "infra", "description": "List and Run 
external services for testing and prototyping", "sourceClass": 
"org.apache.camel.dsl.jbang.core.commands.infra.InfraCommand", "options": [ { 
"names": "--json", "description": "Output in JSON Format", "javaType": 
"boolean", "type": "boolean" }, { "names": "-h,--help", "description": "Display 
the help and sub-commands", "javaType": "boolean", "type": "boolean" } ], 
"subcommands": [ { "name": "get", "fullName": "infra  [...]
+    { "name": "infra", "fullName": "infra", "description": "List and Run 
external services for testing and prototyping", "sourceClass": 
"org.apache.camel.dsl.jbang.core.commands.infra.InfraCommand", "options": [ { 
"names": "--json", "description": "Output in JSON Format", "javaType": 
"boolean", "type": "boolean" }, { "names": "-h,--help", "description": "Display 
the help and sub-commands", "javaType": "boolean", "type": "boolean" } ], 
"subcommands": [ { "name": "get", "fullName": "infra  [...]
     { "name": "init", "fullName": "init", "description": "Creates a new Camel 
integration", "sourceClass": "org.apache.camel.dsl.jbang.core.commands.Init", 
"options": [ { "names": "--clean-dir,--clean-directory", "description": 
"Whether to clean directory first (deletes all files in directory)", 
"javaType": "boolean", "type": "boolean" }, { "names": "--dir,--directory", 
"description": "Directory relative path where the new Camel integration will be 
saved", "defaultValue": ".", "javaType" [...]
     { "name": "jolokia", "fullName": "jolokia", "description": "Attach Jolokia 
JVM Agent to a running Camel integration", "sourceClass": 
"org.apache.camel.dsl.jbang.core.commands.process.Jolokia", "options": [ { 
"names": "--port", "description": "To use a specific port number when attaching 
Jolokia JVM Agent (default a free port is found in range 8778-9999)", 
"javaType": "int", "type": "integer" }, { "names": "--stop", "description": 
"Stops the Jolokia JVM Agent in the running Camel inte [...]
     { "name": "log", "fullName": "log", "description": "Tail logs from running 
Camel integrations", "sourceClass": 
"org.apache.camel.dsl.jbang.core.commands.action.CamelLogAction", "options": [ 
{ "names": "--find", "description": "Find and highlight matching text (ignore 
case).", "javaType": "java.lang.String", "type": "string" }, { "names": 
"--follow", "description": "Keep following and outputting new log lines (press 
enter to exit).", "defaultValue": "true", "javaType": "boolean", "typ [...]
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
index 4f5a5ede5b51..02e883ddda72 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
@@ -46,6 +46,7 @@ import 
org.apache.camel.dsl.jbang.core.commands.infra.InfraGet;
 import org.apache.camel.dsl.jbang.core.commands.infra.InfraList;
 import org.apache.camel.dsl.jbang.core.commands.infra.InfraLog;
 import org.apache.camel.dsl.jbang.core.commands.infra.InfraPs;
+import org.apache.camel.dsl.jbang.core.commands.infra.InfraRestart;
 import org.apache.camel.dsl.jbang.core.commands.infra.InfraRun;
 import org.apache.camel.dsl.jbang.core.commands.infra.InfraStop;
 import org.apache.camel.dsl.jbang.core.commands.plugin.PluginAdd;
@@ -187,6 +188,7 @@ public class CamelJBangMain implements Callable<Integer> {
                         .addSubcommand("list", new CommandLine(new 
InfraList(this)))
                         .addSubcommand("log", new CommandLine(new 
InfraLog(this)))
                         .addSubcommand("ps", new CommandLine(new 
InfraPs(this)))
+                        .addSubcommand("restart", new CommandLine(new 
InfraRestart(this)))
                         .addSubcommand("run", new CommandLine(new 
InfraRun(this)))
                         .addSubcommand("stop", new CommandLine(new 
InfraStop(this))))
                 .addSubcommand("init", new CommandLine(new Init(this)))
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraBaseCommand.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraBaseCommand.java
index 45f5e08759d5..ec6e143772de 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraBaseCommand.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraBaseCommand.java
@@ -20,6 +20,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
+import java.nio.file.NoSuchFileException;
 import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -103,6 +104,8 @@ public abstract class InfraBaseCommand extends CamelCommand 
{
                     pids.put(Long.valueOf(pid), pidFile);
                 }
             }
+        } catch (NoSuchFileException e) {
+            // camel directory does not exist yet
         }
 
         return pids;
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraRestart.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraRestart.java
new file mode 100644
index 000000000000..305a83ccbbe2
--- /dev/null
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraRestart.java
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.commands.infra;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
+import picocli.CommandLine;
+
[email protected](name = "restart",
+                     description = "Restarts a running external service", 
sortOptions = false, showDefaultValues = true,
+                     footer = {
+                             "%nExamples:",
+                             "  camel infra restart kafka",
+                             "  camel infra restart kafka --background" })
+public class InfraRestart extends InfraBaseCommand {
+
+    @CommandLine.Parameters(description = "Service name (and optional 
implementation)", arity = "1..2")
+    private List<String> serviceName;
+
+    @CommandLine.Option(names = { "--port" },
+                        description = "Override the default port for the 
service")
+    Integer port;
+
+    @CommandLine.Option(names = { "--log" },
+                        description = "Log container output to console")
+    boolean logToStdout;
+
+    @CommandLine.Option(names = { "--background" }, defaultValue = "false", 
description = "Run in the background")
+    boolean background;
+
+    @CommandLine.Option(names = { "--kill" },
+                        description = "To force killing the process (SIGKILL) 
when stopping")
+    boolean kill;
+
+    public InfraRestart(CamelJBangMain main) {
+        super(main);
+    }
+
+    @Override
+    public Integer doCall() throws Exception {
+        String service = serviceName.get(0);
+
+        // stop any running instance(s) of the service by deleting the pid file
+        Map<Long, Path> pids = findPids(service);
+        for (var entry : pids.entrySet()) {
+            Path pidFile = entry.getValue();
+            if (Files.exists(pidFile)) {
+                printer().println("Stopping external service " + service + " 
(PID: " + entry.getKey() + ")");
+                PathUtils.deleteFile(pidFile);
+            }
+            if (kill) {
+                
ProcessHandle.of(entry.getKey()).ifPresent(ProcessHandle::destroyForcibly);
+            }
+        }
+
+        // wait for the previous instance(s) to shut down before starting 
again to avoid port clashes
+        for (Long pid : pids.keySet()) {
+            ProcessHandle.of(pid).ifPresent(ph -> {
+                try {
+                    ph.onExit().get(10, TimeUnit.SECONDS);
+                } catch (Exception e) {
+                    // ignore and proceed to start the service again
+                }
+            });
+        }
+
+        return startService(service);
+    }
+
+    private Integer startService(String service) throws Exception {
+        InfraRun infraRun = new InfraRun(getMain());
+        infraRun.setServiceName(serviceName);
+        infraRun.port = port;
+        infraRun.setLogToStdout(logToStdout);
+        infraRun.background = background;
+        return infraRun.doCall();
+    }
+
+    public void setServiceName(List<String> serviceName) {
+        this.serviceName = serviceName;
+    }
+
+}
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 d8af5208feb7..2fb05bedc53b 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
@@ -125,7 +125,18 @@ public class InfraRun extends InfraBaseCommand {
             cmds = new 
ArrayList<>(spec.commandLine().getParseResult().originalArgs());
         } else {
             cmds = new ArrayList<>();
+            cmds.add("infra");
             cmds.add("run");
+            if (serviceName != null) {
+                cmds.addAll(serviceName);
+            }
+            if (port != null) {
+                cmds.add("--port");
+                cmds.add(String.valueOf(port));
+            }
+            if (logToStdout) {
+                cmds.add("--log");
+            }
         }
 
         cmds.remove("--background=true");
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraRestartTest.java
 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraRestartTest.java
new file mode 100644
index 000000000000..fbd714cc2347
--- /dev/null
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraRestartTest.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.commands.infra;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+
+import org.apache.camel.dsl.jbang.core.commands.CamelCommandBaseTestSupport;
+import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
+import org.assertj.core.api.Assertions;
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.Test;
+
+public class InfraRestartTest extends CamelCommandBaseTestSupport {
+
+    /**
+     * Asserts that restart delegates to the (foreground) infra run, reusing 
the same reflection path as
+     * {@link InfraTest}.
+     */
+    @Test
+    public void restartService() throws Exception {
+        // doCall() delegates to the blocking infra run, so execute it on a 
new thread.
+        Thread thread = new Thread(() -> {
+            InfraRestart infraRestart = new InfraRestart(new 
CamelJBangMain().withPrinter(printer));
+            infraRestart.setServiceName(List.of("ftp"));
+            try {
+                infraRestart.doCall();
+            } catch (Exception e) {
+                printer.printErr(e);
+                throw new RuntimeException(e);
+            }
+        });
+        thread.start();
+
+        Awaitility.await().untilAsserted(() -> {
+            List<String> lines = printer.getLines();
+            Assertions.assertThat(lines).anyMatch(l -> l.startsWith("Starting 
service ftp"));
+            Assertions.assertThat(lines).contains("Running (use camel infra 
stop ftp to stop the execution)");
+        });
+
+        thread.interrupt();
+    }
+
+    @Test
+    public void restartStopsExistingService() throws Exception {
+        // Long.MAX_VALUE exceeds OS PID limits so it cannot belong to a real 
process.
+        long fakePid = Long.MAX_VALUE;
+        Path camelDir = CommandLineHelper.getCamelDir();
+        Files.createDirectories(camelDir);
+        Path fakePidFile = camelDir.resolve("infra-ftp-" + fakePid + ".json");
+        Files.writeString(fakePidFile, "{}");
+
+        Thread thread = new Thread(() -> {
+            InfraRestart infraRestart = new InfraRestart(new 
CamelJBangMain().withPrinter(printer));
+            infraRestart.setServiceName(List.of("ftp"));
+            try {
+                infraRestart.doCall();
+            } catch (Exception e) {
+                printer.printErr(e);
+                throw new RuntimeException(e);
+            }
+        });
+        thread.start();
+
+        Awaitility.await().untilAsserted(() -> {
+            List<String> lines = printer.getLines();
+            Assertions.assertThat(lines)
+                    .anyMatch(l -> l.equals("Stopping external service ftp 
(PID: " + fakePid + ")"));
+            Assertions.assertThat(lines).anyMatch(l -> l.startsWith("Starting 
service ftp"));
+            Assertions.assertThat(lines).contains("Running (use camel infra 
stop ftp to stop the execution)");
+        });
+
+        thread.interrupt();
+    }
+}

Reply via email to