|
|
@@ -1,5 +1,7 @@ |
|
|
|
package com.ruoyi.business.service.impl; |
|
|
|
|
|
|
|
import java.text.ParseException; |
|
|
|
import java.time.ZonedDateTime; |
|
|
|
import java.util.*; |
|
|
|
import java.util.concurrent.atomic.AtomicBoolean; |
|
|
|
import java.util.stream.Collectors; |
|
|
@@ -22,7 +24,9 @@ import com.ruoyi.common.enums.RebotStatus; |
|
|
|
import com.ruoyi.common.utils.SecurityUtils; |
|
|
|
import com.ruoyi.common.utils.StringUtils; |
|
|
|
import com.ruoyi.common.utils.bean.BeanUtils; |
|
|
|
import org.quartz.*; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.scheduling.support.CronSequenceGenerator; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import com.ruoyi.business.mapper.ApplyPlanMapper; |
|
|
|
import com.ruoyi.business.domain.ApplyPlan; |
|
|
@@ -53,7 +57,7 @@ public class ApplyPlanServiceImpl extends ServiceImpl<ApplyPlanMapper, ApplyPlan |
|
|
|
@Override |
|
|
|
@Transactional |
|
|
|
public boolean runAppBySetTime() throws IllegalAccessException { |
|
|
|
ListRebotBO listRebotBO = new ListRebotBO(); |
|
|
|
ListRebotBO listRebotBO = new ListRebotBO(); |
|
|
|
listRebotBO.setStatus(RebotStatus.IDLE.getKey()); |
|
|
|
listRebotBO.setPage(1); |
|
|
|
listRebotBO.setSize(100); |
|
|
@@ -76,7 +80,7 @@ public class ApplyPlanServiceImpl extends ServiceImpl<ApplyPlanMapper, ApplyPlan |
|
|
|
//high 高 middle 中 low 低 |
|
|
|
applyStartBO.setPriority("high"); |
|
|
|
String planParams = e.getPlanParams(); |
|
|
|
if(StringUtils.isNotEmpty(planParams)){ |
|
|
|
if (StringUtils.isNotEmpty(planParams)) { |
|
|
|
JSONArray jsonArray = JSON.parseArray(planParams); |
|
|
|
// 直接转换成List |
|
|
|
List<ApplyStartBO.RobotParam> list = jsonArray.toJavaList(ApplyStartBO.RobotParam.class); |
|
|
@@ -92,8 +96,11 @@ public class ApplyPlanServiceImpl extends ServiceImpl<ApplyPlanMapper, ApplyPlan |
|
|
|
applyPlan.setTaskStatus(PlanRunStatus.CREATED.getKey()); |
|
|
|
this.updateById(applyPlan); |
|
|
|
Rebot updateRebot = new Rebot(); |
|
|
|
updateRebot.setUpdateBy("-1"); |
|
|
|
updateRebot.setUpdateTime(new Date()); |
|
|
|
updateRebot.setStatus(RebotStatus.RUNNING.getKey()); |
|
|
|
rebotService.lambdaUpdate().eq(Rebot::getRobotClientName, rebot.getRobotClientName()).update(updateRebot); |
|
|
|
applyPlanList.remove(e); |
|
|
|
} catch (IllegalAccessException ex) { |
|
|
|
throw new RuntimeException(ex); |
|
|
|
} |
|
|
@@ -104,6 +111,115 @@ public class ApplyPlanServiceImpl extends ServiceImpl<ApplyPlanMapper, ApplyPlan |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 表达式执行 |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public boolean runAppByCron() throws IllegalAccessException { |
|
|
|
ListRebotBO listRebotBO = new ListRebotBO(); |
|
|
|
listRebotBO.setStatus(RebotStatus.IDLE.getKey()); |
|
|
|
listRebotBO.setPage(1); |
|
|
|
listRebotBO.setSize(100); |
|
|
|
// 只获取空闲的机器人 |
|
|
|
List<ListRebotVO> listRebotVos = YinDaoHttpUtils.listRebot(listRebotBO); |
|
|
|
if (listRebotVos.isEmpty()) { |
|
|
|
log.error("暂无空闲机器人"); |
|
|
|
// 刷新机器人数据 |
|
|
|
// rebotService.syn(new Rebot()); |
|
|
|
return false; |
|
|
|
} |
|
|
|
List<ApplyPlan> applyPlanList = this.lambdaQuery().eq(ApplyPlan::getExcType, ExcTypeStatus.TREE.getKey()).list(); |
|
|
|
listRebotVos.forEach(rebot -> { |
|
|
|
applyPlanList.forEach(e -> { |
|
|
|
// 是否满足执行条件 |
|
|
|
boolean isExec = false; |
|
|
|
try { |
|
|
|
CronExpression cron = new CronExpression(e.getCronExpression()); |
|
|
|
// 如果该计划没有执行过 |
|
|
|
if (Objects.isNull(e.getLastExecTime())) { |
|
|
|
// 判断当前时间是否满足表达式 |
|
|
|
Date nextDate = cron.getNextValidTimeAfter(new Date()); |
|
|
|
if (new Date().compareTo(nextDate) > -1) { |
|
|
|
isExec = true; |
|
|
|
} |
|
|
|
} else { |
|
|
|
// 根据表达式返回下一个执行时间 |
|
|
|
Date nextDate = cron.getNextValidTimeAfter(e.getNextExecTime()); |
|
|
|
if (new Date().compareTo(nextDate) > -1) { |
|
|
|
isExec = true; |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (ParseException ex) { |
|
|
|
log.error(e.getPlanName() + "的时间表达式错误!"); |
|
|
|
throw new RuntimeException(ex); |
|
|
|
} |
|
|
|
|
|
|
|
// 该计划满足执行条件 |
|
|
|
if (isExec) { |
|
|
|
ApplyStartBO applyStartBO = new ApplyStartBO(); |
|
|
|
applyStartBO.setRobotUuid(e.getAppId()); |
|
|
|
applyStartBO.setAccountName(rebot.getRobotClientName()); |
|
|
|
applyStartBO.setManualTime(e.getManualTime()); |
|
|
|
//high 高 middle 中 low 低 |
|
|
|
applyStartBO.setPriority("high"); |
|
|
|
String planParams = e.getPlanParams(); |
|
|
|
if (StringUtils.isNotEmpty(planParams)) { |
|
|
|
JSONArray jsonArray = JSON.parseArray(planParams); |
|
|
|
// 直接转换成List |
|
|
|
List<ApplyStartBO.RobotParam> list = jsonArray.toJavaList(ApplyStartBO.RobotParam.class); |
|
|
|
applyStartBO.setPlanParamsList(list); |
|
|
|
} |
|
|
|
try { |
|
|
|
JobStartVO jobStartVO = YinDaoHttpUtils.appStart(applyStartBO); |
|
|
|
ApplyPlan applyPlan = new ApplyPlan(); |
|
|
|
applyPlan.setId(e.getId()); |
|
|
|
applyPlan.setUpdateBy("-1"); |
|
|
|
applyPlan.setUpdateTime(new Date()); |
|
|
|
applyPlan.setTaskUuid(jobStartVO.getJobUuid()); |
|
|
|
applyPlan.setTaskStatus(PlanRunStatus.CREATED.getKey()); |
|
|
|
// 根据表达式返回下一个执行时间 |
|
|
|
CronExpression cron = new CronExpression(e.getCronExpression()); |
|
|
|
Date nextDate = null; |
|
|
|
if (Objects.isNull(e.getLastExecTime())) { |
|
|
|
nextDate = cron.getNextValidTimeAfter(new Date()); |
|
|
|
} else { |
|
|
|
nextDate = cron.getNextValidTimeAfter(e.getLastExecTime()); |
|
|
|
} |
|
|
|
Date date = new Date(); |
|
|
|
applyPlan.setLastExecTime(date); |
|
|
|
applyPlan.setNextExecTime(nextDate); |
|
|
|
this.updateById(applyPlan); |
|
|
|
Rebot updateRebot = new Rebot(); |
|
|
|
updateRebot.setUpdateBy("-1"); |
|
|
|
updateRebot.setUpdateTime(new Date()); |
|
|
|
updateRebot.setStatus(RebotStatus.RUNNING.getKey()); |
|
|
|
rebotService.lambdaUpdate().eq(Rebot::getRobotClientName, rebot.getRobotClientName()).update(updateRebot); |
|
|
|
applyPlanList.remove(e); |
|
|
|
} catch (IllegalAccessException | ParseException ex) { |
|
|
|
throw new RuntimeException(ex); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 判断下次执行时间是否满足条件 |
|
|
|
* |
|
|
|
* @param cronExpression cronExpression |
|
|
|
* @param lastExecutionTime 上次执行时间 |
|
|
|
* @return |
|
|
|
* @throws ParseException |
|
|
|
*/ |
|
|
|
private static boolean shouldExecute(String cronExpression, ZonedDateTime lastExecutionTime) throws ParseException { |
|
|
|
CronExpression cron = new CronExpression(cronExpression); |
|
|
|
Date now = Date.from(lastExecutionTime.toInstant()); |
|
|
|
return cron.isSatisfiedBy(now); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 获取应用执行结果 |
|
|
|
* |
|
|
@@ -193,28 +309,35 @@ public class ApplyPlanServiceImpl extends ServiceImpl<ApplyPlanMapper, ApplyPlan |
|
|
|
// rebotService.syn(new Rebot()); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
Map<Long, List<ApplyPlan>> applyPlanMap = this.lambdaQuery().eq(ApplyPlan::getExcType, ExcTypeStatus.ONE.getKey()).isNull(ApplyPlan::getTaskUuid) |
|
|
|
.orderByAsc(ApplyPlan::getPriority).list().stream().collect(Collectors.groupingBy(ApplyPlan::getDeptId)); |
|
|
|
|
|
|
|
// 查询所有待执行的任务 |
|
|
|
if (null == applyStartBO && applyPlanMap.isEmpty()) { |
|
|
|
log.debug("没有等待执行的计划"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// 获取每个部门优先级最高的计划 |
|
|
|
List<ApplyPlan> applyPlans = new ArrayList<>(); |
|
|
|
applyPlanMap.forEach((k, v) -> { |
|
|
|
v.sort((Comparator.comparingInt(ApplyPlan::getPriority))); |
|
|
|
applyPlans.add(v.get(0)); |
|
|
|
}); |
|
|
|
if (null == applyStartBO && applyPlans.isEmpty()) { |
|
|
|
log.debug("没有等待执行的计划"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
applyPlans.sort((Comparator.comparingInt(ApplyPlan::getPriority))); |
|
|
|
|
|
|
|
int i = 0; |
|
|
|
for (ListRebotVO listRebotVO : listRebotVos) { |
|
|
|
ApplyPlan applyPlan = null; |
|
|
|
// 查询所有待执行的任务 |
|
|
|
Map<Long, List<ApplyPlan>> applyPlanMap = this.lambdaQuery().eq(ApplyPlan::getExcType, ExcTypeStatus.ONE.getKey()).isNull(ApplyPlan::getTaskUuid) |
|
|
|
.orderByAsc(ApplyPlan::getPriority).list().stream().collect(Collectors.groupingBy(ApplyPlan::getDeptId)); |
|
|
|
if (null == applyStartBO && applyPlanMap.isEmpty()) { |
|
|
|
log.debug("没有等待执行的计划"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
// 获取每个部门优先级最高的计划 |
|
|
|
List<ApplyPlan> applyPlans = new ArrayList<>(); |
|
|
|
applyPlanMap.forEach((k, v) -> { |
|
|
|
v.sort((Comparator.comparingInt(ApplyPlan::getPriority))); |
|
|
|
applyPlans.add(v.get(0)); |
|
|
|
}); |
|
|
|
if (null == applyStartBO && applyPlans.isEmpty()) { |
|
|
|
log.debug("没有等待执行的计划"); |
|
|
|
return false; |
|
|
|
if(applyPlans.isEmpty()){ |
|
|
|
log.debug("没有等待执行的计划!"); |
|
|
|
continue; |
|
|
|
} |
|
|
|
applyPlans.sort((Comparator.comparingInt(ApplyPlan::getPriority))); |
|
|
|
|
|
|
|
if (null == applyStartBO) { |
|
|
|
applyPlan = applyPlans.get(0); |
|
|
|
applyStartBO = new ApplyStartBO(); |
|
|
@@ -230,7 +353,7 @@ public class ApplyPlanServiceImpl extends ServiceImpl<ApplyPlanMapper, ApplyPlan |
|
|
|
//high 高 middle 中 low 低 |
|
|
|
applyStartBO.setPriority("high"); |
|
|
|
String planParams = applyPlan.getPlanParams(); |
|
|
|
if(StringUtils.isNotEmpty(planParams)){ |
|
|
|
if (StringUtils.isNotEmpty(planParams)) { |
|
|
|
JSONArray jsonArray = JSON.parseArray(planParams); |
|
|
|
// 直接转换成List |
|
|
|
List<ApplyStartBO.RobotParam> list = jsonArray.toJavaList(ApplyStartBO.RobotParam.class); |
|
|
@@ -240,6 +363,8 @@ public class ApplyPlanServiceImpl extends ServiceImpl<ApplyPlanMapper, ApplyPlan |
|
|
|
|
|
|
|
ApplyPlan updateApplyPlan = new ApplyPlan(); |
|
|
|
updateApplyPlan.setId(applyPlan.getId()); |
|
|
|
updateApplyPlan.setCreateBy("-1"); |
|
|
|
updateApplyPlan.setCreateTime(new Date()); |
|
|
|
updateApplyPlan.setUpdateBy("-1"); |
|
|
|
updateApplyPlan.setUpdateTime(new Date()); |
|
|
|
updateApplyPlan.setStartTime(new Date()); |
|
|
@@ -250,6 +375,7 @@ public class ApplyPlanServiceImpl extends ServiceImpl<ApplyPlanMapper, ApplyPlan |
|
|
|
updateApplyPlan.setManualTime(applyPlan.getManualTime()); |
|
|
|
updateApplyPlan.setDeptId(applyStartBO.getDeptId()); |
|
|
|
if (this.saveOrUpdate(updateApplyPlan)) { |
|
|
|
applyPlans.remove(0); |
|
|
|
i++; |
|
|
|
} |
|
|
|
} |
|
|
@@ -274,10 +400,9 @@ public class ApplyPlanServiceImpl extends ServiceImpl<ApplyPlanMapper, ApplyPlan |
|
|
|
applyPlan.setAppType(apply.getAppType()); |
|
|
|
applyPlan.setAppTypeName(apply.getAppTypeName()); |
|
|
|
applyPlan.setManualTime(apply.getManualTime()); |
|
|
|
if(addApplyPlanBO.getSupportParam() == 1){ |
|
|
|
applyPlan.setPlanParams(apply.getParam()); |
|
|
|
if (null != apply.getSupportParam() && apply.getSupportParam() == 1) { |
|
|
|
applyPlan.setPlanParams(addApplyPlanBO.getPlanParams()); |
|
|
|
} |
|
|
|
applyPlan.setManualTime(apply.getParam()); |
|
|
|
applyPlan.setDeptId(SecurityUtils.getDeptId()); |
|
|
|
|
|
|
|
// try { |
|
|
|