Commit a727c5e9 by huangfusuper

重构补批工作流

parent 0b7fac9d
...@@ -12,6 +12,7 @@ import com.byit.enums.*; ...@@ -12,6 +12,7 @@ import com.byit.enums.*;
import com.byit.enums.task.RunResultEnum; import com.byit.enums.task.RunResultEnum;
import com.byit.enums.task.RunTypeEnum; import com.byit.enums.task.RunTypeEnum;
import com.byit.job.utils.DateUtil; import com.byit.job.utils.DateUtil;
import com.byit.job.utils.MythLogUtils;
import com.byit.job.utils.PlaceholderUtils; import com.byit.job.utils.PlaceholderUtils;
import com.byit.model.JobTaskRunLogWithBLOBs; import com.byit.model.JobTaskRunLogWithBLOBs;
import com.byit.model.JobTaskSchedule; import com.byit.model.JobTaskSchedule;
...@@ -19,7 +20,6 @@ import com.byit.service.FastRunLogService; ...@@ -19,7 +20,6 @@ import com.byit.service.FastRunLogService;
import com.byit.service.FlowStatusService; import com.byit.service.FlowStatusService;
import com.byit.service.RunScriptService; import com.byit.service.RunScriptService;
import com.byit.service.impl.JobTaskRunLogServiceImpl; import com.byit.service.impl.JobTaskRunLogServiceImpl;
import com.byit.job.utils.MythLogUtils;
import com.byit.util.GetRegConfig; import com.byit.util.GetRegConfig;
import com.byit.util.SpringUtil; import com.byit.util.SpringUtil;
import io.netty.util.Timeout; import io.netty.util.Timeout;
...@@ -28,13 +28,17 @@ import lombok.extern.slf4j.Slf4j; ...@@ -28,13 +28,17 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.*; import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.alibaba.fastjson.serializer.SerializerFeature.WriteClassName; import static com.alibaba.fastjson.serializer.SerializerFeature.WriteClassName;
/** /**
* 脚本的执行调用器 * 脚本的执行调用器
*
* @author huangfu * @author huangfu
*/ */
@Slf4j @Slf4j
...@@ -56,36 +60,37 @@ public class ScriptExecutorJobTask implements TimerTask { ...@@ -56,36 +60,37 @@ public class ScriptExecutorJobTask implements TimerTask {
public void run(Timeout timeout) { public void run(Timeout timeout) {
log.debug("-----------开始交验工作流时否正在运行中--------------"); log.debug("-----------开始交验工作流时否正在运行中--------------");
String code = checkFlowStatusIsKill(mythJobTaskSchedule.getFlowId(), mythJobTaskSchedule.getRunId()); String code = checkFlowStatusIsKill(mythJobTaskSchedule.getFlowId(), mythJobTaskSchedule.getRunId());
if(code != null){ if (code != null) {
log.warn("--------------该工作流已经被杀死快速失败,执行快速失败!-------------------"); log.warn("--------------该工作流已经被杀死快速失败,执行快速失败!-------------------");
saveErrorLog(mythJobTaskSchedule,code); saveErrorLog(mythJobTaskSchedule, code);
return; return;
} }
log.debug("-----------------工作流校验完成-------------"); log.debug("-----------------工作流校验完成-------------");
//获取任务级别 1最低 2最高 //获取任务级别 1最低 2最高
String priority = mythJobTaskSchedule.getPriority(); String priority = mythJobTaskSchedule.getPriority();
if(NodePropertyEnum.ADVANCED_NODE.getCode().equals(priority)){ if (NodePropertyEnum.ADVANCED_NODE.getCode().equals(priority)) {
MythJobAutoConfigure.ADVANCED_JOB_THREAD_POOL.execute(()-> runJob(mythJobTaskSchedule)); MythJobAutoConfigure.ADVANCED_JOB_THREAD_POOL.execute(() -> runJob(mythJobTaskSchedule));
}else{ } else {
MythJobAutoConfigure.LOW_LEVEL_JOB_THREAD_POOL.execute(()-> runJob(mythJobTaskSchedule)); MythJobAutoConfigure.LOW_LEVEL_JOB_THREAD_POOL.execute(() -> runJob(mythJobTaskSchedule));
} }
} }
/** /**
* 校验工作流是否被杀死或者被快速失败 * 校验工作流是否被杀死或者被快速失败
*
* @param flowId 工作流ID * @param flowId 工作流ID
* @param runId 运行标识 * @param runId 运行标识
* @return * @return
*/ */
private String checkFlowStatusIsKill(Integer flowId,String runId){ private String checkFlowStatusIsKill(Integer flowId, String runId) {
FlowStatusService bean = SpringUtil.getBean(FlowStatusService.class); FlowStatusService bean = SpringUtil.getBean(FlowStatusService.class);
return bean.checkFlowStatusIsKill(flowId,runId); return bean.checkFlowStatusIsKill(flowId, runId);
} }
private void paramBuild(JobTaskSchedule mythJobTaskSchedule){ private void paramBuild(JobTaskSchedule mythJobTaskSchedule) {
//获取运行时参数 //获取运行时参数
String runParam = mythJobTaskSchedule.getRunParam(); String runParam = mythJobTaskSchedule.getRunParam();
//获取命令 //获取命令
...@@ -93,22 +98,22 @@ public class ScriptExecutorJobTask implements TimerTask { ...@@ -93,22 +98,22 @@ public class ScriptExecutorJobTask implements TimerTask {
//转换参数对象为参数包装体 //转换参数对象为参数包装体
RunParamWrapped runParamWrapped = JSON.parseObject(runParam, RunParamWrapped.class); RunParamWrapped runParamWrapped = JSON.parseObject(runParam, RunParamWrapped.class);
//当参数包装体不为空时 证明存在参数 或私有或公有 //当参数包装体不为空时 证明存在参数 或私有或公有
if(runParamWrapped != null) { if (runParamWrapped != null) {
//获取到私有参数 //获取到私有参数
String privateParam = runParamWrapped.getPrivateParam(); String privateParam = runParamWrapped.getPrivateParam();
//替换运行参数中的时间参数 //替换运行参数中的时间参数
if (ScheduleTypeEnum.NORMAL.getCode().equals(mythJobTaskSchedule.getScheduleType()) if (ScheduleTypeEnum.NORMAL.getCode().equals(mythJobTaskSchedule.getScheduleType())
|| ScheduleTypeEnum.REAL.getCode().equals(mythJobTaskSchedule.getScheduleType())) { || ScheduleTypeEnum.REAL.getCode().equals(mythJobTaskSchedule.getScheduleType())) {
privateParam = PlaceholderUtils.formatBizDateParam(privateParam, PlaceholderEnum.DATE_PLACEHOLDER.getName(), 1); privateParam = PlaceholderUtils.formatBizDateParam(privateParam, PlaceholderEnum.DATE_PLACEHOLDER.getName(), 1);
if(StringUtils.isNoneBlank(privateParam)) { if (StringUtils.isNoneBlank(privateParam)) {
ScriptParamAndPlaceholderDto scriptParamAndPlaceholderDto = JSON.parseObject(privateParam,ScriptParamAndPlaceholderDto.class); ScriptParamAndPlaceholderDto scriptParamAndPlaceholderDto = JSON.parseObject(privateParam, ScriptParamAndPlaceholderDto.class);
Map<String, String> placeholder = scriptParamAndPlaceholderDto.getPlaceholder(); Map<String, String> placeholder = scriptParamAndPlaceholderDto.getPlaceholder();
if(CollectionUtil.isNotEmpty(placeholder)) { if (CollectionUtil.isNotEmpty(placeholder)) {
if(placeholder.containsKey(PlaceholderEnum.NOW_DATE_PLACEHOLDER.getName())){ if (placeholder.containsKey(PlaceholderEnum.NOW_DATE_PLACEHOLDER.getName())) {
String nowDateFormatName = placeholder.get(PlaceholderEnum.NOW_DATE_PLACEHOLDER.getName()); String nowDateFormatName = placeholder.get(PlaceholderEnum.NOW_DATE_PLACEHOLDER.getName());
String dateFormat = DateUtil.dateFormat(new Date(), nowDateFormatName); String dateFormat = DateUtil.dateFormat(new Date(), nowDateFormatName);
privateParam = PlaceholderUtils.formatBizDateParam(privateParam, PlaceholderEnum.NOW_DATE_PLACEHOLDER.getName(), dateFormat,0); privateParam = PlaceholderUtils.formatBizDateParam(privateParam, PlaceholderEnum.NOW_DATE_PLACEHOLDER.getName(), dateFormat, 0);
} }
} }
...@@ -118,22 +123,22 @@ public class ScriptExecutorJobTask implements TimerTask { ...@@ -118,22 +123,22 @@ public class ScriptExecutorJobTask implements TimerTask {
} }
//获取到公有参数 //获取到公有参数
Map<String,String> publicParam = runParamWrapped.getPublicParamMap(); Map<String, String> publicParam = runParamWrapped.getPublicParamMap();
//将私有参数转换为对应的参数DTO //将私有参数转换为对应的参数DTO
ScriptParamAndPlaceholderDto scriptParamAndPlaceholderDto = null; ScriptParamAndPlaceholderDto scriptParamAndPlaceholderDto = null;
if(StringUtils.isNoneBlank(privateParam)){ if (StringUtils.isNoneBlank(privateParam)) {
scriptParamAndPlaceholderDto = JSON.parseObject(privateParam,ScriptParamAndPlaceholderDto.class); scriptParamAndPlaceholderDto = JSON.parseObject(privateParam, ScriptParamAndPlaceholderDto.class);
} }
//脚本参数不为空的时候 //脚本参数不为空的时候
if(scriptParamAndPlaceholderDto != null) { if (scriptParamAndPlaceholderDto != null) {
Map<String, String> param = scriptParamAndPlaceholderDto.getParam(); Map<String, String> param = scriptParamAndPlaceholderDto.getParam();
if(CollectionUtil.isNotEmpty(param)){ if (CollectionUtil.isNotEmpty(param)) {
command = PlaceholderUtils.commandReplace(command,param); command = PlaceholderUtils.commandReplace(command, param);
} }
} }
//公共参数不为空的时候 //公共参数不为空的时候
if(CollectionUtil.isNotEmpty(publicParam)) { if (CollectionUtil.isNotEmpty(publicParam)) {
command = PlaceholderUtils.commandReplace(command,publicParam ); command = PlaceholderUtils.commandReplace(command, publicParam);
} }
mythJobTaskSchedule.setRunCommand(command); mythJobTaskSchedule.setRunCommand(command);
} }
...@@ -141,16 +146,17 @@ public class ScriptExecutorJobTask implements TimerTask { ...@@ -141,16 +146,17 @@ public class ScriptExecutorJobTask implements TimerTask {
/** /**
* 运行任务 * 运行任务
*
* @param mythJobTaskSchedule 运行的排期表 * @param mythJobTaskSchedule 运行的排期表
*/ */
private void runJob(JobTaskSchedule mythJobTaskSchedule) { private void runJob(JobTaskSchedule mythJobTaskSchedule) {
DispatchResponseDto dispatchResponseDto = new DispatchResponseDto(); DispatchResponseDto dispatchResponseDto = new DispatchResponseDto();
try{ try {
paramBuild(mythJobTaskSchedule); paramBuild(mythJobTaskSchedule);
mythJobTaskSchedule.setRunCommand(mythJobTaskSchedule.getRunCommand()); mythJobTaskSchedule.setRunCommand(mythJobTaskSchedule.getRunCommand());
RunScriptService runScriptService = SpringUtil.getBean(RunScriptService.class); RunScriptService runScriptService = SpringUtil.getBean(RunScriptService.class);
JobTaskRunLogServiceImpl jobTaskRunLogService = SpringUtil.getBean(JobTaskRunLogServiceImpl.class); JobTaskRunLogServiceImpl jobTaskRunLogService = SpringUtil.getBean(JobTaskRunLogServiceImpl.class);
log.info("--------------logid:{}----------",mythJobTaskSchedule.getLogId()); log.info("--------------logid:{}----------", mythJobTaskSchedule.getLogId());
JobTaskRunLogWithBLOBs jobTaskRunLogById = jobTaskRunLogService.findJobTaskRunLogById(mythJobTaskSchedule.getLogId()); JobTaskRunLogWithBLOBs jobTaskRunLogById = jobTaskRunLogService.findJobTaskRunLogById(mythJobTaskSchedule.getLogId());
jobTaskRunLogById = spinLock(jobTaskRunLogById); jobTaskRunLogById = spinLock(jobTaskRunLogById);
ScriptDto scriptDto = new ScriptDto(); ScriptDto scriptDto = new ScriptDto();
...@@ -161,16 +167,19 @@ public class ScriptExecutorJobTask implements TimerTask { ...@@ -161,16 +167,19 @@ public class ScriptExecutorJobTask implements TimerTask {
scriptDto.setRemotePath(mythJobTaskSchedule.getScriptUrls()); scriptDto.setRemotePath(mythJobTaskSchedule.getScriptUrls());
GetRegConfig getRegConfig = SpringUtil.getBean(GetRegConfig.class); GetRegConfig getRegConfig = SpringUtil.getBean(GetRegConfig.class);
TreeSet<String> gatewayConf = getRegConfig.getGatewayConf(); TreeSet<String> gatewayConf = getRegConfig.getGatewayConf();
if(CollectionUtil.isEmpty(gatewayConf)) {
throw new RuntimeException("网关地址为空!");
}
List<String> callUrlList = gatewayConf.stream().map(callbackIp -> String.format("%s%s%s", HTTP_PRE, callbackIp, HTTP_SUFFIX)).collect(Collectors.toList()); List<String> callUrlList = gatewayConf.stream().map(callbackIp -> String.format("%s%s%s", HTTP_PRE, callbackIp, HTTP_SUFFIX)).collect(Collectors.toList());
scriptDto.setCallbackUrl(JSON.toJSONString(callUrlList)); scriptDto.setCallbackUrl(JSON.toJSONString(callUrlList));
//二次执行的情况下 会有这个信息 //二次执行的情况下 会有这个信息
scriptDto.setLogRemotePath(jobTaskRunLogById.getLogRemotelyPath()); scriptDto.setLogRemotePath(jobTaskRunLogById.getLogRemotelyPath());
dispatchResponseDto = runScriptService.runScript(scriptDto); dispatchResponseDto = runScriptService.runScript(scriptDto);
}catch (Exception e){ } catch (Exception e) {
if (mythJobTaskSchedule.getScheduleType().equals(4)) { if (mythJobTaskSchedule.getScheduleType().equals(4)) {
StringRedisTemplate stringRedisTemplate = (StringRedisTemplate) SpringUtil.getBean("stringRedisTemplate"); StringRedisTemplate stringRedisTemplate = (StringRedisTemplate) SpringUtil.getBean("stringRedisTemplate");
RunLog runLog = RunLog.builder().isEnd(true).isSuccess(false).runLog("执行资源异常" + MythLogUtils.getMessage(e)).build(); RunLog runLog = RunLog.builder().isEnd(true).isSuccess(false).runLog("执行资源异常" + MythLogUtils.getMessage(e)).build();
stringRedisTemplate.opsForList().rightPush("REAL:EXEC:" + mythJobTaskSchedule.getLogId() , JSON.toJSONString(runLog, WriteClassName)); stringRedisTemplate.opsForList().rightPush("REAL:EXEC:" + mythJobTaskSchedule.getLogId(), JSON.toJSONString(runLog, WriteClassName));
} }
log.error("------执行机异常{}-----", MythLogUtils.getMessage(e)); log.error("------执行机异常{}-----", MythLogUtils.getMessage(e));
...@@ -178,30 +187,31 @@ public class ScriptExecutorJobTask implements TimerTask { ...@@ -178,30 +187,31 @@ public class ScriptExecutorJobTask implements TimerTask {
dispatchResponseDto.setCode(JobResultEnum.DISPATCH_FAIL.getCode()); dispatchResponseDto.setCode(JobResultEnum.DISPATCH_FAIL.getCode());
} }
saveLog(mythJobTaskSchedule,dispatchResponseDto); saveLog(mythJobTaskSchedule, dispatchResponseDto);
} }
/** /**
* 模拟自旋锁实现 * 模拟自旋锁实现
*
* @param jobTaskRunLogById * @param jobTaskRunLogById
* @return * @return
* @throws InterruptedException * @throws InterruptedException
*/ */
private JobTaskRunLogWithBLOBs spinLock (JobTaskRunLogWithBLOBs jobTaskRunLogById) throws InterruptedException { private JobTaskRunLogWithBLOBs spinLock(JobTaskRunLogWithBLOBs jobTaskRunLogById) throws InterruptedException {
log.debug("---------------进入自旋锁状态spinLock---------"); log.debug("---------------进入自旋锁状态spinLock---------");
JobTaskRunLogServiceImpl jobTaskRunLogService = SpringUtil.getBean(JobTaskRunLogServiceImpl.class); JobTaskRunLogServiceImpl jobTaskRunLogService = SpringUtil.getBean(JobTaskRunLogServiceImpl.class);
int i = 1; int i = 1;
while (jobTaskRunLogById ==null) { while (jobTaskRunLogById == null) {
Thread.sleep(i*INIT_SLEEP_TIME); Thread.sleep(i * INIT_SLEEP_TIME);
jobTaskRunLogById = jobTaskRunLogService.findJobTaskRunLogById(mythJobTaskSchedule.getLogId()); jobTaskRunLogById = jobTaskRunLogService.findJobTaskRunLogById(mythJobTaskSchedule.getLogId());
if(i >= 3){ if (i >= 3) {
break; break;
} }
i++; i++;
} }
if(jobTaskRunLogById != null){ if (jobTaskRunLogById != null) {
log.debug("---------------自旋获取锁成功---------"); log.debug("---------------自旋获取锁成功---------");
return jobTaskRunLogById; return jobTaskRunLogById;
} }
...@@ -209,7 +219,7 @@ public class ScriptExecutorJobTask implements TimerTask { ...@@ -209,7 +219,7 @@ public class ScriptExecutorJobTask implements TimerTask {
throw new RuntimeException("服务器分配资源失败"); throw new RuntimeException("服务器分配资源失败");
} }
private void saveLog(JobTaskSchedule mythJobTaskSchedule, DispatchResponseDto dispatchResponseDto){ private void saveLog(JobTaskSchedule mythJobTaskSchedule, DispatchResponseDto dispatchResponseDto) {
log.debug("-----------saveLog--保存脚本调度日志开始------------"); log.debug("-----------saveLog--保存脚本调度日志开始------------");
JobTaskRunLogServiceImpl jobTaskRunLogService = SpringUtil.getBean(JobTaskRunLogServiceImpl.class); JobTaskRunLogServiceImpl jobTaskRunLogService = SpringUtil.getBean(JobTaskRunLogServiceImpl.class);
...@@ -217,11 +227,11 @@ public class ScriptExecutorJobTask implements TimerTask { ...@@ -217,11 +227,11 @@ public class ScriptExecutorJobTask implements TimerTask {
JobTaskRunLogWithBLOBs jobTaskRunLog = new JobTaskRunLogWithBLOBs(); JobTaskRunLogWithBLOBs jobTaskRunLog = new JobTaskRunLogWithBLOBs();
//放置调度记录 //放置调度记录
if (jobTaskRunLogById.getRunCount()>1) { if (jobTaskRunLogById.getRunCount() > 1) {
//第一次调度的日志 //第一次调度的日志
String triggerMsg = jobTaskRunLogById.getTriggerMsg(); String triggerMsg = jobTaskRunLogById.getTriggerMsg();
jobTaskRunLog.setTriggerMsg(triggerMsg + dispatchResponseDto.getMsg() + "\n"); jobTaskRunLog.setTriggerMsg(triggerMsg + dispatchResponseDto.getMsg() + "\n");
}else{ } else {
jobTaskRunLog.setTriggerMsg(dispatchResponseDto.getMsg() + "\n"); jobTaskRunLog.setTriggerMsg(dispatchResponseDto.getMsg() + "\n");
} }
...@@ -233,24 +243,24 @@ public class ScriptExecutorJobTask implements TimerTask { ...@@ -233,24 +243,24 @@ public class ScriptExecutorJobTask implements TimerTask {
jobTaskRunLog.setTriggerTime(new Date()); jobTaskRunLog.setTriggerTime(new Date());
String code = RunResultEnum.TRIGGER_SUCCESS.getCode(); String code = RunResultEnum.TRIGGER_SUCCESS.getCode();
//放置调度码 调度失败 那么执行肯定也失败 //放置调度码 调度失败 那么执行肯定也失败
if(JobResultEnum.DISPATCH_FAIL.getCode().equals(dispatchResponseDto.getCode())){ if (JobResultEnum.DISPATCH_FAIL.getCode().equals(dispatchResponseDto.getCode())) {
code = RunResultEnum.TRIGGER_ERROR.getCode(); code = RunResultEnum.TRIGGER_ERROR.getCode();
Date thisTime = new Date(); Date thisTime = new Date();
jobTaskRunLog.setStartTime(thisTime); jobTaskRunLog.setStartTime(thisTime);
jobTaskRunLog.setEndTime(thisTime); jobTaskRunLog.setEndTime(thisTime);
jobTaskRunLog.setRunCode(RunResultEnum.RUN_ERROR.getCode()); jobTaskRunLog.setRunCode(RunResultEnum.RUN_ERROR.getCode());
jobTaskRunLog.setAlertEnd(EmailEnum.IS_ALARM_NO.getCode()); jobTaskRunLog.setAlertEnd(EmailEnum.IS_ALARM_NO.getCode());
if (jobTaskRunLogById.getRunCount()>1) { if (jobTaskRunLogById.getRunCount() > 1) {
//上一次的执行日志 //上一次的执行日志
String runMsg = jobTaskRunLogById.getRunMsg(); String runMsg = jobTaskRunLogById.getRunMsg();
jobTaskRunLog.setRunMsg(runMsg + dispatchResponseDto.getMsg()+LINE); jobTaskRunLog.setRunMsg(runMsg + dispatchResponseDto.getMsg() + LINE);
}else{ } else {
jobTaskRunLog.setRunMsg(dispatchResponseDto.getMsg()+LINE); jobTaskRunLog.setRunMsg(dispatchResponseDto.getMsg() + LINE);
} }
} }
//失败 //失败
jobTaskRunLog.setTriggerCode(code); jobTaskRunLog.setTriggerCode(code);
jobTaskRunLog.setRunCount(jobTaskRunLogById.getRunCount()+1); jobTaskRunLog.setRunCount(jobTaskRunLogById.getRunCount() + 1);
//获取执行机地址 //获取执行机地址
jobTaskRunLog.setJobGroupIp(dispatchResponseDto.getUrl()); jobTaskRunLog.setJobGroupIp(dispatchResponseDto.getUrl());
jobTaskRunLogService.updateJobTaskRunLogWithBLOBs(jobTaskRunLog); jobTaskRunLogService.updateJobTaskRunLogWithBLOBs(jobTaskRunLog);
...@@ -259,10 +269,11 @@ public class ScriptExecutorJobTask implements TimerTask { ...@@ -259,10 +269,11 @@ public class ScriptExecutorJobTask implements TimerTask {
/** /**
* 保存失败的节点执行信息 * 保存失败的节点执行信息
*
* @param mythJobTaskSchedule 节点信息 * @param mythJobTaskSchedule 节点信息
*/ */
private void saveErrorLog(JobTaskSchedule mythJobTaskSchedule,String code){ private void saveErrorLog(JobTaskSchedule mythJobTaskSchedule, String code) {
FastRunLogService bean = SpringUtil.getBean(FastRunLogService.class); FastRunLogService bean = SpringUtil.getBean(FastRunLogService.class);
bean.fastErrorLog(mythJobTaskSchedule,code); bean.fastErrorLog(mythJobTaskSchedule, code);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment