From a5629867928b56a080bd75616d4a09ff52765090 Mon Sep 17 00:00:00 2001 From: stelin <794774870@qq.com> Date: Mon, 3 Jul 2023 20:18:36 +0800 Subject: [PATCH] :zap:update stop shell --- .../src/main/resources/application.properties | 2 +- .../worker/processor/ShellProcessor.java | 74 ++++++++++++++----- 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/openjob-server/openjob-server-starter/src/main/resources/application.properties b/openjob-server/openjob-server-starter/src/main/resources/application.properties index 72c76a48..5e17f76f 100644 --- a/openjob-server/openjob-server-starter/src/main/resources/application.properties +++ b/openjob-server/openjob-server-starter/src/main/resources/application.properties @@ -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 diff --git a/openjob-worker/openjob-worker-core/src/main/java/io/openjob/worker/processor/ShellProcessor.java b/openjob-worker/openjob-worker-core/src/main/java/io/openjob/worker/processor/ShellProcessor.java index b41c3fec..83f65f80 100644 --- a/openjob-worker/openjob-worker-core/src/main/java/io/openjob/worker/processor/ShellProcessor.java +++ b/openjob-worker/openjob-worker-core/src/main/java/io/openjob/worker/processor/ShellProcessor.java @@ -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 @@ -159,6 +146,35 @@ protected List 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 * @@ -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 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); } }