Skip to content

Commit

Permalink
Use ProcessTree.killAll from InboundAgentRule
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Jul 5, 2024
1 parent 3d03cac commit c7755e0
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/jvnet/hudson/test/InboundAgentRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import hudson.slaves.JNLPLauncher;
import hudson.slaves.RetentionStrategy;
import hudson.slaves.SlaveComputer;
import hudson.util.ProcessTree;
import hudson.util.StreamCopyThread;
import hudson.util.VersionNumber;
import java.io.File;
Expand All @@ -46,6 +47,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -77,6 +79,7 @@ public final class InboundAgentRule extends ExternalResource {

private static final Logger LOGGER = Logger.getLogger(InboundAgentRule.class.getName());

private final String id = UUID.randomUUID().toString();
private final ConcurrentMap<String, Process> procs = new ConcurrentHashMap<>();

/**
Expand Down Expand Up @@ -348,6 +351,8 @@ public void start(AgentArguments agentArguments, Options options) throws Excepti
cmd.addAll(agentArguments.commandLineArgs);
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true);
pb.environment().put("INBOUND_AGENT_RULE_ID", id);
pb.environment().put("INBOUND_AGENT_RULE_NAME", options.getName());
LOGGER.info(() -> "Running: " + pb.command());
Process proc = pb.start();
procs.put(options.getName(), proc);
Expand Down Expand Up @@ -391,6 +396,7 @@ public void stop(@NonNull RealJenkinsRule rjr, @NonNull String name) throws Thro
public void stop(@NonNull String name) throws InterruptedException {
Process proc = procs.remove(name);
if (proc != null) {
ProcessTree.get().killAll(proc, Map.of("INBOUND_AGENT_RULE_ID", id, "INBOUND_AGENT_RULE_NAME", name));
proc.destroyForcibly();
proc.waitFor();
}
Expand Down

0 comments on commit c7755e0

Please sign in to comment.