fix(任务中心): 任务中心的清理按照创建时间处理

--bug=1049022 --user=宋昌昌 【任务中心】-系统任务保留时间不生效,超出保留时间的任务没有被清理 https://www.tapd.cn/55049933/s/1611968
This commit is contained in:
song-cc-rock 2024-11-19 13:44:57 +08:00 committed by 刘瑞斌
parent 953a764356
commit 49e605e180
4 changed files with 18 additions and 9 deletions

View File

@ -5,7 +5,9 @@ import io.metersphere.api.mapper.ApiReportRelateTaskMapper;
import io.metersphere.sdk.constants.ProjectApplicationType;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.SubListUtils;
import io.metersphere.system.mapper.ExtExecTaskItemMapper;
import io.metersphere.system.domain.ExecTaskItem;
import io.metersphere.system.domain.ExecTaskItemExample;
import io.metersphere.system.mapper.ExecTaskItemMapper;
import io.metersphere.system.mapper.ExtExecTaskMapper;
import io.metersphere.system.service.BaseCleanUpReport;
import jakarta.annotation.Resource;
@ -14,6 +16,7 @@ import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -29,7 +32,7 @@ public class CleanupTaskResultServiceImpl implements BaseCleanUpReport {
@Resource
private ExtExecTaskMapper extExecTaskMapper;
@Resource
private ExtExecTaskItemMapper extExecTaskItemMapper;
private ExecTaskItemMapper execTaskItemMapper;
@Resource
private ApiReportRelateTaskMapper apiReportRelateTaskMapper;
@ -40,7 +43,13 @@ public class CleanupTaskResultServiceImpl implements BaseCleanUpReport {
String expr = map.get(ProjectApplicationType.TASK.TASK_CLEAN_REPORT.name());
long timeMills = getCleanDate(expr);
List<String> cleanTaskIds = extExecTaskMapper.getTaskIdsByTime(timeMills, projectId);
List<String> cleanTaskItemIds = extExecTaskItemMapper.getTaskItemIdsByTime(timeMills, projectId);
List<ExecTaskItem> execTaskItems = new ArrayList<>();
if (CollectionUtils.isNotEmpty(cleanTaskIds)) {
ExecTaskItemExample itemExample = new ExecTaskItemExample();
itemExample.createCriteria().andTaskIdIn(cleanTaskIds);
execTaskItems = execTaskItemMapper.selectByExample(itemExample);
}
List<String> cleanTaskItemIds = execTaskItems.stream().map(ExecTaskItem::getId).toList();
List<String> cleanIds = ListUtils.union(cleanTaskIds, cleanTaskItemIds);
LogUtils.info("清理当前项目[" + projectId + "]任务中心执行结果, 共[" + cleanIds.size() + "]条");
if (CollectionUtils.isNotEmpty(cleanIds)) {

View File

@ -8,7 +8,6 @@ import io.metersphere.system.domain.ExecTaskItem;
import io.metersphere.system.domain.ExecTaskItemExample;
import io.metersphere.system.mapper.ExecTaskItemMapper;
import io.metersphere.system.mapper.ExecTaskMapper;
import io.metersphere.system.mapper.ExtExecTaskItemMapper;
import io.metersphere.system.mapper.ExtExecTaskMapper;
import io.metersphere.system.service.BaseCleanUpReport;
import jakarta.annotation.Resource;
@ -31,8 +30,6 @@ public class CleanupTaskServiceImpl implements BaseCleanUpReport {
@Resource
private ExtExecTaskMapper extExecTaskMapper;
@Resource
private ExtExecTaskItemMapper extExecTaskItemMapper;
@Resource
private ExecTaskMapper execTaskMapper;
@Resource
private ExecTaskItemMapper execTaskItemMapper;
@ -44,7 +41,10 @@ public class CleanupTaskServiceImpl implements BaseCleanUpReport {
String expr = map.get(ProjectApplicationType.TASK.TASK_RECORD.name());
long timeMills = getCleanDate(expr);
List<String> cleanTaskIds = extExecTaskMapper.getTaskIdsByTime(timeMills, projectId);
List<String> cleanTaskItemIds = extExecTaskItemMapper.getTaskItemIdsByTime(timeMills, projectId);
ExecTaskItemExample itemExample = new ExecTaskItemExample();
itemExample.createCriteria().andTaskIdIn(cleanTaskIds);
List<ExecTaskItem> execTaskItems = execTaskItemMapper.selectByExample(itemExample);
List<String> cleanTaskItemIds = execTaskItems.stream().map(ExecTaskItem::getId).toList();
if (CollectionUtils.isNotEmpty(cleanTaskIds)) {
ExecTaskExample example = new ExecTaskExample();
example.createCriteria().andIdIn(cleanTaskIds);

View File

@ -40,7 +40,7 @@ public class CleanupTaskResultTests {
task.setStartTime(1700000000000L);
task.setResult("PENDING");
task.setCreateUser("admin");
task.setCreateTime(System.currentTimeMillis());
task.setCreateTime(1700000000000L);
execTaskMapper.insertSelective(task);
}

View File

@ -131,7 +131,7 @@
</update>
<select id="getTaskIdsByTime" resultType="java.lang.String">
select id from exec_task where project_id = #{projectId} and start_time &lt;= #{timeMills} and deleted = false
select id from exec_task where project_id = #{projectId} and exec_task.create_time &lt;= #{timeMills} and deleted = false
</select>