|
@@ -15,13 +15,13 @@ import com.css.simulation.resource.scheduler.pojo.po.UserPO;
|
|
import com.css.simulation.resource.scheduler.pojo.to.PrefixTO;
|
|
import com.css.simulation.resource.scheduler.pojo.to.PrefixTO;
|
|
import com.css.simulation.resource.scheduler.service.TaskService;
|
|
import com.css.simulation.resource.scheduler.service.TaskService;
|
|
import com.css.simulation.resource.scheduler.util.ProjectUtil;
|
|
import com.css.simulation.resource.scheduler.util.ProjectUtil;
|
|
-import io.kubernetes.client.openapi.ApiClient;
|
|
|
|
import lombok.SneakyThrows;
|
|
import lombok.SneakyThrows;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
@@ -41,6 +41,8 @@ public class ProjectScheduler {
|
|
String password;
|
|
String password;
|
|
@Value("${scheduler.linux-path.job-yaml}")
|
|
@Value("${scheduler.linux-path.job-yaml}")
|
|
String jobYaml;
|
|
String jobYaml;
|
|
|
|
+ @Value("${scheduler.kubernetes.pod-timeout}")
|
|
|
|
+ Long podTimeOut; // 超时时间,单位毫秒
|
|
// -------------------------------- Comment --------------------------------
|
|
// -------------------------------- Comment --------------------------------
|
|
@Resource
|
|
@Resource
|
|
StringRedisTemplate stringRedisTemplate;
|
|
StringRedisTemplate stringRedisTemplate;
|
|
@@ -55,8 +57,6 @@ public class ProjectScheduler {
|
|
@Resource
|
|
@Resource
|
|
AutoSubProjectMapper autoSubProjectMapper;
|
|
AutoSubProjectMapper autoSubProjectMapper;
|
|
@Resource
|
|
@Resource
|
|
- ApiClient apiClient;
|
|
|
|
- @Resource
|
|
|
|
ProjectConsumer projectConsumer;
|
|
ProjectConsumer projectConsumer;
|
|
@Resource
|
|
@Resource
|
|
ProjectUtil projectUtil;
|
|
ProjectUtil projectUtil;
|
|
@@ -185,28 +185,30 @@ public class ProjectScheduler {
|
|
* 同时也可处理 pod 莫名关闭,因为关闭之后也会超时
|
|
* 同时也可处理 pod 莫名关闭,因为关闭之后也会超时
|
|
*/
|
|
*/
|
|
@Scheduled(fixedDelay = 60 * 1000)
|
|
@Scheduled(fixedDelay = 60 * 1000)
|
|
|
|
+ @Transactional
|
|
public void taskTimeout() {
|
|
public void taskTimeout() {
|
|
- long timeout = 2 * 60 * 1000L;
|
|
|
|
- List<TaskPO> executingTaskList = taskMapper.selectByRunState(DictConstants.TASK_RUNNING);
|
|
|
|
- if (executingTaskList != null && executingTaskList.size() > 0) {
|
|
|
|
- log.info("ProjectScheduler--taskTimeout 正在运行的任务有:" + executingTaskList);
|
|
|
|
- for (TaskPO task : executingTaskList) {
|
|
|
|
- String userId = task.getCreateUserId();
|
|
|
|
- String projectId = task.getPId();
|
|
|
|
- String taskId = task.getId();
|
|
|
|
- PrefixTO redisPrefix = projectUtil.getRedisPrefixByUserIdAndProjectIdAndTaksId(userId, projectId, taskId);
|
|
|
|
- // 获取心跳时间
|
|
|
|
- String tickTime = stringRedisTemplate.opsForValue().get(redisPrefix.getTaskTickKey());
|
|
|
|
- if (StringUtil.isEmpty(tickTime)) {
|
|
|
|
- log.error(redisPrefix.getTaskTickKey() + ",该 key 的心跳时间为空!");
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- long lastTickTime = Long.parseLong(tickTime);
|
|
|
|
- // 如果心跳超时则更改任务状态为 Aborted
|
|
|
|
- if (TimeUtil.getNow() - lastTickTime > timeout) {
|
|
|
|
- String podName = stringRedisTemplate.opsForValue().get(redisPrefix.getTaskPodKey());
|
|
|
|
- taskService.taskState(taskId, DictConstants.TASK_ABORTED, podName);
|
|
|
|
- }
|
|
|
|
|
|
+ //1 查询任务信息(需要过滤已经删除的项目,因为页面上删除项目不会删除任务)
|
|
|
|
+ List<TaskPO> executingTaskList = taskMapper.selectByRunStateAndProjectIsNotDeleted(DictConstants.TASK_RUNNING);
|
|
|
|
+ if (CollectionUtil.isEmpty(executingTaskList)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ log.info("ProjectScheduler--taskTimeout 正在运行的任务有:" + executingTaskList);
|
|
|
|
+ for (TaskPO task : executingTaskList) {
|
|
|
|
+ String userId = task.getCreateUserId();
|
|
|
|
+ String projectId = task.getPId();
|
|
|
|
+ String taskId = task.getId();
|
|
|
|
+ PrefixTO redisPrefix = projectUtil.getRedisPrefixByUserIdAndProjectIdAndTaksId(userId, projectId, taskId);
|
|
|
|
+ // 获取心跳时间
|
|
|
|
+ String tickTime = stringRedisTemplate.opsForValue().get(redisPrefix.getTaskTickKey());
|
|
|
|
+ if (StringUtil.isEmpty(tickTime)) {
|
|
|
|
+ log.error(redisPrefix.getTaskTickKey() + ",该 key 的心跳时间为空!");
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ long lastTickTime = Long.parseLong(tickTime);
|
|
|
|
+ // 如果心跳超时则更改任务状态为 Aborted
|
|
|
|
+ if (TimeUtil.getNow() - lastTickTime > podTimeOut) {
|
|
|
|
+ String podName = stringRedisTemplate.opsForValue().get(redisPrefix.getTaskPodKey());
|
|
|
|
+ taskService.taskState(taskId, DictConstants.TASK_ABORTED, podName);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|