定时任务执行基础逻辑
This commit is contained in:
@@ -39,10 +39,10 @@ public class TestTask extends BaseEntity
|
||||
private String crontab;
|
||||
|
||||
/** 定时任务开关 */
|
||||
@Excel(name = "定时任务开关")
|
||||
@Excel(name = "定时任务开关:0打开;2关闭")
|
||||
private Integer status;
|
||||
|
||||
/** 失败重试开关 */
|
||||
/** 失败重试开关:0打开;2关闭 */
|
||||
@Excel(name = "失败重试开关")
|
||||
private Integer retry;
|
||||
|
||||
@@ -50,7 +50,7 @@ public class TestTask extends BaseEntity
|
||||
@Excel(name = "失败重试次数")
|
||||
private Long retryCount;
|
||||
|
||||
/** 并行开关 */
|
||||
/** 并行开关:0打开;2关闭 */
|
||||
@Excel(name = "并行开关")
|
||||
private Integer async;
|
||||
|
||||
|
||||
@@ -29,9 +29,9 @@ public class TestTaskResult extends BaseEntity
|
||||
@Excel(name = "任务id")
|
||||
private Long taskId;
|
||||
|
||||
/** 执行时间 */
|
||||
/** 触发时间 */
|
||||
@JsonFormat(pattern = "yyyy-MM-dd")
|
||||
@Excel(name = "执行时间", width = 30, dateFormat = "yyyy-MM-dd")
|
||||
@Excel(name = "触发时间", width = 30, dateFormat = "yyyy-MM-dd")
|
||||
private Date triggerTime;
|
||||
|
||||
/** 执行时间 */
|
||||
@@ -41,7 +41,7 @@ public class TestTaskResult extends BaseEntity
|
||||
|
||||
/** 触发方式:1-定时任务;2-手动 */
|
||||
@Excel(name = "触发方式:1-定时任务;2-手动")
|
||||
private Long triggerType;
|
||||
private Integer triggerType;
|
||||
|
||||
/** 环境 */
|
||||
@Excel(name = "环境")
|
||||
@@ -49,11 +49,11 @@ public class TestTaskResult extends BaseEntity
|
||||
|
||||
/** 定时任务执行状态(1:成功:2:失败) */
|
||||
@Excel(name = "定时任务执行状态", readConverterExp = "1=:成功:2:失败")
|
||||
private Long status;
|
||||
private Integer status;
|
||||
|
||||
/** 发送状态(0:未发送:1:已发送) */
|
||||
@Excel(name = "发送状态", readConverterExp = "0=:未发送:1:已发送")
|
||||
private Long sendStatus;
|
||||
private Integer sendStatus;
|
||||
|
||||
/** 执行耗时毫秒 */
|
||||
@Excel(name = "执行耗时毫秒")
|
||||
@@ -61,11 +61,11 @@ public class TestTaskResult extends BaseEntity
|
||||
|
||||
/** 用例数量 */
|
||||
@Excel(name = "用例数量")
|
||||
private Long caseCount;
|
||||
private Integer caseCount;
|
||||
|
||||
/** 用例步骤数量 */
|
||||
@Excel(name = "用例步骤数量")
|
||||
private Long caseStepCount;
|
||||
private Integer caseStepCount;
|
||||
|
||||
/** 结果描述 */
|
||||
@Excel(name = "结果描述")
|
||||
|
||||
@@ -59,4 +59,11 @@ public interface TestTaskCaseMapper
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteTestTaskCaseByTaskIds(Long[] taskIds);
|
||||
|
||||
/**
|
||||
* 根据taskId查询自动化测试和用例关联表List
|
||||
* @param taskId
|
||||
* @return
|
||||
*/
|
||||
public List<TestTaskCase> selectTestTaskCaseListByTaskId(Long taskId);
|
||||
}
|
||||
|
||||
@@ -1,24 +1,39 @@
|
||||
package com.test.test.service.impl;
|
||||
|
||||
import com.test.common.utils.DateUtils;
|
||||
import com.test.test.domain.TestTask;
|
||||
import com.test.test.domain.*;
|
||||
import com.test.test.domain.qo.GroupIdQO;
|
||||
import com.test.test.mapper.TestTaskMapper;
|
||||
import com.test.test.mapper.*;
|
||||
import com.test.test.service.ITestTaskService;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 自动化测试Service业务层处理
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class TestTaskServiceImpl implements ITestTaskService {
|
||||
|
||||
@Resource
|
||||
private TestTaskMapper testTaskMapper;
|
||||
|
||||
@Resource
|
||||
private TestTaskCaseMapper testTaskCaseMapper;
|
||||
|
||||
@Resource
|
||||
private TestCaseMapper testCaseMapper;
|
||||
|
||||
@Resource
|
||||
private TestCaseStepMapper testCaseStepMapper;
|
||||
|
||||
@Resource
|
||||
private TestTaskResultMapper testTaskResultMapper;
|
||||
|
||||
/**
|
||||
* 查询自动化测试
|
||||
*/
|
||||
@@ -67,4 +82,59 @@ public class TestTaskServiceImpl implements ITestTaskService {
|
||||
public int deleteTestTaskById(Long id) {
|
||||
return testTaskMapper.deleteTestTaskById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据测试任务id执行测试定时任务
|
||||
* @param id 测试定时任务id
|
||||
* @param isImmediateRun 是否立即执行
|
||||
* @param triggerType 触发方式:1-定时任务;2-手动
|
||||
* @param environment 环境
|
||||
* @return 是否成功
|
||||
*/
|
||||
@Override
|
||||
public boolean executeTestTaskById(Long id, boolean isImmediateRun, Integer triggerType, String environment) {
|
||||
TestTask testTask = this.selectTestTaskById(id);
|
||||
if (testTask == null || testTask.getStatus() == null
|
||||
|| testTask.getStatus() > 0 || "2".equals(testTask.getDelFlag())) {
|
||||
log.error("定时任务已删除或未启用,不能执行!");
|
||||
return false;
|
||||
}
|
||||
TestTaskResult testTaskResult = new TestTaskResult();
|
||||
testTaskResult.setTaskId(id);
|
||||
testTaskResult.setTriggerTime(DateUtils.getNowDate());
|
||||
testTaskResult.setTriggerType(triggerType);
|
||||
testTaskResult.setEnvironment(environment);
|
||||
String crontab = testTask.getCrontab();
|
||||
Integer async = testTask.getAsync();
|
||||
Integer retry = testTask.getRetry();
|
||||
Long retryCount = testTask.getRetryCount();
|
||||
int caseStepCount = 0;
|
||||
// 查询关联的测试用例列表
|
||||
List<TestTaskCase> testTaskCaseList = testTaskCaseMapper.selectTestTaskCaseListByTaskId(id);
|
||||
List<TestCase> testCaseList = new ArrayList<>();
|
||||
for (TestTaskCase testTaskCase : testTaskCaseList) {
|
||||
TestCase testCase = testCaseMapper.selectTestCaseById(testTaskCase.getCaseId());
|
||||
if (testCase != null) {
|
||||
testCaseList.add(testCase);
|
||||
TestCaseStep testCaseStep = new TestCaseStep();
|
||||
testCaseStep.setCaseId(testCase.getId());
|
||||
testCaseStep.setDelFlag("0");
|
||||
caseStepCount += testCaseStepMapper.selectTestCaseStepList(testCaseStep).size();
|
||||
}
|
||||
}
|
||||
int caseCount = testCaseList.size();
|
||||
testTaskResult.setStartTime(DateUtils.getNowDate());
|
||||
long startTime = System.currentTimeMillis();
|
||||
// 开始执行定时任务逻辑。。。
|
||||
|
||||
testTaskResult.setStatus(1);
|
||||
testTaskResult.setCostTime(System.currentTimeMillis() - startTime);
|
||||
testTaskResult.setCaseCount(caseCount);
|
||||
testTaskResult.setCaseStepCount(caseStepCount);
|
||||
String result_desc = "";
|
||||
testTaskResult.setResultDesc(result_desc);
|
||||
testTaskResult.setCreateTime(DateUtils.getNowDate());
|
||||
testTaskResultMapper.insertTestTaskResult(testTaskResult);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,4 +59,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
#{taskId}
|
||||
</foreach>
|
||||
</delete>
|
||||
|
||||
<select id="selectTestTaskCaseListByTaskId" parameterType="Long" resultMap="TestTaskCaseResult">
|
||||
<include refid="selectTestTaskCaseVo"/>
|
||||
where task_id = #{taskId}
|
||||
ORDER BY sort
|
||||
</select>
|
||||
</mapper>
|
||||
|
||||
Reference in New Issue
Block a user