fix: fetch instance log failed when server use different http port

This commit is contained in:
tjq 2020-12-19 21:49:41 +08:00
parent d799c0f2bd
commit 977b8bfd4b
5 changed files with 32 additions and 32 deletions

View File

@ -24,8 +24,8 @@ public enum InstanceStatus {
CANCELED(9, "取消"),
STOPPED(10, "手动停止");
private int v;
private String des;
private final int v;
private final String des;
// 广义的运行状态
public static final List<Integer> generalizedRunningStatus = Lists.newArrayList(WAITING_DISPATCH.v, WAITING_WORKER_RECEIVE.v, RUNNING.v);

View File

@ -81,6 +81,7 @@ public class DispatchService {
if (maxInstanceNum > 0) {
// 这个 runningInstanceCount 已经包含了本 instance
// 不统计 WAITING_DISPATCH 的状态使用 OpenAPI 触发的延迟任务显然不应该统计进去比如 delay 1
long runningInstanceCount = instanceInfoRepository.countByJobIdAndStatusIn(jobId, Lists.newArrayList(WAITING_WORKER_RECEIVE.getV(), RUNNING.getV()));
// 超出最大同时运行限制不执行调度
if (runningInstanceCount > maxInstanceNum) {

View File

@ -4,7 +4,9 @@ import com.github.kfcfans.powerjob.common.OmsConstant;
import com.github.kfcfans.powerjob.common.TimeExpressionType;
import com.github.kfcfans.powerjob.common.model.InstanceLogContent;
import com.github.kfcfans.powerjob.common.utils.CommonUtils;
import com.github.kfcfans.powerjob.common.utils.NetUtils;
import com.github.kfcfans.powerjob.common.utils.SegmentLock;
import com.github.kfcfans.powerjob.server.common.redirect.DesignateServer;
import com.github.kfcfans.powerjob.server.common.utils.OmsFileUtils;
import com.github.kfcfans.powerjob.server.persistence.StringPage;
import com.github.kfcfans.powerjob.server.persistence.core.model.JobInfoDO;
@ -21,6 +23,7 @@ import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@ -45,6 +48,9 @@ import java.util.stream.Stream;
@Service
public class InstanceLogService {
@Value("${server.port}")
private int port;
@Resource
private InstanceMetadataService instanceMetadataService;
@Resource
@ -99,11 +105,13 @@ public class InstanceLogService {
/**
* 获取任务实例运行日志默认存在本地数据需要由生成完成请求的路由与转发
* @param appId appIdAOP 专用
* @param instanceId 任务实例ID
* @param index 页码从0开始
* @return 文本字符串
*/
public StringPage fetchInstanceLog(Long instanceId, long index) {
@DesignateServer(appIdParameterName = "appId")
public StringPage fetchInstanceLog(Long appId, Long instanceId, Long index) {
try {
Future<File> fileFuture = prepareLogFile(instanceId);
// 超时并不会打断正在执行的任务
@ -125,7 +133,7 @@ public class InstanceLogService {
++lines;
}
}catch (Exception e) {
log.warn("[InstanceLog-{}] read logFile from disk failed.", instanceId, e);
log.warn("[InstanceLog-{}] read logFile from disk failed for app: {}.", instanceId, appId, e);
return StringPage.simple("oms-server execution exception, caused by " + ExceptionUtils.getRootCauseMessage(e));
}
@ -140,6 +148,19 @@ public class InstanceLogService {
}
}
/**
* 获取日志的下载链接
* @param appId AOP 专用
* @param instanceId 任务实例 ID
* @return 下载链接
*/
@DesignateServer(appIdParameterName = "appId")
public String fetchDownloadUrl(Long appId, Long instanceId) {
String url = "http://" + NetUtils.getLocalHost() + ":" + port + "/instance/downloadLog?instanceId=" + instanceId;
log.info("[InstanceLog-{}] downloadURL for appId[{}]: {}", instanceId, appId, url);
return url;
}
/**
* 下载全部的任务日志文件
* @param instanceId 任务实例ID

View File

@ -3,7 +3,6 @@ package com.github.kfcfans.powerjob.server.web.controller;
import com.github.kfcfans.powerjob.common.InstanceStatus;
import com.github.kfcfans.powerjob.common.PowerJobException;
import com.github.kfcfans.powerjob.common.response.ResultDTO;
import com.github.kfcfans.powerjob.server.akka.OhMyServer;
import com.github.kfcfans.powerjob.server.common.utils.OmsFileUtils;
import com.github.kfcfans.powerjob.server.persistence.PageResult;
import com.github.kfcfans.powerjob.server.persistence.StringPage;
@ -19,7 +18,6 @@ import com.github.kfcfans.powerjob.server.web.response.InstanceDetailVO;
import com.github.kfcfans.powerjob.server.web.response.InstanceInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
@ -46,8 +44,7 @@ import java.util.stream.Collectors;
@RequestMapping("/instance")
public class InstanceController {
@Value("${server.port}")
private int port;
@Resource
private InstanceService instanceService;
@ -79,32 +76,13 @@ public class InstanceController {
}
@GetMapping("/log")
public ResultDTO<StringPage> getInstanceLog(Long instanceId, Long index, HttpServletResponse response) {
String targetServer = getTargetServer(instanceId);
// 转发HTTP请求如果使用Akka则需要传输两次而转发HTTP请求只需要传输一次""数据包
if (!OhMyServer.getActorSystemAddress().equals(targetServer)) {
String ip = targetServer.split(":")[0];
String url = String.format("http://%s:%s/instance/log?instanceId=%d&index=%d", ip, port, instanceId, index);
try {
response.sendRedirect(url);
return ResultDTO.success(StringPage.simple("redirecting..."));
}catch (Exception e) {
log.warn("[Instance-{}] redirect request to url[{}] failed, please ensure all server has the same http port!", instanceId, url, e);
return ResultDTO.failed(e);
}
}
return ResultDTO.success(instanceLogService.fetchInstanceLog(instanceId, index));
public ResultDTO<StringPage> getInstanceLog(Long appId, Long instanceId, Long index) {
return ResultDTO.success(instanceLogService.fetchInstanceLog(appId, instanceId, index));
}
@GetMapping("/downloadLogUrl")
public ResultDTO<String> getDownloadUrl(Long instanceId) {
String targetServer = getTargetServer(instanceId);
String ip = targetServer.split(":")[0];
String url = "http://" + ip + ":" + port + "/instance/downloadLog?instanceId=" + instanceId;
return ResultDTO.success(url);
public ResultDTO<String> getDownloadUrl(Long appId, Long instanceId) {
return ResultDTO.success(instanceLogService.fetchDownloadUrl(appId, instanceId));
}
@GetMapping("/downloadLog")

File diff suppressed because one or more lines are too long