Просмотр исходного кода

立刻执行流程跑通

pull/4/head
lcr 5 месяцев назад
Родитель
Сommit
47b3107c45
8 измененных файлов: 269 добавлений и 43 удалений
  1. +36
    -5
      ruoyi-business/src/main/java/com/ruoyi/business/domain/ApplyPlan.java
  2. +4
    -0
      ruoyi-business/src/main/java/com/ruoyi/business/domain/bo/ApplyStartBO.java
  3. +12
    -3
      ruoyi-business/src/main/java/com/ruoyi/business/service/IApplyPlanService.java
  4. +135
    -25
      ruoyi-business/src/main/java/com/ruoyi/business/service/impl/ApplyPlanServiceImpl.java
  5. +1
    -1
      ruoyi-business/src/main/java/com/ruoyi/business/util/YinDaoHttpUtils.java
  6. +22
    -3
      ruoyi-business/src/main/resources/mapper/business/ApplyPlanMapper.xml
  7. +41
    -0
      ruoyi-common/src/main/java/com/ruoyi/common/enums/PlanRunStatus.java
  8. +18
    -6
      ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RyTask.java

+ 36
- 5
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)


+ 4
- 0
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;

/**
* 指定的机器人名称


+ 12
- 3
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<ApplyPlan> {

/**
* 执行计划
* @param addApplyPlanBO
* 更新计划应用执行结果
* @param
* @return
*/
void queryAppStartResult() throws IllegalAccessException;

/**
* 获取应用执行结果
* @return
*/
boolean execPlan() throws IllegalAccessException;
boolean execPlan(ApplyStartBO applyStartBO) throws IllegalAccessException;

/**
* 创建计划


+ 135
- 25
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<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


+ 1
- 1
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";




+ 22
- 3
ruoyi-business/src/main/resources/mapper/business/ApplyPlanMapper.xml Просмотреть файл

@@ -21,8 +21,6 @@
<result property="appId" column="app_id"/>
<!-- 应用类型枚举(app:应用 activity:指令) -->
<result property="appType" column="app_type"/>
<!-- 预计人工耗时 -->
<result property="manualTime" column="manual_time"/>
<!-- 计划参数 -->
<result property="planParams" column="plan_params"/>
<!-- 等待超时时间 -->
@@ -35,8 +33,18 @@
<result property="excTime" column="exc_time"/>
<!-- 执行表达式 -->
<result property="cronExpression" column="cron_expression"/>
<!-- 任务执行状态 -->
<result property="taskStatus" column="task_status" />
<!-- 任务运行uuid -->
<result property="taskUuid" column="task_uuid" />
<!-- 任务开始运行的时间 -->
<result property="startTime" column="start_time" />
<!-- 任务结束运行的时间 -->
<result property="endTime" column="end_time" />
<!-- 实际耗时 -->
<result property="planTime" column="plan_time" />
<!-- 节约时间 -->
<result property="timeSaving" column="time_saving" />
<!-- 创建者 -->
<result property="createBy" column="create_by"/>
<!-- 创建时间 -->
@@ -61,14 +69,19 @@
plan.app_name,<!-- 应用名称 -->
plan.app_id,<!-- appid -->
plan.app_type,<!-- 应用类型枚举(app:应用 activity:指令) -->
plan.manual_time,<!-- 预计人工耗时 -->
plan.plan_params,<!-- 计划参数 -->
plan.wait_timeout,<!-- 等待超时时间 -->
plan.priority,<!-- 优先级 -->
plan.exc_type,<!-- 执行类型(0立即执行 1指定时间执行 2周期执行) -->
plan.exc_time,<!-- 执行时间 -->
plan.cron_expression,<!-- 执行表达式 -->
plan.task_status,<!-- 任务执行状态 -->
plan.task_uuid,<!-- 任务运行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 @@
<if test="excType != null and excType != ''"> and exc_type = #{excType}</if>
<if test="excTime != null "> and exc_time = #{excTime}</if>
<if test="cronExpression != null and cronExpression != ''"> and cron_expression = #{cronExpression}</if>
<if test="taskStatus != null and taskStatus != ''"> and task_status = #{taskStatus}</if>
<if test="taskUuid != null and taskUuid != ''"> and task_uuid = #{taskUuid}</if>
<if test="manualTime != null and manualTime != ''"> and manual_time = #{manualTime}</if>
<if test="startTime != null "> and start_time = #{startTime}</if>
<if test="endTime != null "> and end_time = #{endTime}</if>
<if test="planTime != null and planTime != ''"> and plan_time = #{planTime}</if>
<if test="timeSaving != null and timeSaving != ''"> and time_saving = #{timeSaving}</if>
</where>
<!-- 数据范围过滤 -->
${params.dataScope}


+ 41
- 0
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;
}
}

+ 18
- 6
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("更新应用执行结果结束");
}
}

Загрузка…
Отмена
Сохранить