[server] 修复bug

This commit is contained in:
zeaslity
2023-02-17 16:24:31 +08:00
parent 9197efc4ec
commit 3c42fff6cf
7 changed files with 154 additions and 59 deletions

View File

@@ -55,6 +55,7 @@ public class OMHandlerExecutor extends AbstractOctopusMessageHandler {
return true; return true;
} }
// 只有 在预先定义的脚本缓存中查找到 才执行 功能类型
String executionType = executionMessage.getType(); String executionType = executionMessage.getType();
if (ALL_FUNCTION_MAP.containsKey(executionType)) { if (ALL_FUNCTION_MAP.containsKey(executionType)) {
// execute the exist function // execute the exist function

View File

@@ -42,14 +42,23 @@ public class GlobalExceptionHandler {
public R<Object> MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) { public R<Object> MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
// 从异常对象中拿到ObjectError对象获取所有的错误信息 // 从异常对象中拿到ObjectError对象获取所有的错误信息
List<ObjectError> objectError = e.getBindingResult().getAllErrors(); List<ObjectError> objectError = e
.getBindingResult()
.getAllErrors();
// 然后提取错误提示信息进行返回 // 然后提取错误提示信息进行返回
HashMap<Integer, String> errorMap = new HashMap<>(); HashMap<Integer, String> errorMap = new HashMap<>();
objectError.forEach(objectError1 -> errorMap.put(objectError.indexOf(objectError1), objectError1.getDefaultMessage())); objectError.forEach(objectError1 -> errorMap.put(
objectError.indexOf(objectError1),
objectError1.getDefaultMessage()
));
// 使用标准化返回体返回数据 // 使用标准化返回体返回数据
return R.resetResult(ResultStat.VALIDATE_FAILED.getCode(), ResultStat.VALIDATE_FAILED.getDescription(), errorMap); return R.resetResult(
ResultStat.VALIDATE_FAILED.getCode(),
ResultStat.VALIDATE_FAILED.getDescription(),
errorMap
);
//return errorMap; //return errorMap;
} }
@@ -62,14 +71,22 @@ public class GlobalExceptionHandler {
//按需重新封装需要返回的错误信息 //按需重新封装需要返回的错误信息
Map<String, String> invalidMap = new LinkedHashMap(99); Map<String, String> invalidMap = new LinkedHashMap(99);
//解析原错误信息,封装后返回,此处返回非法的字段名称,原始值,错误信息 //解析原错误信息,封装后返回,此处返回非法的字段名称,原始值,错误信息
invalidMap.put(exception.getParameter().getParameterName(), ResultStat.PARAM_ERROR.getDescription()); invalidMap.put(
exception
.getParameter()
.getParameterName(),
ResultStat.PARAM_ERROR.getDescription()
);
R<Map<String, String>> vo = new R<>(); R<Map<String, String>> vo = new R<>();
vo.setCode(ResultStat.PARAM_ERROR.getCode()); vo.setCode(ResultStat.PARAM_ERROR.getCode());
vo.setMsg(getLocaleMsg(ResultStat.PARAM_ERROR.getDescription())); vo.setMsg(getLocaleMsg(ResultStat.PARAM_ERROR.getDescription()));
vo.setData(invalidMap); vo.setData(invalidMap);
log.debug(exception.getMessage(), exception); log.debug(
exception.getMessage(),
exception
);
return vo; return vo;
} }
@@ -93,7 +110,10 @@ public class GlobalExceptionHandler {
vo.setData("数据库异常,操作失败!"); vo.setData("数据库异常,操作失败!");
} }
log.debug(exception.getMessage(), exception); log.debug(
exception.getMessage(),
exception
);
return vo; return vo;
} }
@@ -104,12 +124,18 @@ public class GlobalExceptionHandler {
R<Object> R = new R<>(); R<Object> R = new R<>();
ResultStat status = exception.getStatus(); ResultStat status = exception.getStatus();
if (status != null) { if (status != null) {
R.setMsg(getLocaleMsg(exception.getMessage(), exception.getParams())); R.setMsg(getLocaleMsg(
exception.getMessage(),
exception.getParams()
));
R.setCode(status.getCode()); R.setCode(status.getCode());
R.setData(exception.getData()); R.setData(exception.getData());
} else { } else {
R.setCode(ResultStat.FAILED.getCode()); R.setCode(ResultStat.FAILED.getCode());
R.setMsg(getLocaleMsg(exception.getMessage(), exception.getParams())); R.setMsg(getLocaleMsg(
exception.getMessage(),
exception.getParams()
));
R.setData(null); R.setData(null);
} }
return R; return R;
@@ -136,20 +162,34 @@ public class GlobalExceptionHandler {
//解析原错误信息,封装后返回,此处返回非法的字段名称,原始值,错误信息 //解析原错误信息,封装后返回,此处返回非法的字段名称,原始值,错误信息
if (exception != null) { if (exception != null) {
List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors(); List<FieldError> fieldErrors = exception
fieldErrors.stream().sorted(Comparator.comparing(FieldError::getCode)).forEach(error -> { .getBindingResult()
String defaultMessage = error.getDefaultMessage(); .getFieldErrors();
String finalMessage = defaultMessage; fieldErrors
if (defaultMessage != null && defaultMessage.startsWith("{") && defaultMessage.endsWith("}")) { .stream()
finalMessage = messageSource.getMessage(defaultMessage.substring(1, defaultMessage.length() - 1)); .sorted(Comparator.comparing(FieldError::getCode))
} .forEach(error -> {
String defaultMessage = error.getDefaultMessage();
String finalMessage = defaultMessage;
if (defaultMessage != null && defaultMessage.startsWith("{") && defaultMessage.endsWith("}")) {
finalMessage = messageSource.getMessage(defaultMessage.substring(
1,
defaultMessage.length() - 1
));
}
if (StringUtils.isNotEmpty(invalidMap.get(error.getField()))) { if (StringUtils.isNotEmpty(invalidMap.get(error.getField()))) {
invalidMap.put(error.getField(), invalidMap.get(error.getField()) + "," + finalMessage); invalidMap.put(
} else { error.getField(),
invalidMap.put(error.getField(), finalMessage); invalidMap.get(error.getField()) + "," + finalMessage
} );
}); } else {
invalidMap.put(
error.getField(),
finalMessage
);
}
});
} }
R<Map<String, String>> vo = new R<>(); R<Map<String, String>> vo = new R<>();
@@ -157,7 +197,10 @@ public class GlobalExceptionHandler {
vo.setMsg(getLocaleMsg(ResultStat.VALIDATE_FAILED.getDescription())); vo.setMsg(getLocaleMsg(ResultStat.VALIDATE_FAILED.getDescription()));
vo.setData(invalidMap); vo.setData(invalidMap);
log.debug(exception.getMessage(), exception); log.debug(
exception.getMessage(),
exception
);
return vo; return vo;
@@ -168,7 +211,10 @@ public class GlobalExceptionHandler {
*/ */
@ExceptionHandler(value = Exception.class) @ExceptionHandler(value = Exception.class)
public Object exceptionHandel(Exception e) { public Object exceptionHandel(Exception e) {
return getResponseVO(e, getLocaleMsg("系统错误,请联系管理员")); return getResponseVO(
e,
getLocaleMsg("系统错误,请联系管理员")
);
} }
private Object getResponseVO(Exception e, String msg) { private Object getResponseVO(Exception e, String msg) {
@@ -176,12 +222,20 @@ public class GlobalExceptionHandler {
R.setCode(ResultStat.FAILED.getCode()); R.setCode(ResultStat.FAILED.getCode());
R.setMsg(msg); R.setMsg(msg);
R.setMsg(e.getMessage()); R.setMsg(e.getMessage());
log.error(e.getMessage(), e);
// 此处修改不要打印 错误 e
log.error(
e.getMessage(),
e
);
return R; return R;
} }
private String getLocaleMsg(String msgCode, Object... params) { private String getLocaleMsg(String msgCode, Object... params) {
return messageSource.getMessage(msgCode, params); return messageSource.getMessage(
msgCode,
params
);
} }
} }

View File

@@ -47,7 +47,9 @@ public class FunctionReader {
try { try {
// 构造一个 buffered Reader // 构造一个 buffered Reader
BufferedReader bufferedReader = new BufferedReader(new StringReader(functionContent)); BufferedReader bufferedReader = new BufferedReader(
new StringReader(functionContent)
);
// 执行read操作 // 执行read操作
result = doReadContent( result = doReadContent(
@@ -63,6 +65,14 @@ public class FunctionReader {
} }
/**
* 实际解析从前端传入的 脚本命令, 将其转换为 List<List<String>>
*
* @param result
* @param bufferedReader
* @return
* @throws IOException
*/
private static List<List<String>> doReadContent(List<List<String>> result, BufferedReader bufferedReader) throws IOException { private static List<List<String>> doReadContent(List<List<String>> result, BufferedReader bufferedReader) throws IOException {
String line = bufferedReader.readLine(); String line = bufferedReader.readLine();
@@ -73,7 +83,7 @@ public class FunctionReader {
while (line != null) { while (line != null) {
if (!StringUtils.isEmpty(line)) { if (!StringUtils.isEmpty(line)) {
result.add(SplitLineToCommandList(line)); result.add(SplitSpaceIndentToCommandList(line));
} }
line = bufferedReader.readLine(); line = bufferedReader.readLine();
} }
@@ -81,10 +91,25 @@ public class FunctionReader {
return result; return result;
} }
public static List<String> SplitLineToCommandList(String commandLine) { public static List<String> SplitSpaceIndentToCommandList(String commandLine) {
return Arrays return Arrays
.stream(commandLine.split(" ")) .stream(commandLine
.split(" "))
.collect(Collectors.toList());
}
public static List<String> SplitCommaIndentToCommandList(String commandLine) {
// 需要进行归一化,去除掉多余的空格
return Arrays
.stream(commandLine.split(","))
.map(
split -> split.replace(
" ",
""
)
)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }

View File

@@ -94,13 +94,13 @@ public class ProxyNodeSet {
chengduAgent = ProxyNode chengduAgent = ProxyNode
.builder() .builder()
.location("Chengdu") .location("Tokyo")
.num(99) .num(99)
.publicIPv4("183.220.112.113") .publicIPv4("34.146.3.239")
.proxyNodeType(ProxyNodeType.EXTERNAL) .proxyNodeType(ProxyNodeType.EXTERNAL)
.name("chengdu-agent") .name("tokyo-07")
.agentName("Chengdu-amd64-77") .agentName("Tokyo-amd64-07")
.agentTopicName("Chengdu-amd64-77-remote") .agentTopicName("Tokyo-amd64-07-f66a41")
.build(); .build();
ProxyNodeMap.put(chengdu.getNum(), chengdu); ProxyNodeMap.put(chengdu.getNum(), chengdu);

View File

@@ -88,7 +88,8 @@ public class CoreExecutionServiceImpl implements CoreExecutionService {
"agentTopicName异常! 输入为 => {}", "agentTopicName异常! 输入为 => {}",
agentTopicName agentTopicName
); );
throw new MyRuntimeException("agentTopicName异常!" + agentTopicName); return null;
//throw new MyRuntimeException("agentTopicName异常!" + agentTopicName);
} }
// 归一化type类型 不行 // 归一化type类型 不行

View File

@@ -3,6 +3,7 @@ package io.wdd.rpc.scheduler.beans;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@@ -54,6 +55,7 @@ public class ScriptSchedulerVO {
* 脚本任务的内容 * 脚本任务的内容
*/ */
@TableField(value = "script_content") @TableField(value = "script_content")
@ApiModelProperty("单行命令之间使用 空格 分隔, 每一行之间使用 \\n 分隔")
@Nullable @Nullable
private String scriptContent; private String scriptContent;

View File

@@ -11,7 +11,6 @@ import io.wdd.server.beans.po.ScriptSchedulerPO;
import io.wdd.server.service.ScriptSchedulerService; import io.wdd.server.service.ScriptSchedulerService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.quartz.*; import org.quartz.*;
@@ -33,20 +32,16 @@ import static org.quartz.TriggerBuilder.newTrigger;
@Service @Service
public class QuartzSchedulerServiceImpl implements QuartzSchedulerService { public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
@Resource
ScriptSchedulerService scriptSchedulerService;
@Resource
QuartzSchedulerUtils quartzSchedulerUtils;
@Autowired
private Scheduler scheduler;
/** /**
* 保存 定时脚本任务的中间DTO在Quartz框架中JobDataMap的Key名称 * 保存 定时脚本任务的中间DTO在Quartz框架中JobDataMap的Key名称
*/ */
public static final String SCRIPT_SCHEDULER_MISSION_KEY = "scriptSchedulerDTO"; public static final String SCRIPT_SCHEDULER_MISSION_KEY = "scriptSchedulerDTO";
@Resource
ScriptSchedulerService scriptSchedulerService;
@Resource
QuartzSchedulerUtils quartzSchedulerUtils;
@Autowired
private Scheduler scheduler;
@Override @Override
public HashMap<String, String> createScriptScheduledMission(ScriptSchedulerVO scriptSchedulerVO) { public HashMap<String, String> createScriptScheduledMission(ScriptSchedulerVO scriptSchedulerVO) {
@@ -75,6 +70,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
dataMap dataMap
); );
// 生成下一次真正执行的任务ResultKey
HashMap<String, String> futureExecutionResultKey = quartzSchedulerUtils.generateAndSetFutureExecutionResultKey(scriptSchedulerDTO); HashMap<String, String> futureExecutionResultKey = quartzSchedulerUtils.generateAndSetFutureExecutionResultKey(scriptSchedulerDTO);
log.debug( log.debug(
@@ -82,8 +78,14 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
futureExecutionResultKey futureExecutionResultKey
); );
log.info("[ 定时脚本任务 ] - 新建任务成功, 任务DTO为 => {}", scriptSchedulerDTO); log.info(
log.info("[ 定时脚本任务 ] - 新建任务成功, 任务内容为 => {}", jobDetail); "[ 定时脚本任务 ] - 新建任务成功, 任务DTO为 => {}",
scriptSchedulerDTO
);
log.info(
"[ 定时脚本任务 ] - 新建任务成功, 任务内容为 => {}",
jobDetail
);
// persistent the script scheduled mission // persistent the script scheduled mission
// dto should store more info // dto should store more info
@@ -132,23 +134,31 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
} }
// 归一化 completeCommandList // 归一化 completeCommandList
List<List<String>> completeCommandList = FunctionReader.ReadStringToCommandList(scriptSchedulerVO.getScriptContent()); List<List<String>> completeCommandList = FunctionReader
.ReadStringToCommandList(scriptSchedulerVO.getScriptContent());
if (completeCommandList.size() == 0) { if (completeCommandList.size() == 0) {
throw new MyRuntimeException("completeCommandList parse wrong !"); throw new MyRuntimeException("完整的命令脚本列表解析错误!");
} }
// 执行机器目标归一化 // 执行机器目标归一化
String[] targetMachineSplit = scriptSchedulerVO String[] targetMachineSplit = scriptSchedulerVO
.getTargetMachine() .getTargetMachine()
.split(","); .split(",");
if (targetMachineSplit.length == 0) { List<String> goodTargetMachine = Arrays
.stream(targetMachineSplit)
.map(
machine -> machine.replace(
" ",
""
)
)
.collect(Collectors.toList());
if (goodTargetMachine.size() == 0) {
throw new MyRuntimeException("target machine wrong !"); throw new MyRuntimeException("target machine wrong !");
} }
ArrayList<String> targetMachineList = new ArrayList<>(); // 防止于最终对线中
Collections.addAll( ArrayList<String> targetMachineList = new ArrayList<>(goodTargetMachine);
targetMachineList,
targetMachineSplit
);
// 生成DTO对象 // 生成DTO对象
ScriptSchedulerDTO dto = new ScriptSchedulerDTO(); ScriptSchedulerDTO dto = new ScriptSchedulerDTO();
@@ -317,7 +327,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
trigger trigger
); );
return jobDetail; return jobDetail;
} catch (Exception e) { } catch (Exception e) {
@@ -576,8 +586,10 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
// 修改JobDetail信息 // 修改JobDetail信息
ScriptSchedulerDTO scriptSchedulerDTO = validateAndConvertToScriptSchedulerDTO(scriptSchedulerVO); ScriptSchedulerDTO scriptSchedulerDTO = validateAndConvertToScriptSchedulerDTO(scriptSchedulerVO);
JobDataMap map = jobDetail.getJobDataMap(); JobDataMap map = jobDetail.getJobDataMap();
map.put(SCRIPT_SCHEDULER_MISSION_KEY, map.put(
scriptSchedulerDTO); SCRIPT_SCHEDULER_MISSION_KEY,
scriptSchedulerDTO
);
// 修改 Mission相应的 cron时间信息 // 修改 Mission相应的 cron时间信息
updateMission( updateMission(