martin il y a 3 ans
Parent
commit
170c82264e

+ 13 - 0
api-common/src/main/java/api/common/util/LinuxUtil.java

@@ -25,4 +25,17 @@ public class LinuxUtil {
         return out.toString();
     }
 
+    public static String execute1(String cmd) throws IOException {
+        Runtime run = Runtime.getRuntime();
+        Process process = run.exec(cmd);
+        InputStream in = new BufferedInputStream(process.getInputStream());
+        StringBuilder out = new StringBuilder();
+        byte[] b = new byte[8192];
+        for (int n; (n = in.read(b)) != -1; ) {
+            out.append(new String(b, 0, n));
+        }
+        in.close();
+        process.destroy();
+        return out.toString();
+    }
 }

Fichier diff supprimé car celui-ci est trop grand
+ 294 - 3
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/consumer/ManualProjectConsumer.java


+ 13 - 17
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/scheduler/TickScheduler.java

@@ -1,6 +1,7 @@
 package com.css.simulation.resource.scheduler.scheduler;
 
 import api.common.pojo.constants.DictConstants;
+import api.common.util.CollectionUtil;
 import api.common.util.LinuxUtil;
 import api.common.util.StringUtil;
 import api.common.util.TimeUtil;
@@ -42,31 +43,26 @@ public class TickScheduler {
     public void tick() {
 
         ArrayList<TaskPO> executingTaskList = taskMapper.selectExecuting();
-        if (executingTaskList != null && executingTaskList.size() > 0) {
+        if (CollectionUtil.isEmpty(executingTaskList)) {
             //        log.info("------- TickScheduler 查询出所有执行中的任务('Running'):" + executingTaskList);
             //2 根据 key 查出任务的心跳时间
-
             for (TaskPO task : executingTaskList) {
                 String taskId = task.getId();
                 String projectId = task.getPId();
-                try {
-                    String s = redisTemplate.opsForValue().get(manualProjectTopic + ":" + projectId + ":" + taskId);
+                String s = redisTemplate.opsForValue().get(manualProjectTopic + ":" + projectId + ":" + taskId);
 //                Optional.ofNullable(s).orElseThrow(() -> new RuntimeException("项目 " + projectId + " 下的任务 " + taskId + " 的心跳查询失败"));
-                    assert s != null;
-                    long tickTime = Long.parseLong(s);
-                    long timeout = 2 * 60 * 1000L;
-                    long now = TimeUtil.getNow();
-                    long difference = now - tickTime;
+                assert s != null;
+                long tickTime = Long.parseLong(s);
+                long timeout = 2 * 60 * 1000L;
+                long now = TimeUtil.getNow();
+                long difference = now - tickTime;
 //                log.info("------- TickScheduler 任务" + taskId + "心跳时间为:" + tickTime + "最大仿真时间为:" + tickTime + "时间差为:" + difference);
-                    if (difference > timeout) {
-                        log.info("------- TickScheduler 任务" + taskId + "已超时,状态修改为:" + DictConstants.TASK_ABORTED);
-                        String podName = redisTemplate.opsForValue().get("podName:" + taskId);
-                        if (podName != null) {
-                            taskService.taskState(taskId, DictConstants.TASK_ABORTED, podName);
-                        }
+                if (difference > timeout) {
+                    log.info("------- TickScheduler 任务" + taskId + "已超时,状态修改为:" + DictConstants.TASK_ABORTED);
+                    String podName = redisTemplate.opsForValue().get("podName:" + taskId);
+                    if (podName != null) {
+                        taskService.taskState(taskId, DictConstants.TASK_ABORTED, podName);
                     }
-                } catch (Exception e) {
-                    throw new RuntimeException(e.getMessage());
                 }
             }
         }

+ 11 - 2
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/service/TaskService.java

@@ -107,11 +107,13 @@ public class TaskService {
 
     @SneakyThrows
     public void taskState(String taskId, String state, String podName) {
-        log.info("TaskService--state 修改任务 " + taskId + "的状态:" + state + ",pod 名称为:" + podName);
+
         redisTemplate.opsForValue().set("podName:" + taskId, podName);
         if ("Running".equals(state)) {
+            log.info("TaskService--state 修改任务 " + taskId + "的状态为 Running,pod 名称为:" + podName);
             taskMapper.updateStateWithStartTime(taskId, state, TimeUtil.getNowForMysql());
-        } else if ("Aborted".equals(state) || "Terminated".equals(state)) {
+        } else if ("Aborted".equals(state)) {
+            log.info("TaskService--state 修改任务 " + taskId + "的状态为 Aborted,pod 名称为:" + podName);
             LinuxUtil.execute("kubectl delete pod " + podName);
 //            taskManager.updateFailStateWithStopTime(taskId, state, TimeUtil.getNowForMysql());
             taskMapper.updateFailStateWithStopTime(taskId, state, TimeUtil.getNowForMysql());
@@ -134,6 +136,13 @@ public class TaskService {
             }
             String errorMessageString = errorMessage.get();
             return;
+        } else if ("Terminated".equals(state)) {
+            String command = "kubectl delete pod " + podName;
+            log.info("TaskService--state 修改任务 " + taskId + "的状态为 Terminated,pod 名称为:" + podName
+                    + ",并执行命令:" + command);
+            String resultTerminated = LinuxUtil.execute1(command);
+            taskMapper.updateSuccessStateWithStopTime(taskId, state, TimeUtil.getNowForMysql());
+            redisTemplate.delete("podName:" + taskId);
         } else if ("PendingAnalysis".equals(state)) {
             LinuxUtil.execute("kubectl delete pod " + podName);
             taskMapper.updateSuccessStateWithStopTime(taskId, state, TimeUtil.getNowForMysql());

Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff