Skip to content

Commit

Permalink
fix: keploy fails to stop when tests end (#156)
Browse files Browse the repository at this point in the history
Signed-off-by: gouravkrosx <gouravgreatkr@gmail.com>
  • Loading branch information
gouravkrosx authored Sep 28, 2023
1 parent a5cdb51 commit 48d2cfe
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@
<artifactId>progressbar</artifactId>
<version>0.9.5</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.1</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
62 changes: 47 additions & 15 deletions v2/src/main/java/io.keploy.cli/KeployCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class KeployCLI {
Expand Down Expand Up @@ -292,37 +296,65 @@ private static String getSimulateResponseBody(HttpURLConnection conn) throws IOE
return content.toString();
}



public static void StopKeployServer() {
// kprocess.destroy();
killProcessOnPort(serverPort);
}

public static void killProcessOnPort(int port) {
logger.debug("trying to kill process running on port:{}",port);
String command = "lsof -t -i:" + port;

try {
Process process = Runtime.getRuntime().exec(command);
Process process = new ProcessBuilder("sh", "-c", "lsof -t -i:" + port).start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
forceKillProcessByPID(line.trim());
String pids = reader.readLine();
if (pids != null) {
Arrays.stream(pids.split("\n")).forEach(pidStr -> {
if (!pidStr.isEmpty()) {
int pid = Integer.parseInt(pidStr.trim());
killProcessesAndTheirChildren(pid);
}
});
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Failed to fetch the process ID on port " + port);
}
}

public static void killProcessesAndTheirChildren(int parentPID) {
List<Integer> pids = new ArrayList<>();
findAndCollectChildProcesses(String.valueOf(parentPID), pids);
for (int childPID : pids) {
if (childPID != getCurrentPid()) {
try {
new ProcessBuilder("sudo", "kill", "-9", String.valueOf(childPID)).start();
logger.debug("Killed child process " + childPID);
} catch (Exception e) {
e.printStackTrace();
System.err.println("Failed to kill child process " + childPID);
}
}
}
}

private static void forceKillProcessByPID(String pid) {
public static void findAndCollectChildProcesses(String parentPID, List<Integer> pids) {
try {
String cmd = "kill -9 "+pid;
logger.debug("cmd to kill:{}",cmd);
Runtime.getRuntime().exec("kill -9 " + pid);
pids.add(Integer.parseInt(parentPID));
Process process = new ProcessBuilder("pgrep", "-P", parentPID).start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String output = reader.readLine();
if (output != null) {
Arrays.stream(output.split("\n")).forEach(childPID -> {
if (!childPID.isEmpty()) {
findAndCollectChildProcesses(childPID, pids);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}

private static int getCurrentPid() {
String processName = ManagementFactory.getRuntimeMXBean().getName();
return Integer.parseInt(processName.split("@")[0]);
}
}

0 comments on commit 48d2cfe

Please sign in to comment.