From 47b3107c454fa33f8f02ec064ba9459a4d07ca6e Mon Sep 17 00:00:00 2001 From: lcr <977192391@qq.com> Date: Sat, 15 Jun 2024 15:07:30 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AB=8B=E5=88=BB=E6=89=A7=E8=A1=8C=E6=B5=81?= =?UTF-8?q?=E7=A8=8B=E8=B7=91=E9=80=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/business/domain/ApplyPlan.java | 41 ++++- .../business/domain/bo/ApplyStartBO.java | 4 + .../business/service/IApplyPlanService.java | 15 +- .../service/impl/ApplyPlanServiceImpl.java | 160 +++++++++++++++--- .../ruoyi/business/util/YinDaoHttpUtils.java | 2 +- .../mapper/business/ApplyPlanMapper.xml | 25 ++- .../com/ruoyi/common/enums/PlanRunStatus.java | 41 +++++ .../java/com/ruoyi/quartz/task/RyTask.java | 24 ++- 8 files changed, 269 insertions(+), 43 deletions(-) create mode 100644 ruoyi-common/src/main/java/com/ruoyi/common/enums/PlanRunStatus.java diff --git a/ruoyi-business/src/main/java/com/ruoyi/business/domain/ApplyPlan.java b/ruoyi-business/src/main/java/com/ruoyi/business/domain/ApplyPlan.java index 7bfe626..ee2b30f 100644 --- a/ruoyi-business/src/main/java/com/ruoyi/business/domain/ApplyPlan.java +++ b/ruoyi-business/src/main/java/com/ruoyi/business/domain/ApplyPlan.java @@ -74,11 +74,6 @@ public class ApplyPlan extends BaseEntity @ApiModelProperty(name="appType",value = "应用类型枚举") private String appType; - /** 预计人工耗时 */ - @Excel(name = "预计人工耗时") - @ApiModelProperty(name="manualTime",value = "预计人工耗时") - private String manualTime; - /** 计划参数 */ @Excel(name = "计划参数") @ApiModelProperty(name="planParams",value = "计划参数") @@ -111,11 +106,47 @@ public class ApplyPlan extends BaseEntity @Excel(name = "执行表达式") @ApiModelProperty(name="cronExpression",value = "执行表达式") private String cronExpression; + + /** 任务执行状态 */ + @Excel(name = "任务执行状态") + @ApiModelProperty(name="taskStatus",value = "任务执行状态") + private String taskStatus; + /** 任务运行uuid */ @Excel(name = "任务运行uuid") @ApiModelProperty(name="taskUuid",value = "任务运行uuid") private String taskUuid; + /** 预计人工耗时 */ + @Excel(name = "预计人工耗时") + @ApiModelProperty(name="manualTime",value = "预计人工耗时") + private String manualTime; + + + + /** 任务开始运行的时间 */ + @JsonFormat(pattern = "yyyy-MM-dd") + @Excel(name = "任务开始运行的时间", width = 30, dateFormat = "yyyy-MM-dd") + @ApiModelProperty(name="startTime",value = "任务开始运行的时间") + private Date startTime; + + + /** 任务结束运行的时间 */ + @JsonFormat(pattern = "yyyy-MM-dd") + @Excel(name = "任务结束运行的时间", width = 30, dateFormat = "yyyy-MM-dd") + @ApiModelProperty(name="endTime",value = "任务结束运行的时间") + private Date endTime; + + /** 实际耗时 */ + @Excel(name = "实际耗时") + @ApiModelProperty(name="planTime",value = "实际耗时") + private String planTime; + + /** 节约时间 */ + @Excel(name = "节约时间") + @ApiModelProperty(name="timeSaving",value = "节约时间") + private String timeSaving; + @Override public String toString() { return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) diff --git a/ruoyi-business/src/main/java/com/ruoyi/business/domain/bo/ApplyStartBO.java b/ruoyi-business/src/main/java/com/ruoyi/business/domain/bo/ApplyStartBO.java index 20875d3..bad9fba 100644 --- a/ruoyi-business/src/main/java/com/ruoyi/business/domain/bo/ApplyStartBO.java +++ b/ruoyi-business/src/main/java/com/ruoyi/business/domain/bo/ApplyStartBO.java @@ -28,6 +28,10 @@ import java.util.List; @ApiModel(value = "ApplyStartBO", description = "启动应用") public class ApplyStartBO { private static final long serialVersionUID = 1L; + /** 预计人工耗时 */ + @Excel(name = "预计人工耗时") + @ApiModelProperty(name="manualTime",value = "预计人工耗时") + private String manualTime; /** * 指定的机器人名称 diff --git a/ruoyi-business/src/main/java/com/ruoyi/business/service/IApplyPlanService.java b/ruoyi-business/src/main/java/com/ruoyi/business/service/IApplyPlanService.java index 1c3c215..c3bb19f 100644 --- a/ruoyi-business/src/main/java/com/ruoyi/business/service/IApplyPlanService.java +++ b/ruoyi-business/src/main/java/com/ruoyi/business/service/IApplyPlanService.java @@ -5,6 +5,9 @@ import java.util.List; import com.ruoyi.business.domain.ApplyPlan; import com.baomidou.mybatisplus.extension.service.IService; import com.ruoyi.business.domain.bo.AddApplyPlanBO; +import com.ruoyi.business.domain.bo.ApplyStartBO; +import com.ruoyi.business.domain.bo.JobQueryBO; +import com.ruoyi.business.domain.vo.JobQueryVO; /** * 应用执行计划管理Service接口 @@ -15,11 +18,17 @@ import com.ruoyi.business.domain.bo.AddApplyPlanBO; public interface IApplyPlanService extends IService { /** - * 执行计划 - * @param addApplyPlanBO + * 更新计划应用执行结果 + * @param + * @return + */ + void queryAppStartResult() throws IllegalAccessException; + + /** + * 获取应用执行结果 * @return */ - boolean execPlan() throws IllegalAccessException; + boolean execPlan(ApplyStartBO applyStartBO) throws IllegalAccessException; /** * 创建计划 diff --git a/ruoyi-business/src/main/java/com/ruoyi/business/service/impl/ApplyPlanServiceImpl.java b/ruoyi-business/src/main/java/com/ruoyi/business/service/impl/ApplyPlanServiceImpl.java index 2542dbd..84aef2c 100644 --- a/ruoyi-business/src/main/java/com/ruoyi/business/service/impl/ApplyPlanServiceImpl.java +++ b/ruoyi-business/src/main/java/com/ruoyi/business/service/impl/ApplyPlanServiceImpl.java @@ -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 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 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 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 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 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 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 applyPlanList = new ArrayList<>(); Map> 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 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 diff --git a/ruoyi-business/src/main/java/com/ruoyi/business/util/YinDaoHttpUtils.java b/ruoyi-business/src/main/java/com/ruoyi/business/util/YinDaoHttpUtils.java index 7ddca8b..b40c7f7 100644 --- a/ruoyi-business/src/main/java/com/ruoyi/business/util/YinDaoHttpUtils.java +++ b/ruoyi-business/src/main/java/com/ruoyi/business/util/YinDaoHttpUtils.java @@ -42,7 +42,7 @@ public class YinDaoHttpUtils { private static final String APP_LIST = REQUEST_PREFIX + "/app/open/query/list"; // 启动应用 private static final String APP_START = REQUEST_PREFIX + "/dispatch/v2/job/start"; - // 启动应用 + // 查询应用运行结果 private static final String QUERY_APP_START_RESULT = REQUEST_PREFIX + "/dispatch/v2/job/query"; diff --git a/ruoyi-business/src/main/resources/mapper/business/ApplyPlanMapper.xml b/ruoyi-business/src/main/resources/mapper/business/ApplyPlanMapper.xml index fba1a5a..a73c436 100644 --- a/ruoyi-business/src/main/resources/mapper/business/ApplyPlanMapper.xml +++ b/ruoyi-business/src/main/resources/mapper/business/ApplyPlanMapper.xml @@ -21,8 +21,6 @@ - - @@ -35,8 +33,18 @@ + + + + + + + + + + @@ -61,14 +69,19 @@ plan.app_name, plan.app_id, plan.app_type, - plan.manual_time, plan.plan_params, plan.wait_timeout, plan.priority, plan.exc_type, plan.exc_time, plan.cron_expression, + plan.task_status, plan.task_uuid, + plan.manual_time, + plan.start_time, + plan.end_time, + plan.plan_time, + plan.time_saving, plan.create_by, plan.create_time, plan.update_by, @@ -94,7 +107,13 @@ and exc_type = #{excType} and exc_time = #{excTime} and cron_expression = #{cronExpression} + and task_status = #{taskStatus} and task_uuid = #{taskUuid} + and manual_time = #{manualTime} + and start_time = #{startTime} + and end_time = #{endTime} + and plan_time = #{planTime} + and time_saving = #{timeSaving} ${params.dataScope} diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/enums/PlanRunStatus.java b/ruoyi-common/src/main/java/com/ruoyi/common/enums/PlanRunStatus.java new file mode 100644 index 0000000..efbbe7f --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/enums/PlanRunStatus.java @@ -0,0 +1,41 @@ +package com.ruoyi.common.enums; + +/** + * 计划任务应用执行状态 + */ +public enum PlanRunStatus { + AWAIT_CREATE("await_create", "等待创建"), + CREATED("created", "已创建"), + WAITING("waiting", "等待调度"), + RUNNING("running", "运行中"), + FINISH("finish", "完成"), + STOPPING("stopping", "停止中"), + STOPPED("stopped", "已停止"), + ERROR("error", "异常"), + SKIPPED("skipped", "已跳过"), + CANCEL("cancel", "已取消"); + + private String key; + private String value; + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + PlanRunStatus(String key, String value) { + this.key = key; + this.value = value; + } +} diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RyTask.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RyTask.java index fe12948..3b77056 100644 --- a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RyTask.java +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RyTask.java @@ -1,8 +1,10 @@ package com.ruoyi.quartz.task; import com.ruoyi.business.domain.ApplyPlan; +import com.ruoyi.business.domain.bo.JobQueryBO; import com.ruoyi.business.service.IApplyPlanService; import com.ruoyi.common.enums.ExcTypeStatus; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.ruoyi.common.utils.StringUtils; @@ -13,28 +15,38 @@ import com.ruoyi.common.utils.StringUtils; * @author #author# */ @Component("ryTask") +@Slf4j public class RyTask { @Autowired private IApplyPlanService applyPlanService; public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) { - System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i)); + log.debug(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i)); } public void ryParams(String params) { - System.out.println("执行有参方法:" + params); + log.debug("执行有参方法:" + params); } public void ryNoParams() { - System.out.println("执行无参方法"); + log.debug("执行无参方法"); } /** * 按优先级执行立即执行的计划任务 */ public void runAppNow() throws IllegalAccessException { - System.out.println("执行顺序启动应用开始"); - applyPlanService.execPlan(); - System.out.println("执行顺序启动应用结束"); + log.debug("执行顺序启动应用开始"); + applyPlanService.execPlan(null); + log.debug("执行顺序启动应用结束"); + } + + /** + * 获取应用执行结果 + */ + public void getAppRunResult() throws IllegalAccessException { + log.debug("更新应用执行结果开始"); + applyPlanService.queryAppStartResult(); + log.debug("更新应用执行结果结束"); } }