From c074f64d3035e327d323ecd13370f6788474a5eb Mon Sep 17 00:00:00 2001 From: tjq Date: Thu, 7 May 2020 17:16:55 +0800 Subject: [PATCH] [dev] support log download --- .../server/service/InstanceLogService.java | 11 ++++ .../web/controller/InstanceController.java | 63 +++++++++++++++---- 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/service/InstanceLogService.java b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/service/InstanceLogService.java index 8cddda28..86adbbc9 100644 --- a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/service/InstanceLogService.java +++ b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/service/InstanceLogService.java @@ -136,6 +136,17 @@ public class InstanceLogService { } } + /** + * 下载全部的任务日志文件 + * @param instanceId 任务实例ID + * @return 日志文件 + * @throws Exception 异常 + */ + public File downloadInstanceLog(long instanceId) throws Exception { + Future fileFuture = prepareLogFile(instanceId); + return fileFuture.get(1, TimeUnit.MINUTES); + } + /** * 异步准备日志文件 * @param instanceId 任务实例ID diff --git a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/web/controller/InstanceController.java b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/web/controller/InstanceController.java index 2b4d3223..2cbde1a8 100644 --- a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/web/controller/InstanceController.java +++ b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/web/controller/InstanceController.java @@ -18,13 +18,20 @@ import com.github.kfcfans.oms.server.web.response.InstanceLogVO; import org.apache.commons.lang3.time.DateFormatUtils; import org.springframework.beans.BeanUtils; 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.PageRequest; import org.springframework.data.domain.Sort; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -71,17 +78,7 @@ public class InstanceController { @GetMapping("/log") public ResultDTO getInstanceLog(Long instanceId, Long index, HttpServletResponse response) { - InstanceInfoDO instanceInfo = instanceInfoRepository.findByInstanceId(instanceId); - if (instanceInfo == null) { - return ResultDTO.failed("invalid instanceId: " + instanceId); - } - - Optional appInfoOpt = appInfoRepository.findById(instanceInfo.getAppId()); - if (!appInfoOpt.isPresent()) { - return ResultDTO.failed("impossible"); - } - - String targetServer = appInfoOpt.get().getCurrentServer(); + String targetServer = getTargetServer(instanceId); // 转发HTTP请求(如果使用Akka,则需要传输两次,而转发HTTP请求只需要传输一次"大"数据包) if (!OhMyServer.getActorSystemAddress().equals(targetServer)) { @@ -98,6 +95,35 @@ public class InstanceController { 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") public ResultDTO> list(@RequestBody QueryInstanceRequest request) { @@ -151,4 +177,19 @@ public class InstanceController { pageResult.setData(content); 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 appInfoOpt = appInfoRepository.findById(instanceInfo.getAppId()); + return appInfoOpt.orElseThrow(() -> new RuntimeException("impossible")).getCurrentServer(); + } }