Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ stopped in time, it exits with code `124`, and `--kill` stops it at once. Use `-
NOTE: The name is matched as a prefix — `camel stop d` stops all integrations starting with _d_ — unless an
integration has exactly that name: with `dude` and `dude2` running, `camel stop dude` stops only `dude`, and
`camel stop dude*` stops both. Append `!` to match only that name (`camel stop dude!`), or give the PID.
A PID must be a running Camel integration, and a name or PID that matches nothing exits with code `3`
A PID can be a running Camel integration or its launcher, such as the JBang launcher of `camel run`, whose pid test
tools like Citrus keep. `camel stop` then stops the Camel integrations the launcher started, without signalling the
launcher. A name or PID that matches nothing exits with code `3`
(see xref:camel-jbang-exit-codes.adoc[Exit Codes and Output]).

Restart an integration (stop + re-launch):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -78,7 +79,10 @@ public Integer doCall() throws Exception {
if (!all && (name == null || name.isBlank())) {
requireOneRunning();
}
List<Long> pids = findIntegrationPids(all ? "*" : name);
List<Long> pids = launchedBy(all ? null : name);
if (pids.isEmpty()) {
pids = findIntegrationPids(all ? "*" : name);
}

for (Long pid : pids) {
Path pidFile = CommandLineHelper.getCamelDir().resolve(Long.toString(pid));
Expand Down Expand Up @@ -139,6 +143,35 @@ private void requireOneRunning() {
}
}

/**
* The running Camel integrations that the process with this pid launched, when the pid is not a Camel integration
* itself: such as the JBang launcher of camel run, whose pid Citrus keeps, while the Camel JVM is its child. Camel
* 4.23 signalled a pid as it was given; since CAMEL-26911 only Camel integrations are signalled, so the
* integrations below the launcher are stopped instead (CAMEL-27149).
*
* @param nameOrPid the argument of camel stop
* @return the pids of the integrations, or an empty list when the argument is not a pid, is a Camel
* integration, or launched none
*/
private List<Long> launchedBy(String nameOrPid) {
if (nameOrPid == null || !nameOrPid.matches("\\d+") || !ProcessFinder.find(nameOrPid).isEmpty()) {
return List.of();
}
long launcher = Long.parseLong(nameOrPid);
Set<Long> descendants = ProcessHandle.of(launcher)
.map(ph -> ph.descendants().map(ProcessHandle::pid).collect(Collectors.toSet()))
.orElse(Set.of());
if (descendants.isEmpty()) {
return List.of();
}
List<Long> pids = ProcessFinder.findAll().stream().map(ProcessFinder.CamelProcess::pid)
.filter(descendants::contains).collect(Collectors.toCollection(ArrayList::new));
if (!pids.isEmpty()) {
printer().println("PID " + launcher + " is not a Camel integration: stopping the Camel integration it launched");
}
return pids;
}

/**
* Waits until the process has exited.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -235,6 +238,69 @@ void testStopAllWithANameIsAUsageError() {
assertEquals(CliExitCode.USAGE, e.getExitCode());
}

@Test
void testStopByThePidOfTheLauncherStopsTheIntegrationItLaunched() throws Exception {
// CAMEL-27149: Citrus keeps the pid of the JBang launcher of camel run, and the Camel JVM is its child
long launcherPid = TEST_PID;
long integrationPid = TEST_PID + 1;
Path pidFile = running(integrationPid, "orders");
StopProcess command = new StopProcess(new CamelJBangMain().withPrinter(printer));
command.name = Long.toString(launcherPid);

try (MockedStatic<ProcessHandle> mocked = mockStatic(ProcessHandle.class)) {
ProcessHandle integration = mockProcessHandle(integrationPid);
ProcessHandle launcher = mockLauncher(launcherPid, integration);
ProcessHandle current = mockCurrentHandle();
mocked.when(ProcessHandle::current).thenReturn(current);
mocked.when(ProcessHandle::allProcesses).thenAnswer(inv -> Stream.of(launcher, integration));
mocked.when(() -> ProcessHandle.of(launcherPid)).thenReturn(Optional.of(launcher));
mocked.when(() -> ProcessHandle.of(integrationPid)).thenReturn(Optional.of(integration));
when(integration.onExit()).thenReturn(CompletableFuture.completedFuture(integration));

assertEquals(0, command.doCall());

assertFalse(Files.exists(pidFile), "the integration is stopped");
assertTrue(printer.getOutput().contains(
"PID 12345 is not a Camel integration: stopping the Camel integration it launched"),
printer.getOutput());
assertTrue(printer.getOutput().contains("Shutting down Camel integration (PID: 12346)"), printer.getOutput());
verify(launcher, never()).destroy();
verify(launcher, never()).destroyForcibly();
}
}

@Test
void testStopByThePidOfAProcessThatLaunchedNoIntegrationFails() throws Exception {
StopProcess command = new StopProcess(new CamelJBangMain().withPrinter(printer));
command.name = Long.toString(TEST_PID);

try (MockedStatic<ProcessHandle> mocked = mockStatic(ProcessHandle.class)) {
// a process that is not a Camel integration, with a child that is not one either
ProcessHandle child = mock(ProcessHandle.class);
lenient().when(child.pid()).thenReturn(TEST_PID + 1);
ProcessHandle other = mockLauncher(TEST_PID, child);
ProcessHandle current = mockCurrentHandle();
mocked.when(ProcessHandle::current).thenReturn(current);
mocked.when(ProcessHandle::allProcesses).thenAnswer(inv -> Stream.of(other));
mocked.when(() -> ProcessHandle.of(TEST_PID)).thenReturn(Optional.of(other));

CliException e = assertThrows(CliException.class, command::doCall);

assertEquals(CliExitCode.NOT_FOUND, e.getExitCode());
assertEquals("No running Camel integration matches: 12345", e.getMessage());
verify(other, never()).destroy();
verify(other, never()).destroyForcibly();
}
}

// a process that is not a Camel integration (no status file), with a child process
private static ProcessHandle mockLauncher(long pid, ProcessHandle child) {
ProcessHandle launcher = mock(ProcessHandle.class);
lenient().when(launcher.pid()).thenReturn(pid);
lenient().when(launcher.descendants()).thenAnswer(inv -> Stream.of(child));
return launcher;
}

// an integration that runs: its status file and its pid file, which camel stop deletes
private static Path running(long pid, String name) throws Exception {
writeStatusFile(pid, buildContextStatus(name, 5));
Expand Down
Loading