feat(接口测试): 查询后台任务的上次和下次执行时间

This commit is contained in:
AgAngle 2024-10-21 18:31:57 +08:00 committed by Craftsman
parent 5dfe0330bc
commit 6315e15e90
4 changed files with 42 additions and 3 deletions

View File

@ -45,6 +45,9 @@ public class TaskHubScheduleDTO implements Serializable {
@Schema(description = "运行规则cron表达式")
private String value;
@Schema(description = "上次完成时间")
private Long lastTime;
@Schema(description = "下次执行时间")
private Long nextTime;

View File

@ -42,10 +42,11 @@ public interface ExtScheduleMapper {
List<Schedule> getSchedule(@Param("request") TaskCenterScheduleBatchRequest request, @Param("projectIds") List<String> projectIds);
int countByProjectIds(@Param("ids") List<String> ids);
List<TaskHubScheduleDTO> selectScheduleList(@Param("request") BasePageRequest request, @Param("projectIds") List<String> projectIds);
List<Schedule> getSchedules(@Param("request") TableBatchProcessDTO request, @Param("projectIds") List<String> projectIds);
List<TaskHubScheduleDTO> getLastAndNextTime(@Param("resourceIds") List<String> resourceIds);
}

View File

@ -366,6 +366,13 @@
</include>
</where>
</select>
<select id="getLastAndNextTime" resultType="io.metersphere.system.dto.taskhub.TaskHubScheduleDTO">
SELECT JOB_NAME AS resource_id, NEXT_FIRE_TIME AS nextTime, PREV_FIRE_TIME AS lastTime
FROM QRTZ_TRIGGERS WHERE TRIGGER_NAME IN
<foreach collection="resourceIds" item="resourceId" separator="," open="(" close=")">
#{resourceId}
</foreach>
</select>
<sql id="taskCenterQuery">
<if test="projectIds != null and projectIds.size() > 0">

View File

@ -51,13 +51,16 @@ import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.quartz.CronExpression;
import org.quartz.JobKey;
import org.quartz.TriggerKey;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.text.ParseException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -233,10 +236,35 @@ public class BaseTaskHubService {
.flatMap(item -> Stream.of(item.getCreateUserName()))
.collect(Collectors.toSet());
Map<String, String> userMap = userLoginService.getUserNameMap(new ArrayList<>(userSet));
list.forEach(item -> {
List<String> resourceIds = list.stream()
.filter(item -> StringUtils.isNotBlank(item.getResourceId()))
.map(TaskHubScheduleDTO::getResourceId)
.toList();
Map<String, TaskHubScheduleDTO> trigerTimeMap = Map.of();
if (CollectionUtils.isNotEmpty(resourceIds)) {
trigerTimeMap = extScheduleMapper.getLastAndNextTime(resourceIds)
.stream()
.collect(Collectors.toMap(TaskHubScheduleDTO::getResourceId, Function.identity()));
}
for (TaskHubScheduleDTO item : list) {
item.setCreateUserName(userMap.getOrDefault(item.getCreateUserName(), StringUtils.EMPTY));
item.setOrganizationName(orgMap.getOrDefault(item.getProjectId(), StringUtils.EMPTY));
});
if (trigerTimeMap.get(item.getResourceId()) != null) {
item.setNextTime(trigerTimeMap.get(item.getResourceId()).getNextTime());
item.setLastTime(trigerTimeMap.get(item.getResourceId()).getLastTime());
} else {
try {
CronExpression cron = new CronExpression(item.getValue());
Date date = new Date(System.currentTimeMillis());
item.setNextTime(cron.getNextValidTimeAfter(date).getTime());
} catch (ParseException e) {
LogUtils.error(e);
}
}
}
}
}