[*] 日常优化
This commit is contained in:
@@ -3,6 +3,7 @@ package com.acgist.taoyao.boot.utils;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
@@ -24,6 +25,17 @@ public final class ScriptUtils {
|
|||||||
private ScriptUtils() {
|
private ScriptUtils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行命令
|
||||||
|
*
|
||||||
|
* @param args 命令参数
|
||||||
|
*
|
||||||
|
* @return 执行结果
|
||||||
|
*/
|
||||||
|
public static final ScriptExecutor execute(String ... args) {
|
||||||
|
return ScriptUtils.execute(String.join(" ", args));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行命令
|
* 执行命令
|
||||||
*
|
*
|
||||||
@@ -108,13 +120,15 @@ public final class ScriptUtils {
|
|||||||
}
|
}
|
||||||
log.debug("开始执行命令:{}", this.script);
|
log.debug("开始执行命令:{}", this.script);
|
||||||
this.running = true;
|
this.running = true;
|
||||||
|
final CountDownLatch latch = new CountDownLatch(2);
|
||||||
try (
|
try (
|
||||||
final InputStream input = this.process.getInputStream();
|
final InputStream input = this.process.getInputStream();
|
||||||
final InputStream error = this.process.getErrorStream();
|
final InputStream error = this.process.getErrorStream();
|
||||||
) {
|
) {
|
||||||
this.streamThread(linux, "TaoyaoScriptInput", this.input, input);
|
this.streamThread(linux, "TaoyaoScriptInput", this.input, input, latch);
|
||||||
this.streamThread(linux, "TaoyaoScriptError", this.error, error);
|
this.streamThread(linux, "TaoyaoScriptError", this.error, error, latch);
|
||||||
this.code = this.process.waitFor();
|
this.code = this.process.waitFor();
|
||||||
|
latch.await();
|
||||||
}
|
}
|
||||||
this.running = false;
|
this.running = false;
|
||||||
log.debug("""
|
log.debug("""
|
||||||
@@ -130,8 +144,11 @@ public final class ScriptUtils {
|
|||||||
* @param name 线程名称
|
* @param name 线程名称
|
||||||
* @param builder 日志记录
|
* @param builder 日志记录
|
||||||
* @param input 日志输入流
|
* @param input 日志输入流
|
||||||
|
* @param latch 计数器
|
||||||
|
*
|
||||||
|
* @return 线程
|
||||||
*/
|
*/
|
||||||
private void streamThread(boolean linux, String name, StringBuilder builder, InputStream input) {
|
private Thread streamThread(boolean linux, String name, StringBuilder builder, InputStream input, CountDownLatch latch) {
|
||||||
final Thread streamThread = new Thread(() -> {
|
final Thread streamThread = new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
int length;
|
int length;
|
||||||
@@ -141,11 +158,14 @@ public final class ScriptUtils {
|
|||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("读取执行命令日志异常", e);
|
log.error("读取执行命令日志异常", e);
|
||||||
|
} finally {
|
||||||
|
latch.countDown();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
streamThread.setName(name);
|
streamThread.setName(name);
|
||||||
streamThread.setDaemon(true);
|
streamThread.setDaemon(true);
|
||||||
streamThread.start();
|
streamThread.start();
|
||||||
|
return streamThread;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -173,7 +193,7 @@ public final class ScriptUtils {
|
|||||||
wait = 5000;
|
wait = 5000;
|
||||||
}
|
}
|
||||||
// 等待正常结束
|
// 等待正常结束
|
||||||
while(this.process.isAlive() && wait >= 0) {
|
while(this.process.isAlive() && wait > 0) {
|
||||||
wait -= 10;
|
wait -= 10;
|
||||||
try {
|
try {
|
||||||
Thread.sleep(10);
|
Thread.sleep(10);
|
||||||
@@ -184,11 +204,9 @@ public final class ScriptUtils {
|
|||||||
if(this.process.isAlive()) {
|
if(this.process.isAlive()) {
|
||||||
log.info("强制结束命令:{}", this.script);
|
log.info("强制结束命令:{}", this.script);
|
||||||
// 所有子进程
|
// 所有子进程
|
||||||
this.process.children().forEach(ProcessHandle::destroy);
|
this.process.children().forEach(this::destroy);
|
||||||
// this.process.children().forEach(ProcessHandle::destroyForcibly);
|
|
||||||
// 所有派生进程
|
// 所有派生进程
|
||||||
// this.process.descendants().forEach(ProcessHandle::destroy);
|
this.process.descendants().forEach(this::destroy);
|
||||||
// this.process.descendants().forEach(ProcessHandle::destroyForcibly);
|
|
||||||
// 当前父进程
|
// 当前父进程
|
||||||
this.process.destroy();
|
this.process.destroy();
|
||||||
// this.process.destroyForcibly();
|
// this.process.destroyForcibly();
|
||||||
@@ -201,7 +219,25 @@ public final class ScriptUtils {
|
|||||||
* @return 执行结果
|
* @return 执行结果
|
||||||
*/
|
*/
|
||||||
public String getResult() {
|
public String getResult() {
|
||||||
return this.input.isEmpty() ? this.error.toString() : this.input.toString();
|
if(this.code == 0) {
|
||||||
|
return this.input.toString();
|
||||||
|
} else {
|
||||||
|
return this.error.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 销毁线程
|
||||||
|
*
|
||||||
|
* @param handle 线程
|
||||||
|
*/
|
||||||
|
private void destroy(ProcessHandle handle) {
|
||||||
|
try {
|
||||||
|
handle.destroy();
|
||||||
|
// handle.destroyForcibly();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("销毁线程异常", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user