Skip to content

Commit

Permalink
Solved problem with empty lines and wrong pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
jlolling committed Jun 19, 2024
1 parent 589628c commit 22ab8e7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.cimt.talendcomp</groupId>
<artifactId>cimt-talendcomp-jobinstance</artifactId>
<version>8.10</version>
<version>8.11</version>
<organization>
<name>cimt AG</name>
</organization>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2020 Jan Lolling jan.lolling@gmail.com
* Copyright 2024 Jan Lolling jan.lolling@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1544,8 +1544,8 @@ public void setProcessInstanceId(Long processInstanceId) {
private int countBrokenInstances = 0;
private Date lastSystemStart = null;

private String unixCommand = "ps -eo pid";
private String unixPidPattern = "[0-9]{1,8}";
private String unixCommand = "ps -eo pid=";
private String unixPidPattern = "([0-9]{1,8})";
private String windowsCommand = "tasklist /fo list";
private String windowsPidPattern = "PID[:\\s]*([0-9]{1,6})";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public class ProcessHelper {
private static Logger LOG = LogManager.getLogger(ProcessHelper.class);
private boolean isUnix = false;
private boolean isWindows = false;
private String unixCommand = "ps -eo pid";
private String unixPidPattern = "([0-9]{2,9})";
private String unixCommand = "ps -eo pid=";
private String unixPidPattern = "([0-9]{1,9})";
private String windowsCommand = "tasklist /fo list";
private String windowsPidPattern = "PID[:\\s]*([0-9]{2,6})";
private String windowsPidPattern = "PID[:\\s]*([0-9]{1,9})";

public ProcessHelper() {}

Expand Down Expand Up @@ -106,10 +106,13 @@ public List<Integer> retrieveProcessList(String command, String patternStr) thro
line = line.trim();
psCommandResponse.append(line);
psCommandResponse.append("\n");
if (line.isEmpty()) {
continue;
}
Matcher m = patternPid.matcher(line);
if (m.find()) {
if (m.groupCount() == 0) {
throw new Exception("The regex to find the PIDs must have at least one group (only the first group will be used). regex: " + patternStr);
throw new Exception("The regex to find the PIDs must have at least one group (only the first group will be used). regex: " + patternStr + " command: " + command + " current line: " + line);
}
if (m.start() < m.end()) {
String pidStr = m.group(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public void testListProcessesUnixAltCommand() throws Exception {
ProcessHelper ph = new ProcessHelper();
ph.init();
ph.setUnixCommand("ps -eo pid");
ph.setUnixPidPattern("([0-9]{2,9})");
ph.setUnixPidPattern("([0-9]{1,9})");
List<Integer> pidList = ph.retrieveProcessList();
System.out.println("Found: " + pidList.size() + " processes");
assertTrue("Empty list", pidList.size() == 1);
assertTrue("Empty list", pidList.size() > 0);
}

@Test
Expand Down

0 comments on commit 22ab8e7

Please sign in to comment.