mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
[dev] support log download
This commit is contained in:
parent
d546b2f814
commit
c074f64d30
@ -136,6 +136,17 @@ public class InstanceLogService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载全部的任务日志文件
|
||||||
|
* @param instanceId 任务实例ID
|
||||||
|
* @return 日志文件
|
||||||
|
* @throws Exception 异常
|
||||||
|
*/
|
||||||
|
public File downloadInstanceLog(long instanceId) throws Exception {
|
||||||
|
Future<File> fileFuture = prepareLogFile(instanceId);
|
||||||
|
return fileFuture.get(1, TimeUnit.MINUTES);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异步准备日志文件
|
* 异步准备日志文件
|
||||||
* @param instanceId 任务实例ID
|
* @param instanceId 任务实例ID
|
||||||
|
@ -18,13 +18,20 @@ import com.github.kfcfans.oms.server.web.response.InstanceLogVO;
|
|||||||
import org.apache.commons.lang3.time.DateFormatUtils;
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.core.io.InputStreamResource;
|
||||||
|
import org.springframework.core.io.UrlResource;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -71,17 +78,7 @@ public class InstanceController {
|
|||||||
@GetMapping("/log")
|
@GetMapping("/log")
|
||||||
public ResultDTO<StringPage> getInstanceLog(Long instanceId, Long index, HttpServletResponse response) {
|
public ResultDTO<StringPage> getInstanceLog(Long instanceId, Long index, HttpServletResponse response) {
|
||||||
|
|
||||||
InstanceInfoDO instanceInfo = instanceInfoRepository.findByInstanceId(instanceId);
|
String targetServer = getTargetServer(instanceId);
|
||||||
if (instanceInfo == null) {
|
|
||||||
return ResultDTO.failed("invalid instanceId: " + instanceId);
|
|
||||||
}
|
|
||||||
|
|
||||||
Optional<AppInfoDO> appInfoOpt = appInfoRepository.findById(instanceInfo.getAppId());
|
|
||||||
if (!appInfoOpt.isPresent()) {
|
|
||||||
return ResultDTO.failed("impossible");
|
|
||||||
}
|
|
||||||
|
|
||||||
String targetServer = appInfoOpt.get().getCurrentServer();
|
|
||||||
|
|
||||||
// 转发HTTP请求(如果使用Akka,则需要传输两次,而转发HTTP请求只需要传输一次"大"数据包)
|
// 转发HTTP请求(如果使用Akka,则需要传输两次,而转发HTTP请求只需要传输一次"大"数据包)
|
||||||
if (!OhMyServer.getActorSystemAddress().equals(targetServer)) {
|
if (!OhMyServer.getActorSystemAddress().equals(targetServer)) {
|
||||||
@ -98,6 +95,35 @@ public class InstanceController {
|
|||||||
return ResultDTO.success(instanceLogService.fetchInstanceLog(instanceId, index));
|
return ResultDTO.success(instanceLogService.fetchInstanceLog(instanceId, index));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/downloadLog")
|
||||||
|
public void downloadLogFile(Long instanceId , HttpServletResponse response) throws Exception {
|
||||||
|
String targetServer = getTargetServer(instanceId);
|
||||||
|
// 转发HTTP请求(如果使用Akka,则需要传输两次,而转发HTTP请求只需要传输一次"大"数据包)
|
||||||
|
if (!OhMyServer.getActorSystemAddress().equals(targetServer)) {
|
||||||
|
String ip = targetServer.split(":")[0];
|
||||||
|
String url = "http://" + ip + ":" + port + "/instance/downloadLog?instanceId=" + instanceId;
|
||||||
|
try {
|
||||||
|
response.sendRedirect(url);
|
||||||
|
}catch (Exception ignore) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = instanceLogService.downloadInstanceLog(instanceId);
|
||||||
|
response.setContentType("application/octet-stream");
|
||||||
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
|
||||||
|
|
||||||
|
byte[] buffer = new byte[4096];
|
||||||
|
try (BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
|
||||||
|
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
|
||||||
|
|
||||||
|
while (bis.read(buffer) != -1) {
|
||||||
|
bos.write(buffer);
|
||||||
|
}
|
||||||
|
}catch (IOException ignore) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@PostMapping("/list")
|
@PostMapping("/list")
|
||||||
public ResultDTO<PageResult<InstanceLogVO>> list(@RequestBody QueryInstanceRequest request) {
|
public ResultDTO<PageResult<InstanceLogVO>> list(@RequestBody QueryInstanceRequest request) {
|
||||||
|
|
||||||
@ -151,4 +177,19 @@ public class InstanceController {
|
|||||||
pageResult.setData(content);
|
pageResult.setData(content);
|
||||||
return pageResult;
|
return pageResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取该 instanceId 对应的服务器地址
|
||||||
|
* @param instanceId 任务实例ID
|
||||||
|
* @return 对应服务器地址
|
||||||
|
*/
|
||||||
|
private String getTargetServer(Long instanceId) {
|
||||||
|
InstanceInfoDO instanceInfo = instanceInfoRepository.findByInstanceId(instanceId);
|
||||||
|
if (instanceInfo == null) {
|
||||||
|
throw new RuntimeException("invalid instanceId: " + instanceId);
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<AppInfoDO> appInfoOpt = appInfoRepository.findById(instanceInfo.getAppId());
|
||||||
|
return appInfoOpt.orElseThrow(() -> new RuntimeException("impossible")).getCurrentServer();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user