Skip to content

Commit

Permalink
⚡update stop shell
Browse files Browse the repository at this point in the history
  • Loading branch information
stelin committed Jul 3, 2023
1 parent ceca8f5 commit a562986
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
server.port=${SERVER_PORT:8080}
spring.profiles.active=pgsql
#spring.profiles.active=pgsql
#spring.profiles.active=oracle
### admin config
# user passwd hash salt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,8 @@ public ProcessResult postProcess(JobContext context) {

@Override
public void stop(JobContext context) {
// Windows
if (ShellTypeEnum.WINDOWS.getType().equals(this.type)) {
Long processPid = -1L;
try {
processPid = this.getProcessPid(this.process);
if (processPid > 0) {
this.stopProcessForWindows(processPid);
}
logger.info("Process stop success! pid={}", processPid);
log.info("Process stop success! pid={}", processPid);
} catch (Throwable throwable) {
logger.error(String.format("Process stop failed! pid=%d", processPid), throwable);
log.error(String.format("Process stop failed! pid=%d", processPid), throwable);
}
}
// Stop child threads
this.stopChildThreads();

try {
// Process
Expand Down Expand Up @@ -159,6 +146,35 @@ protected List<String> parseDefaultCommand(String type) {
return params;
}

/**
* Stop child threads
*/
protected void stopChildThreads() {
Long processPid = -1L;
try {
// Obtain process pid failed!
processPid = this.getProcessPid(this.process);
if (processPid < 0) {
logger.warn("Obtain process pid failed!");
log.warn("Obtain process pid failed!");
return;
}

if (ShellTypeEnum.WINDOWS.getType().equals(this.type)) {
// Windows
this.stopProcessForWindows(processPid);
} else {
// unix
this.stopProcessForUnix(processPid);
}
logger.info("Process stop {} success! pid={}", this.type, processPid);
log.info("Process stop {} success! pid={}", this.type, processPid);
} catch (Throwable throwable) {
logger.error(String.format("Process stop failed! pid=%d", processPid), throwable);
log.error(String.format("Process stop failed! pid=%d", processPid), throwable);
}
}

/**
* Get process pid
*
Expand Down Expand Up @@ -212,8 +228,32 @@ protected void stopProcessForWindows(Long pid) throws IOException {

String line;
while ((line = readerKill.readLine()) != null) {
logger.info("Stop result {}", line);
log.info("Stop result {}", line);
logger.info("Stop windows result {}", line);
log.info("Stop windows result {}", line);
}
}

/**
* Stop process for unix
*
* @param pid pid
* @throws IOException IOException
*/
protected void stopProcessForUnix(Long pid) throws IOException {
List<String> commandKill = new ArrayList<>();
commandKill.add("/bin/sh");
commandKill.add("-c");
commandKill.add("kill");
commandKill.add("-9");
commandKill.add(String.format("-%d", pid));
ProcessBuilder pbk = new ProcessBuilder(commandKill);
Process stopProcess = pbk.start();
BufferedReader readerKill = new BufferedReader(this.getInputStreamReader(stopProcess.getInputStream()));

String line;
while ((line = readerKill.readLine()) != null) {
logger.info("Stop unix result {}", line);
log.info("Stop unix result {}", line);
}
}

Expand Down

0 comments on commit a562986

Please sign in to comment.