mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
[fix] fix aop bug which will lead to online log function un reachable
This commit is contained in:
parent
bd915867e1
commit
915e619147
@ -1,7 +1,9 @@
|
|||||||
package com.github.kfcfans.oms.server.web;
|
package com.github.kfcfans.oms.server.web;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
import org.aspectj.lang.JoinPoint;
|
import org.aspectj.lang.JoinPoint;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
import org.aspectj.lang.annotation.Before;
|
import org.aspectj.lang.annotation.Before;
|
||||||
@ -11,6 +13,8 @@ import org.springframework.web.context.request.RequestContextHolder;
|
|||||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用AOP记录访问日志
|
* 使用AOP记录访问日志
|
||||||
@ -46,20 +50,44 @@ public class WebLogAspect {
|
|||||||
|
|
||||||
@Before("webLog()")
|
@Before("webLog()")
|
||||||
public void doBefore(JoinPoint joinPoint) {
|
public void doBefore(JoinPoint joinPoint) {
|
||||||
// 获取请求域
|
try {
|
||||||
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
// 获取请求域
|
||||||
if (requestAttributes == null) {
|
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||||
return;
|
if (requestAttributes == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
HttpServletRequest request = requestAttributes.getRequest();
|
||||||
|
|
||||||
|
String[] classNameSplit = joinPoint.getSignature().getDeclaringTypeName().split("\\.");
|
||||||
|
String classNameMini = classNameSplit[classNameSplit.length - 1];
|
||||||
|
String classMethod = classNameMini + "." + joinPoint.getSignature().getName();
|
||||||
|
|
||||||
|
// 排除特殊类
|
||||||
|
|
||||||
|
// 192.168.1.1|POST|com.xxx.xxx.save|请求参数
|
||||||
|
log.info("{}|{}|{}|{}", request.getRemoteAddr(), request.getMethod(), classMethod, stringify(joinPoint.getArgs()));
|
||||||
|
}catch (Exception e) {
|
||||||
|
// just for safe
|
||||||
|
log.error("[WebLogAspect] aop occur exception, please concat @KFCFans to fix the bug!", e);
|
||||||
}
|
}
|
||||||
HttpServletRequest request = requestAttributes.getRequest();
|
}
|
||||||
|
|
||||||
String[] classNameSplit = joinPoint.getSignature().getDeclaringTypeName().split("\\.");
|
/**
|
||||||
String classNameMini = classNameSplit[classNameSplit.length - 1];
|
* 序列化请求对象,需要特殊处理无法序列化的对象(HttpServletRequest/HttpServletResponse)
|
||||||
String classMethod = classNameMini + "." + joinPoint.getSignature().getName();
|
* @param args Web请求参数
|
||||||
|
* @return JSON字符串
|
||||||
// 排除特殊类
|
*/
|
||||||
|
private static String stringify(Object[] args) {
|
||||||
// 192.168.1.1|POST|com.xxx.xxx.save|请求参数
|
if (ArrayUtils.isEmpty(args)) {
|
||||||
log.info("{}|{}|{}|{}", request.getRemoteAddr(), request.getMethod(), classMethod, JSONObject.toJSONString(joinPoint.getArgs()));
|
return null;
|
||||||
|
}
|
||||||
|
List<Object> objList = Lists.newLinkedList();
|
||||||
|
for (Object obj : args) {
|
||||||
|
if (obj instanceof HttpServletRequest || obj instanceof HttpServletResponse) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
objList.add(obj);
|
||||||
|
}
|
||||||
|
return JSONObject.toJSONString(objList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
package com.github.kfcfans.oms.server.web.controller;
|
package com.github.kfcfans.oms.server.web.controller;
|
||||||
|
|
||||||
import com.github.kfcfans.oms.common.InstanceStatus;
|
|
||||||
import com.github.kfcfans.oms.common.OmsConstant;
|
|
||||||
import com.github.kfcfans.oms.common.response.ResultDTO;
|
|
||||||
import com.github.kfcfans.oms.common.model.InstanceDetail;
|
import com.github.kfcfans.oms.common.model.InstanceDetail;
|
||||||
|
import com.github.kfcfans.oms.common.response.ResultDTO;
|
||||||
import com.github.kfcfans.oms.server.akka.OhMyServer;
|
import com.github.kfcfans.oms.server.akka.OhMyServer;
|
||||||
import com.github.kfcfans.oms.server.common.utils.OmsFileUtils;
|
import com.github.kfcfans.oms.server.common.utils.OmsFileUtils;
|
||||||
import com.github.kfcfans.oms.server.persistence.PageResult;
|
import com.github.kfcfans.oms.server.persistence.PageResult;
|
||||||
@ -17,7 +15,6 @@ import com.github.kfcfans.oms.server.service.InstanceLogService;
|
|||||||
import com.github.kfcfans.oms.server.service.instance.InstanceService;
|
import com.github.kfcfans.oms.server.service.instance.InstanceService;
|
||||||
import com.github.kfcfans.oms.server.web.request.QueryInstanceRequest;
|
import com.github.kfcfans.oms.server.web.request.QueryInstanceRequest;
|
||||||
import com.github.kfcfans.oms.server.web.response.InstanceInfoVO;
|
import com.github.kfcfans.oms.server.web.response.InstanceInfoVO;
|
||||||
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.data.domain.Example;
|
import org.springframework.data.domain.Example;
|
||||||
@ -28,7 +25,7 @@ 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.io.File;
|
||||||
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;
|
||||||
@ -65,8 +62,8 @@ public class InstanceController {
|
|||||||
return ResultDTO.success(null);
|
return ResultDTO.success(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/status")
|
@GetMapping("/detail")
|
||||||
public ResultDTO<InstanceDetail> getRunningStatus(String instanceId) {
|
public ResultDTO<InstanceDetail> getInstanceDetail(String instanceId) {
|
||||||
return ResultDTO.success(instanceService.getInstanceDetail(Long.valueOf(instanceId)));
|
return ResultDTO.success(instanceService.getInstanceDetail(Long.valueOf(instanceId)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,38 +117,8 @@ public class InstanceController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private PageResult<InstanceInfoVO> convertPage(Page<InstanceInfoDO> page) {
|
private PageResult<InstanceInfoVO> convertPage(Page<InstanceInfoDO> page) {
|
||||||
List<InstanceInfoVO> content = page.getContent().stream().map(instanceLogDO -> {
|
List<InstanceInfoVO> content = page.getContent().stream()
|
||||||
InstanceInfoVO instanceInfoVO = new InstanceInfoVO();
|
.map(x -> InstanceInfoVO.from(x, cacheService.getJobName(x.getJobId()))).collect(Collectors.toList());
|
||||||
BeanUtils.copyProperties(instanceLogDO, instanceInfoVO);
|
|
||||||
|
|
||||||
// 状态转化为中文
|
|
||||||
instanceInfoVO.setStatusStr(InstanceStatus.of(instanceLogDO.getStatus()).getDes());
|
|
||||||
// 额外设置任务名称,提高可读性
|
|
||||||
instanceInfoVO.setJobName(cacheService.getJobName(instanceLogDO.getJobId()));
|
|
||||||
|
|
||||||
// ID 转化为 String(JS精度丢失)
|
|
||||||
instanceInfoVO.setJobId(instanceLogDO.getJobId().toString());
|
|
||||||
instanceInfoVO.setInstanceId(instanceLogDO.getInstanceId().toString());
|
|
||||||
if (instanceLogDO.getWfInstanceId() == null) {
|
|
||||||
instanceInfoVO.setWfInstanceId(OmsConstant.NONE);
|
|
||||||
}else {
|
|
||||||
instanceInfoVO.setWfInstanceId(String.valueOf(instanceLogDO.getWfInstanceId()));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 格式化时间
|
|
||||||
if (instanceLogDO.getActualTriggerTime() == null) {
|
|
||||||
instanceInfoVO.setActualTriggerTime(OmsConstant.NONE);
|
|
||||||
}else {
|
|
||||||
instanceInfoVO.setActualTriggerTime(DateFormatUtils.format(instanceLogDO.getActualTriggerTime(), OmsConstant.TIME_PATTERN));
|
|
||||||
}
|
|
||||||
if (instanceLogDO.getFinishedTime() == null) {
|
|
||||||
instanceInfoVO.setFinishedTime(OmsConstant.NONE);
|
|
||||||
}else {
|
|
||||||
instanceInfoVO.setFinishedTime(DateFormatUtils.format(instanceLogDO.getFinishedTime(), OmsConstant.TIME_PATTERN));
|
|
||||||
}
|
|
||||||
|
|
||||||
return instanceInfoVO;
|
|
||||||
}).collect(Collectors.toList());
|
|
||||||
|
|
||||||
PageResult<InstanceInfoVO> pageResult = new PageResult<>(page);
|
PageResult<InstanceInfoVO> pageResult = new PageResult<>(page);
|
||||||
pageResult.setData(content);
|
pageResult.setData(content);
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
package com.github.kfcfans.oms.server.web.response;
|
package com.github.kfcfans.oms.server.web.response;
|
||||||
|
|
||||||
|
import com.github.kfcfans.oms.common.InstanceStatus;
|
||||||
|
import com.github.kfcfans.oms.common.OmsConstant;
|
||||||
|
import com.github.kfcfans.oms.server.persistence.core.model.InstanceInfoDO;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
||||||
|
import org.springframework.beans.BeanUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* InstanceInfo 对外展示对象
|
* InstanceInfo 对外展示对象
|
||||||
@ -36,4 +41,37 @@ public class InstanceInfoVO {
|
|||||||
private String actualTriggerTime;
|
private String actualTriggerTime;
|
||||||
// 结束时间(同理,需要格式化)
|
// 结束时间(同理,需要格式化)
|
||||||
private String finishedTime;
|
private String finishedTime;
|
||||||
|
|
||||||
|
public static InstanceInfoVO from(InstanceInfoDO instanceInfoDo, String jobName) {
|
||||||
|
InstanceInfoVO instanceInfoVO = new InstanceInfoVO();
|
||||||
|
BeanUtils.copyProperties(instanceInfoDo, instanceInfoVO);
|
||||||
|
|
||||||
|
// 状态转化为中文
|
||||||
|
instanceInfoVO.setStatusStr(InstanceStatus.of(instanceInfoDo.getStatus()).getDes());
|
||||||
|
// 额外设置任务名称,提高可读性
|
||||||
|
instanceInfoVO.setJobName(jobName);
|
||||||
|
|
||||||
|
// ID 转化为 String(JS精度丢失)
|
||||||
|
instanceInfoVO.setJobId(instanceInfoDo.getJobId().toString());
|
||||||
|
instanceInfoVO.setInstanceId(instanceInfoDo.getInstanceId().toString());
|
||||||
|
if (instanceInfoDo.getWfInstanceId() == null) {
|
||||||
|
instanceInfoVO.setWfInstanceId(OmsConstant.NONE);
|
||||||
|
}else {
|
||||||
|
instanceInfoVO.setWfInstanceId(String.valueOf(instanceInfoDo.getWfInstanceId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 格式化时间
|
||||||
|
if (instanceInfoDo.getActualTriggerTime() == null) {
|
||||||
|
instanceInfoVO.setActualTriggerTime(OmsConstant.NONE);
|
||||||
|
}else {
|
||||||
|
instanceInfoVO.setActualTriggerTime(DateFormatUtils.format(instanceInfoDo.getActualTriggerTime(), OmsConstant.TIME_PATTERN));
|
||||||
|
}
|
||||||
|
if (instanceInfoDo.getFinishedTime() == null) {
|
||||||
|
instanceInfoVO.setFinishedTime(OmsConstant.NONE);
|
||||||
|
}else {
|
||||||
|
instanceInfoVO.setFinishedTime(DateFormatUtils.format(instanceInfoDo.getFinishedTime(), OmsConstant.TIME_PATTERN));
|
||||||
|
}
|
||||||
|
|
||||||
|
return instanceInfoVO;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user