|
|
@@ -1,17 +1,13 @@ |
|
|
|
package com.ruoyi.business.service.impl; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.*; |
|
|
|
import java.util.concurrent.atomic.AtomicBoolean; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
import com.ruoyi.business.domain.Apply; |
|
|
|
import com.ruoyi.business.domain.Rebot; |
|
|
|
import com.ruoyi.business.domain.bo.AddApplyPlanBO; |
|
|
|
import com.ruoyi.business.domain.bo.ApplyStartBO; |
|
|
|
import com.ruoyi.business.domain.bo.ListApplyBO; |
|
|
|
import com.ruoyi.business.domain.bo.ListRebotBO; |
|
|
|
import com.ruoyi.business.domain.bo.*; |
|
|
|
import com.ruoyi.business.domain.vo.JobQueryVO; |
|
|
|
import com.ruoyi.business.domain.vo.JobStartVO; |
|
|
|
import com.ruoyi.business.domain.vo.ListRebotVO; |
|
|
|
import com.ruoyi.business.service.IApplyService; |
|
|
@@ -19,7 +15,8 @@ import com.ruoyi.business.service.IRebotService; |
|
|
|
import com.ruoyi.business.util.YinDaoHttpUtils; |
|
|
|
import com.ruoyi.common.annotation.DataScope; |
|
|
|
import com.ruoyi.common.enums.ExcTypeStatus; |
|
|
|
import com.ruoyi.common.utils.DateUtils; |
|
|
|
import com.ruoyi.common.enums.PlanRunStatus; |
|
|
|
import com.ruoyi.common.enums.RebotStatus; |
|
|
|
import com.ruoyi.common.utils.SecurityUtils; |
|
|
|
import com.ruoyi.common.utils.bean.BeanUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
@@ -30,8 +27,6 @@ import com.ruoyi.business.service.IApplyPlanService; |
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import static cn.hutool.json.XMLTokener.entity; |
|
|
|
|
|
|
|
/** |
|
|
|
* 应用执行计划管理Service业务层处理 |
|
|
|
* |
|
|
@@ -46,6 +41,75 @@ public class ApplyPlanServiceImpl extends ServiceImpl<ApplyPlanMapper, ApplyPlan |
|
|
|
@Autowired |
|
|
|
private IRebotService rebotService; |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取应用执行结果 |
|
|
|
* |
|
|
|
* @return |
|
|
|
* @throws IllegalAccessException |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Transactional |
|
|
|
public void queryAppStartResult() throws IllegalAccessException { |
|
|
|
// 不查询的状态 |
|
|
|
List<String> noStatus = new ArrayList<>(); |
|
|
|
noStatus.add(PlanRunStatus.FINISH.getKey()); |
|
|
|
noStatus.add(PlanRunStatus.WAITING.getKey()); |
|
|
|
noStatus.add(PlanRunStatus.STOPPED.getKey()); |
|
|
|
noStatus.add(PlanRunStatus.ERROR.getKey()); |
|
|
|
noStatus.add(PlanRunStatus.SKIPPED.getKey()); |
|
|
|
noStatus.add(PlanRunStatus.CANCEL.getKey()); |
|
|
|
// 查询任务运行状态非空非终态并且已经创建的任务 |
|
|
|
List<ApplyPlan> list = this.lambdaQuery().isNotNull(ApplyPlan::getTaskUuid).and(e -> { |
|
|
|
e.notIn(ApplyPlan::getTaskStatus, noStatus).isNotNull(ApplyPlan::getTaskStatus); |
|
|
|
}).orderByAsc(ApplyPlan::getPriority).list(); |
|
|
|
if (list.isEmpty()) { |
|
|
|
log.debug("没有需要更新状态的计划任务"); |
|
|
|
return; |
|
|
|
} |
|
|
|
List<JobQueryVO> jobQueryVOS = new ArrayList<>(); |
|
|
|
for (int i = 0; i < list.size(); i++) { |
|
|
|
ApplyPlan applyPlan = list.get(i); |
|
|
|
JobQueryBO jobQueryBO = new JobQueryBO(); |
|
|
|
jobQueryBO.setJobUuid(applyPlan.getTaskUuid()); |
|
|
|
JobQueryVO jobQueryVO = YinDaoHttpUtils.queryAppStartResult(jobQueryBO); |
|
|
|
// 如果状态不一致,更新最新状态 |
|
|
|
if (jobQueryVO != null && !applyPlan.getTaskStatus().equals(jobQueryVO.getStatus())) { |
|
|
|
// 修改的任务计划 |
|
|
|
ApplyPlan updateApplyPlan = new ApplyPlan(); |
|
|
|
updateApplyPlan.setTaskStatus(jobQueryVO.getStatus()); |
|
|
|
if (null != jobQueryVO.getEndTime()) { |
|
|
|
updateApplyPlan.setEndTime(jobQueryVO.getEndTime()); |
|
|
|
updateApplyPlan.setStartTime(jobQueryVO.getStartTime()); |
|
|
|
updateApplyPlan.setEndTime(jobQueryVO.getEndTime()); |
|
|
|
long diff = updateApplyPlan.getEndTime().getTime() - updateApplyPlan.getStartTime().getTime(); |
|
|
|
long planTime = diff / (60 * 1000); |
|
|
|
updateApplyPlan.setPlanTime(planTime + ""); |
|
|
|
updateApplyPlan.setTimeSaving((Long.parseLong(applyPlan.getManualTime()) - planTime) + ""); |
|
|
|
} |
|
|
|
updateApplyPlan.setId(applyPlan.getId()); |
|
|
|
updateApplyPlan.setUpdateBy("-1"); |
|
|
|
updateApplyPlan.setUpdateTime(new Date()); |
|
|
|
this.updateById(updateApplyPlan); |
|
|
|
} |
|
|
|
// 如果是终态改变机器人状态 |
|
|
|
List<String> finishStatus = Arrays.asList(PlanRunStatus.FINISH.getKey(), PlanRunStatus.STOPPED.getKey(), PlanRunStatus.ERROR.getKey(), PlanRunStatus.SKIPPED.getKey(), PlanRunStatus.CANCEL.getKey()); |
|
|
|
if(finishStatus.contains(jobQueryVO.getStatus()) ){ |
|
|
|
Rebot rebot = new Rebot(); |
|
|
|
rebot.setStatus(RebotStatus.IDLE.getKey()); |
|
|
|
rebot.setUpdateBy("-1"); |
|
|
|
rebot.setUpdateTime(new Date()); |
|
|
|
rebot.setStatus(RebotStatus.IDLE.getKey()); |
|
|
|
rebotService.lambdaUpdate().eq(Rebot::getRobotClientName,jobQueryVO.getRobotClientName()).update(rebot); |
|
|
|
}else if(PlanRunStatus.RUNNING.getKey().equals(jobQueryVO.getStatus())){ |
|
|
|
Rebot rebot = new Rebot(); |
|
|
|
rebot.setStatus(RebotStatus.RUNNING.getKey()); |
|
|
|
rebot.setUpdateBy("-1"); |
|
|
|
rebot.setUpdateTime(new Date()); |
|
|
|
rebotService.lambdaUpdate().eq(Rebot::getRobotClientName,jobQueryVO.getRobotClientName()).update(rebot); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 执行计划(针对立即执行的计划) |
|
|
|
* |
|
|
@@ -53,7 +117,7 @@ public class ApplyPlanServiceImpl extends ServiceImpl<ApplyPlanMapper, ApplyPlan |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Transactional |
|
|
|
public boolean execPlan() throws IllegalAccessException { |
|
|
|
public boolean execPlan(ApplyStartBO applyStartBO) throws IllegalAccessException { |
|
|
|
ListRebotBO listRebotBO = new ListRebotBO(); |
|
|
|
listRebotBO.setStatus("idle"); |
|
|
|
listRebotBO.setPage(1); |
|
|
@@ -65,34 +129,55 @@ public class ApplyPlanServiceImpl extends ServiceImpl<ApplyPlanMapper, ApplyPlan |
|
|
|
// 刷新机器人数据 |
|
|
|
// rebotService.syn(new Rebot()); |
|
|
|
} |
|
|
|
int i = 0; |
|
|
|
for (ListRebotVO listRebotVO : listRebotVOS) { |
|
|
|
List<ApplyPlan> list = this.lambdaQuery().eq(ApplyPlan::getExcType, ExcTypeStatus.ONE.getKey()).isNull(ApplyPlan::getTaskUuid).orderByAsc(ApplyPlan::getPriority).list(); |
|
|
|
if (list.isEmpty()) { |
|
|
|
log.debug("没有等待执行的计划"); |
|
|
|
break; |
|
|
|
ApplyPlan applyPlan = null; |
|
|
|
if (null == applyStartBO) { |
|
|
|
List<ApplyPlan> list = this.lambdaQuery().eq(ApplyPlan::getExcType, ExcTypeStatus.ONE.getKey()).isNull(ApplyPlan::getTaskUuid).orderByAsc(ApplyPlan::getPriority).list(); |
|
|
|
if (list.isEmpty()) { |
|
|
|
log.debug("没有等待执行的计划"); |
|
|
|
break; |
|
|
|
} |
|
|
|
applyPlan = list.get(0); |
|
|
|
applyStartBO = new ApplyStartBO(); |
|
|
|
applyStartBO.setRobotUuid(applyPlan.getAppId()); |
|
|
|
}else{ |
|
|
|
|
|
|
|
applyPlan = new ApplyPlan(); |
|
|
|
BeanUtils.copyBeanProp(applyPlan,applyStartBO); |
|
|
|
applyStartBO.setRobotUuid(applyStartBO.getRobotUuid()); |
|
|
|
applyStartBO.setManualTime(applyStartBO.getManualTime()); |
|
|
|
} |
|
|
|
ApplyStartBO applyStartBO = new ApplyStartBO(); |
|
|
|
ApplyPlan applyPlan = list.get(0); |
|
|
|
applyStartBO.setRobotUuid(applyPlan.getAppId()); |
|
|
|
applyStartBO.setAccountName(listRebotVO.getRobotClientName()); |
|
|
|
//high 高 middle 中 low 低 |
|
|
|
applyStartBO.setPriority("high"); |
|
|
|
JobStartVO jobStartVO = YinDaoHttpUtils.appStart(applyStartBO); |
|
|
|
|
|
|
|
ApplyPlan updateApplyPlan = new ApplyPlan(); |
|
|
|
updateApplyPlan.setId(applyPlan.getId()); |
|
|
|
updateApplyPlan.setUpdateBy("-1"); |
|
|
|
updateApplyPlan.setUpdateTime(new Date()); |
|
|
|
updateApplyPlan.setStartTime(new Date()); |
|
|
|
updateApplyPlan.setTaskUuid(jobStartVO.getJobUuid()); |
|
|
|
this.updateById(updateApplyPlan); |
|
|
|
updateApplyPlan.setPlanName(applyPlan.getPlanName()); |
|
|
|
updateApplyPlan.setTaskStatus(PlanRunStatus.CREATED.getKey()); |
|
|
|
updateApplyPlan.setRobotName(listRebotVO.getRobotClientName()); |
|
|
|
updateApplyPlan.setManualTime(applyPlan.getManualTime()); |
|
|
|
if(this.saveOrUpdate(updateApplyPlan)){ |
|
|
|
i++; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
return i>0; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional |
|
|
|
public boolean save(AddApplyPlanBO addApplyPlanBO) { |
|
|
|
List<ApplyPlan> applyPlanList = new ArrayList<>(); |
|
|
|
Map<String, List<Apply>> appMap = applyService.lambdaQuery().in(Apply::getAppId, addApplyPlanBO.getAppId()).list().stream().collect(Collectors.groupingBy(Apply::getAppId)); |
|
|
|
// 是否有空闲机器人并且创建计划成功 |
|
|
|
AtomicBoolean insert = new AtomicBoolean(false); |
|
|
|
|
|
|
|
addApplyPlanBO.getAppId().forEach(e -> { |
|
|
|
ApplyPlan applyPlan = new ApplyPlan(); |
|
|
|
BeanUtils.copyBeanProp(applyPlan, addApplyPlanBO); |
|
|
@@ -101,10 +186,35 @@ public class ApplyPlanServiceImpl extends ServiceImpl<ApplyPlanMapper, ApplyPlan |
|
|
|
applyPlan.setAppId(apply.getAppId()); |
|
|
|
applyPlan.setAppType(apply.getAppType()); |
|
|
|
applyPlan.setAppTypeName(apply.getAppTypeName()); |
|
|
|
applyPlanList.add(applyPlan); |
|
|
|
}); |
|
|
|
applyPlan.setManualTime(apply.getManualTime()); |
|
|
|
|
|
|
|
return super.saveBatch(applyPlanList); |
|
|
|
// try { |
|
|
|
// ApplyStartBO applyStartBO = new ApplyStartBO(); |
|
|
|
// applyStartBO.setRobotUuid(applyPlan.getAppId()); |
|
|
|
// |
|
|
|
// ListRebotBO listRebotBO = new ListRebotBO(); |
|
|
|
// listRebotBO.setStatus("idle"); |
|
|
|
// listRebotBO.setPage(1); |
|
|
|
// listRebotBO.setSize(100); |
|
|
|
// // 只获取空闲的机器人 |
|
|
|
// List<ListRebotVO> listRebotVOS = YinDaoHttpUtils.listRebot(listRebotBO); |
|
|
|
// if (listRebotVOS.isEmpty()) { |
|
|
|
// log.debug("创建计划成功,但暂无空闲机器人"); |
|
|
|
// } |
|
|
|
// applyStartBO.setAccountName(listRebotVOS.get(0).getRobotClientName()); |
|
|
|
// //high 高 middle 中 low 低 |
|
|
|
// applyStartBO.setPriority("high"); |
|
|
|
// applyStartBO.setManualTime(applyPlan.getManualTime()); |
|
|
|
// insert.set(execPlan(applyStartBO)); |
|
|
|
// } catch (Exception ex) { |
|
|
|
// log.error("分配机器人异常:" + ex.getMessage()); |
|
|
|
// } |
|
|
|
if(!insert.get()){ |
|
|
|
applyPlan.setTaskStatus(PlanRunStatus.AWAIT_CREATE.getKey()); |
|
|
|
applyPlanList.add(applyPlan); |
|
|
|
} |
|
|
|
}); |
|
|
|
return this.saveBatch(applyPlanList) || insert.get(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|