feat - 添加执行 shell 命令的 web api

This commit is contained in:
wangli 2024-10-22 15:49:30 +08:00
parent ee87f42882
commit 9069c732d0
2 changed files with 131 additions and 50 deletions

View File

@ -0,0 +1,124 @@
package cn.axzo.workflow.server.common.util;
import cn.hutool.core.date.DateUtil;
import liquibase.pro.packaged.D;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Objects;
/**
* TODO
*
* @author wangli
* @since 2024-10-22 15:05
*/
@Slf4j
public class ShellUtil {
private ShellUtil() {
}
public static String grepDateLog(String profile, String keyword, String dateStr) {
return grepLog(profile, keyword, DateUtil.parseDate(dateStr), null);
}
public static String grepDateLog(String profile, String keyword, Date date) {
return grepLog(profile, keyword, date, null);
}
public static String grepTodayLog(String profile, String keyword) {
return grepLog(profile, keyword, new Date(), null);
}
/**
* 入参示例 zgrep '9812ea104cf643c4908e772a46abac17' workflowEngine.log | grep 'checkDeath()'
* @param profile 环境
* @param keyword 具体的命令
* @return
*/
public static String grepRealTimeLog(String profile, String keyword) {
return grepLog(profile, keyword, null, null);
}
/**
*
* @param profile
* @param keyword
* @param date
* @param grepKeyword
* @return
*/
public static String grepLog(String profile, String keyword, Date date, String grepKeyword) {
return executeCmd(getLogPath(profile) + " && zgrep '" + keyword + "' workflowEngine.log" +
(Objects.nonNull(date) ? "." + DateUtil.formatDate(date) + ".*" : "") +
(StringUtils.hasText(grepKeyword) ? " | grep '" + grepKeyword + "'": "")
);
}
/**
* 入参示例 cd /mnt/app-logdata/java-dev/workflowEngine && zgrep '9812ea104cf643c4908e772a46abac17' workflowEngine.log | grep 'checkDeath()'
* @param fullCommand
* @return
*/
private static String executeCmd(String fullCommand) {
if (!StringUtils.hasText(fullCommand)) {
return "命令不能为空";
}
try {
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", fullCommand);
Process process = pb.start();
InputStream inputStream = process.getInputStream();
InputStream errorStream = process.getErrorStream();
StringBuilder outputBuilder = new StringBuilder();
// 读取命令输出的线程
Thread outputReader = new Thread(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
outputBuilder.append(line).append("\n");
}
} catch (IOException e) {
log.warn("outputReader 发生异常: {}", e.getMessage(), e);
}
});
outputReader.start();
// 读取命令错误输出的线程
Thread errorReader = new Thread(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(errorStream))) {
String line;
while ((line = reader.readLine()) != null) {
outputBuilder.append("Error: ").append(line).append("\n");
}
} catch (IOException e) {
log.warn("errorReader 发生异常: {}", e.getMessage(), e);
}
});
errorReader.start();
// 等待命令执行完成
outputReader.join();
errorReader.join();
return outputBuilder.toString();
} catch (IOException | InterruptedException e) {
log.error("Execute command 发生异常: {}", e.getMessage(), e);
return "Error executing command.";
}
}
public static String getLogPath(String profile) {
String logSegment = profile;
if (Objects.equals("dev", profile)) {
logSegment = "java-dev";
}
return "/mnt/app-logdata/" + logSegment + "/workflowEngine/";
}
}

View File

@ -8,6 +8,7 @@ import cn.axzo.workflow.common.model.response.bpmn.process.ProcessNodeDetailVO;
import cn.axzo.workflow.core.service.BpmnProcessInstanceService;
import cn.axzo.workflow.core.service.support.FlowNodeForecastService;
import cn.axzo.workflow.server.common.annotation.RepeatSubmit;
import cn.axzo.workflow.server.common.util.ShellUtil;
import cn.azxo.framework.common.model.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.FlowElement;
@ -18,6 +19,7 @@ import org.flowable.engine.RuntimeService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
@ -25,12 +27,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
@ -50,7 +47,8 @@ import static cn.axzo.workflow.common.enums.BpmnProcessInstanceResultEnum.PROCES
@RestController
@Validated
public class TestController {
@Value("${spring.profiles.active}")
private String profile;
@Autowired
private FlowNodeForecastService forecastService;
@Autowired
@ -250,51 +248,10 @@ public class TestController {
}*/
@GetMapping("log")
public String log(@RequestParam String cmdStr) throws Exception {
if(!StringUtils.hasText(cmdStr)) {
public String log(@RequestParam String keyword) throws Exception {
if (!StringUtils.hasText(keyword)) {
return "命令不能为空";
}
try {
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", cmdStr);
Process process = pb.start();
InputStream inputStream = process.getInputStream();
InputStream errorStream = process.getErrorStream();
StringBuilder outputBuilder = new StringBuilder();
// 读取命令输出的线程
Thread outputReader = new Thread(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine())!= null) {
outputBuilder.append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
});
outputReader.start();
// 读取命令错误输出的线程
Thread errorReader = new Thread(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(errorStream))) {
String line;
while ((line = reader.readLine())!= null) {
outputBuilder.append("Error: ").append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
});
errorReader.start();
// 等待命令执行完成
outputReader.join();
errorReader.join();
return outputBuilder.toString();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
return "Error executing command.";
}
return ShellUtil.grepRealTimeLog(profile, keyword);
}
}