|
44 | 44 |
|
45 | 45 | import java.util.ArrayList; |
46 | 46 | import java.util.Arrays; |
| 47 | +import java.util.Collections; |
47 | 48 | import java.util.HashMap; |
48 | 49 | import java.util.List; |
49 | 50 | import java.util.Map; |
@@ -113,15 +114,7 @@ public static boolean kill(@NonNull TaskExecutionContext request) { |
113 | 114 | } |
114 | 115 |
|
115 | 116 | // Get all child processes |
116 | | - String pids = getPidsStr(processId); |
117 | | - String[] pidArray = PID_PATTERN.split(pids); |
118 | | - if (pidArray.length == 0) { |
119 | | - log.warn("No valid PIDs found for process: {}", processId); |
120 | | - return true; |
121 | | - } |
122 | | - |
123 | | - // Convert PID string to list of integers |
124 | | - List<Integer> pidList = Arrays.stream(pidArray).map(Integer::parseInt).collect(Collectors.toList()); |
| 117 | + List<Integer> pidList = getPidList(processId); |
125 | 118 |
|
126 | 119 | // 1. Try to terminate gracefully (SIGINT) |
127 | 120 | boolean gracefulKillSuccess = sendKillSignal("SIGINT", pidList, request.getTenantCode()); |
@@ -251,26 +244,69 @@ private static boolean isProcessAlive(int pid, String tenantCode) { |
251 | 244 | } |
252 | 245 |
|
253 | 246 | /** |
254 | | - * get pids str. |
| 247 | + * Get all descendant process IDs (including the given process) using pstree. |
255 | 248 | * |
256 | | - * @param processId process id |
257 | | - * @return pids pid String |
258 | | - * @throws Exception exception |
| 249 | + * @param processId the root process ID |
| 250 | + * @return list of process IDs; returns empty list if no PIDs found (e.g., process not exists) |
| 251 | + * @throws IllegalArgumentException if any PID is invalid (blank, non-numeric, or non-positive) |
| 252 | + * @throws Exception if command execution fails unexpectedly (e.g., command not found) |
259 | 253 | */ |
260 | | - public static String getPidsStr(int processId) throws Exception { |
261 | | - |
| 254 | + public static List<Integer> getPidList(int processId) throws Exception { |
262 | 255 | String rawPidStr; |
263 | 256 |
|
264 | | - // pstree pid get sub pids |
265 | | - if (SystemUtils.IS_OS_MAC) { |
266 | | - rawPidStr = OSUtils.exeCmd(String.format("%s -sp %d", TaskConstants.PSTREE, processId)); |
267 | | - } else if (SystemUtils.IS_OS_LINUX) { |
268 | | - rawPidStr = OSUtils.exeCmd(String.format("%s -p %d", TaskConstants.PSTREE, processId)); |
269 | | - } else { |
270 | | - rawPidStr = OSUtils.exeCmd(String.format("%s -p %d", TaskConstants.PSTREE, processId)); |
| 257 | + try { |
| 258 | + if (SystemUtils.IS_OS_MAC) { |
| 259 | + rawPidStr = OSUtils.exeCmd(String.format("%s -sp %d", TaskConstants.PSTREE, processId)); |
| 260 | + } else if (SystemUtils.IS_OS_LINUX) { |
| 261 | + rawPidStr = OSUtils.exeCmd(String.format("%s -p %d", TaskConstants.PSTREE, processId)); |
| 262 | + } else { |
| 263 | + log.warn("Unsupported OS for pstree: {}. Attempting generic command.", SystemUtils.OS_NAME); |
| 264 | + rawPidStr = OSUtils.exeCmd(String.format("%s -p %d", TaskConstants.PSTREE, processId)); |
| 265 | + } |
| 266 | + } catch (Exception ex) { |
| 267 | + log.error("Failed to execute 'pstree' command for process ID: {}", processId, ex); |
| 268 | + throw ex; |
| 269 | + } |
| 270 | + |
| 271 | + String pidsStr = parsePidStr(rawPidStr); |
| 272 | + if (StringUtils.isBlank(pidsStr)) { |
| 273 | + log.warn("No PIDs found for process: {}", processId); |
| 274 | + return Collections.emptyList(); |
| 275 | + } |
| 276 | + |
| 277 | + String[] pidArray = PID_PATTERN.split(pidsStr.trim()); |
| 278 | + if (pidArray.length == 0) { |
| 279 | + log.warn("No PIDs parsed for process: {}", processId); |
| 280 | + return Collections.emptyList(); |
| 281 | + } |
| 282 | + |
| 283 | + List<Integer> pidList = new ArrayList<>(); |
| 284 | + for (String pidStr : pidArray) { |
| 285 | + pidStr = pidStr.trim(); |
| 286 | + |
| 287 | + if (StringUtils.isBlank(pidStr)) { |
| 288 | + log.error("Empty or blank PID found in output for process: {}, full PIDs string: {}", processId, |
| 289 | + pidsStr); |
| 290 | + throw new IllegalArgumentException("Empty or blank PID found in output from process: " + processId); |
| 291 | + } |
| 292 | + |
| 293 | + try { |
| 294 | + int pid = Integer.parseInt(pidStr); |
| 295 | + if (pid <= 0) { |
| 296 | + log.error("Invalid PID value (must be positive): {} for process: {}, full PIDs string: {}", |
| 297 | + pidStr, processId, pidsStr); |
| 298 | + throw new IllegalArgumentException("Invalid PID value (must be positive): " + pid); |
| 299 | + } |
| 300 | + pidList.add(pid); |
| 301 | + } catch (NumberFormatException e) { |
| 302 | + log.error("Invalid PID format in output: {} for process: {}, full PIDs string: {}", |
| 303 | + pidStr, processId, pidsStr, e); |
| 304 | + throw new IllegalArgumentException( |
| 305 | + "Invalid PID format in output: '" + pidStr + "' (from process " + processId + ")", e); |
| 306 | + } |
271 | 307 | } |
272 | 308 |
|
273 | | - return parsePidStr(rawPidStr); |
| 309 | + return pidList; |
274 | 310 | } |
275 | 311 |
|
276 | 312 | public static String parsePidStr(String rawPidStr) { |
|
0 commit comments