新增:hpc动态配置升级;hpc滚动日志接口增加

This commit is contained in:
2026-03-06 11:15:21 +08:00
parent 9491e7a7a6
commit 6f798612a0
7 changed files with 277 additions and 31 deletions

View File

@@ -26,12 +26,13 @@ import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.channels.Channels;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.*;
@Slf4j
@Component
@@ -61,6 +62,13 @@ public class HpcCommandExcuteUtil {
@Value("${hpc.delHpcJobsUrl:}")
private String delHpcJobsUrl;
// hpc pack 滚动日志查询
@Value("${hpc.streamlogUrl:http://127.0.0.1:9098/streamLog/%s?filePath=%s}")
private String streamlogUrl;
@Value("${hpc.stopStreamlogUrl:http://127.0.0.1:9098/stopLog/%s}")
private String stopStreamlogUrl;
@Autowired
private HttpClientUtil httpClientUtil;
@@ -262,4 +270,117 @@ public class HpcCommandExcuteUtil {
}
}
/**
* 代理日志流请求适配filePath为查询参数的接口
* @param filePath 日志文件路径需URL编码
* @param clientToken 客户端标识(路径参数)
* @param outputStream 响应输出流
*/
public void proxyStreamLog(String filePath, String clientToken, OutputStream outputStream) {
HttpURLConnection conn = null;
InputStream inputStream = null;
try {
// ========== 关键修改2对filePath做URL编码避免特殊字符问题 ==========
String encodedFilePath = URLEncoder.encode(filePath, StandardCharsets.UTF_8.name());
// 拼接URL第一个占位符是clientToken第二个是编码后的filePath
String reqUrl = String.format(streamlogUrl, clientToken, encodedFilePath);
URL url = new URL(reqUrl);
System.out.println("代理日志请求URL" + reqUrl); // 调试用,可删除
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", MediaType.TEXT_EVENT_STREAM_VALUE);
conn.setRequestProperty("Connection", "keep-alive");
conn.setReadTimeout(0); // 禁用读取超时,保持长连接
conn.connect();
inputStream = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
String line;
// 逐行读取上游日志并透传给前端
while ((line = reader.readLine()) != null) {
try {
// SSE规范要求每行以"data: "开头,结尾加"\n\n"(按需调整,根据上游返回格式)
writer.write("data: " + line + "\n\n");
writer.flush();
} catch (IOException e) {
System.out.println("前端断开,终止任务[" + filePath + "]日志透传");
break;
}
}
} catch (Exception e) {
// 异常时向前端返回错误日志符合SSE格式
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
writer.write("data: [ERROR] 连接Windows日志服务失败" + e.getMessage() + "\n\n");
writer.flush();
} catch (IOException ex) {
System.out.println("向前端写入错误日志失败:" + ex.getMessage());
}
} finally {
// 资源释放完善try-catch避免空指针
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
System.out.println("关闭输入流失败:" + e.getMessage());
}
}
if (conn != null) {
conn.disconnect();
}
}
}
public SdmResponse<String> stopStreamTaskLog(String clientToken) {
String msg="";
boolean stop = false;
try {
String reqUrl = String.format(stopStreamlogUrl, clientToken);
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(3000);
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
conn.disconnect();
// 解析响应
Map<String, Object> result = new HashMap<>();
String json = sb.toString().replace("{", "").replace("}", "").replace("\"", "");
String[] pairs = json.split(",");
for (String pair : pairs) {
String[] kv = pair.split(":");
if (kv.length == 2) {
String key = kv[0].trim();
String value = kv[1].trim();
if (key.equals("success")) {
boolean b = Boolean.parseBoolean(value);
result.put(key, b);
stop = b;
} else if (key.equals("code")) {
result.put(key, Integer.parseInt(value));
} else {
result.put(key, value);
msg = value;
}
}
}
} catch (Exception e) {
msg="关闭日志失败:" + e.getMessage();
}
if (stop) {
return SdmResponse.success(msg);
}else{
return SdmResponse.failed(msg);
}
}
}