martin há 3 anos atrás
pai
commit
4719248fbb

+ 3 - 2
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/configuration/kubernetes/KubernetesConfiguration.java

@@ -5,6 +5,7 @@ import io.kubernetes.client.util.ClientBuilder;
 import io.kubernetes.client.util.KubeConfig;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.util.ResourceUtils;
 
 import java.io.File;
 import java.io.FileReader;
@@ -15,9 +16,9 @@ public class KubernetesConfiguration {
 
     @Bean
     public ApiClient apiClient() throws IOException {
-//        File config = ResourceUtils.getFile("classpath:kubernetes/config");  // 开发环境可用,生产环境不行,无法从jar 包读取
+        File config = ResourceUtils.getFile("classpath:kubernetes/config");  // 开发环境可用,生产环境不行,无法从jar 包读取
 //        File config = new File("D:\\idea-project\\simulation-cloud\\simulation-resource-scheduler\\src\\main\\resources\\kubernetes\\config");  //windows
-        File config = new File("/root/.kube/config");   //linux
+//        File config = new File("/root/.kube/config");   //linux
 //
 //        ClassPathResource classPathResource = new ClassPathResource("kubernetes/config");
 //        InputStream inputStream = classPathResource.getInputStream();

Diff do ficheiro suprimidas por serem muito extensas
+ 215 - 4
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/consumer/ManualProjectConsumer.java


+ 7 - 11
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/controller/TaskController.java

@@ -1,16 +1,14 @@
 package com.css.simulation.resource.scheduler.controller;
 
 
+import api.common.pojo.dto.ProjectMessageDTO;
 import api.common.util.IoUtil;
 import com.css.simulation.resource.scheduler.consumer.ManualProjectConsumer;
 import com.css.simulation.resource.scheduler.service.TaskService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.cloud.context.config.annotation.RefreshScope;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletResponse;
 import java.io.FileInputStream;
@@ -43,13 +41,11 @@ public class TaskController {
     }
 
 
-//    /**
-//     * Pod 的心跳接口
-//     */
-//    @PostMapping("/test")
-//    public void test(@RequestBody ProjectMessageDTO projectMessageDTO) {
-//        manualProjectConsumer.parseProject1(projectMessageDTO);
-//    }
+
+    @PostMapping("/test")
+    public void test(@RequestBody ProjectMessageDTO projectMessageDTO) {
+        manualProjectConsumer.parseProject1(projectMessageDTO);
+    }
 
     /**
      * Pod 的心跳接口

+ 90 - 85
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/scheduler/TickScheduler.java

@@ -1,23 +1,13 @@
 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;
 import com.css.simulation.resource.scheduler.mapper.ProjectMapper;
 import com.css.simulation.resource.scheduler.mapper.TaskMapper;
-import com.css.simulation.resource.scheduler.pojo.po.TaskPO;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.core.StringRedisTemplate;
-import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
-import java.io.IOException;
-import java.util.List;
-
 @Component
 @Slf4j
 public class TickScheduler {
@@ -34,79 +24,94 @@ public class TickScheduler {
     @Value("${scheduler.manual-project.job-yaml}")
     String jobYaml;
 
-    @Scheduled(fixedDelay = 2000)
-    public void tick() {
-
-        List<TaskPO> executingTaskList = taskMapper.selectExecuting();
-        if (CollectionUtil.isEmpty(executingTaskList)) {
-            return;
-        }
-//        log.info("------- TickScheduler 查询出所有执行中的任务('Running'):" + executingTaskList);
-        //2 根据 key 查出任务的心跳时间
-        executingTaskList.forEach(task -> {
-            String taskId = task.getId();
-            String projectId = task.getPId();
-            try {
-                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 maxSimulationTime = task.getMaxSimulationTime() * 1000;
-                long now = TimeUtil.getNow();
-                long difference = now - tickTime;
-//                log.info("------- TickScheduler 任务" + taskId + "心跳时间为:" + tickTime + "最大仿真时间为:" + tickTime + "时间差为:" + difference);
-                if (difference > maxSimulationTime) {
-//                    log.info("------- TickScheduler 任务" + taskId + "已超时,状态修改为:" + DictConstants.TASK_ABORTED);
-                    taskMapper.updateState(taskId, DictConstants.TASK_ABORTED);
-                }
-            } catch (Exception e) {
-                throw new RuntimeException(e.getMessage());
-            }
-
-        });
-
-    }
-
-
-    /**
-     * 检查如果有 job 在运行但是 pod 全部关闭的情况,此时需要重启一下 job
-     */
-    @Scheduled(fixedDelay = 60 * 1000)
-    public void checkProject() {
-        //1 查询出正在运行中的 project
-        List<String> projectIdList = projectMapper.selectIdByState("20");
-        log.info("TickScheduler-------checkProject 查询出正在运行中的 project" + projectIdList);
-        //2 根据 projectId 获取 pod
-        projectIdList.forEach(projectId -> {
-
-            String key = manualProjectTopic + ":" + projectId + ":check";
-            String nowString = TimeUtil.getNowString();
-            
-            try {
-                String lastNowString = redisTemplate.opsForValue().get(manualProjectTopic + ":" + projectId + ":check");
-                String execute = LinuxUtil.execute("kubectl get pod | grep project-" + projectId);
-                int i = StringUtil.countSubString(execute, projectId);
-                if (StringUtil.isEmpty(lastNowString) && i == 0) {
-                    redisTemplate.opsForValue().set(key, nowString);
-                }
-
-                if (StringUtil.isNotEmpty(lastNowString) && i == 0) {
-                    // 判断两次是否超过2分钟
-                    long lastNow = Long.parseLong(lastNowString);
-                    long now = Long.parseLong(nowString);
-                    if (now - lastNow > 2L * 60 * 1000) {
-                        LinuxUtil.execute("kubectl delete job project-" + projectId);
-                        log.info("TickScheduler-------checkProject 重新执行项目" + projectId);
-                        String jobTemplateYamlPathTarget = jobYaml + "project-" + projectId + ".yaml";
-                        LinuxUtil.execute("kubectl apply -f " + jobTemplateYamlPathTarget);
-                    }
-                }
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
-        });
-
-        //3 如果 pod 为空,则重启 job
-
-    }
+//    @Scheduled(fixedDelay = 2000)
+//    public void tick() {
+//
+//        List<TaskPO> executingTaskList = taskMapper.selectExecuting();
+//        if (CollectionUtil.isEmpty(executingTaskList)) {
+//            return;
+//        }
+////        log.info("------- TickScheduler 查询出所有执行中的任务('Running'):" + executingTaskList);
+//        //2 根据 key 查出任务的心跳时间
+//        executingTaskList.forEach(task -> {
+//            String taskId = task.getId();
+//            String projectId = task.getPId();
+//            try {
+//                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 maxSimulationTime = task.getMaxSimulationTime() * 1000;
+//                long now = TimeUtil.getNow();
+//                long difference = now - tickTime;
+////                log.info("------- TickScheduler 任务" + taskId + "心跳时间为:" + tickTime + "最大仿真时间为:" + tickTime + "时间差为:" + difference);
+//                if (difference > maxSimulationTime) {
+////                    log.info("------- TickScheduler 任务" + taskId + "已超时,状态修改为:" + DictConstants.TASK_ABORTED);
+//                    taskMapper.updateState(taskId, DictConstants.TASK_ABORTED);
+//                }
+//            } catch (Exception e) {
+//                throw new RuntimeException(e.getMessage());
+//            }
+//
+//        });
+//
+//    }
+//
+//
+//    /**
+//     * 检查如果有 job 在运行但是 pod 全部关闭的情况,此时需要重启一下 job
+//     */
+//    @Scheduled(fixedDelay = 60 * 1000)
+//    public void checkProject() throws IOException {
+//        SshClient client = SshUtil.getClient();
+//        ClientSession session = SshUtil.getSession(client, "182.92.203.182", "root", "CICV2022test");
+//
+//        //1 查询出正在运行中的 project
+//        List<String> projectIdList = projectMapper.selectIdByState("20");
+//        log.info("TickScheduler-------checkProject 查询出正在运行中的 project" + projectIdList);
+//        //2 根据 projectId 获取 pod
+//        projectIdList.forEach(projectId -> {
+//
+//            String key = manualProjectTopic + ":" + projectId + ":check";
+//            String nowString = TimeUtil.getNowString();
+//
+//            try {
+//                String lastNowString = redisTemplate.opsForValue().get(manualProjectTopic + ":" + projectId + ":check");
+//                String podList = SshUtil.execute(session, "kubectl get pod | grep project-" + projectId);
+//                int taskNumber = StringUtil.countSubString(podList, "project");
+//                if (StringUtil.isEmpty(lastNowString) && taskNumber == 0) {
+//                    redisTemplate.opsForValue().set(key, nowString);
+//                }
+//
+//                if (StringUtil.isNotEmpty(lastNowString) && taskNumber == 0) {
+//                    // 判断两次是否超过2分钟
+//                    //3 如果 pod 为空,则重启 job
+//                    long lastNow = Long.parseLong(lastNowString);
+//                    long now = Long.parseLong(nowString);
+//                    if (now - lastNow > 2L * 60 * 1000) {
+//                        LinuxUtil.execute("kubectl delete job project-" + projectId);
+//                        while (true) {
+//                            log.info("TickScheduler-------checkProject 准备重启项目 " + projectId);
+//                            Thread.sleep(10000);
+//                            String podList2 = SshUtil.execute(session, "kubectl get pod | grep project-" + projectId);
+//                            log.info("TickScheduler-------checkProject 项目 " + projectId + " 剩余的 pod 信息为:" + podList2);
+//                            int taskNumber2 = StringUtil.countSubString(podList2, "project");
+//                            if (taskNumber2 == 0) {
+//                                break;
+//                            }
+//                        }
+//                        log.info("TickScheduler-------checkProject 重新执行项目" + projectId);
+//                        String jobTemplateYamlPathTarget = jobYaml + "project-" + projectId + ".yaml";
+//                        LinuxUtil.execute("kubectl apply -f " + jobTemplateYamlPathTarget);
+//                    }
+//                }
+//            } catch (IOException | InterruptedException e) {
+//                e.printStackTrace();
+//            }
+//        });
+//
+//        session.close();
+//        client.stop();
+//
+//    }
 }

+ 2 - 2
simulation-resource-scheduler/src/main/resources/bootstrap-dev.yaml

@@ -2,9 +2,9 @@ spring:
   cloud:
     nacos:
       discovery:
-        server-addr: 10.15.12.70:8848
+        server-addr: 47.94.105.148:8848
         namespace: 3698bfc2-a612-487a-b2a2-aaad16cd9d9d
       config:
-        server-addr: 10.15.12.70:8848
+        server-addr: 47.94.105.148:8848
         namespace: 3698bfc2-a612-487a-b2a2-aaad16cd9d9d
         file-extension: yaml

+ 1 - 1
simulation-resource-scheduler/src/main/resources/bootstrap.yaml

@@ -6,4 +6,4 @@ spring:
   application:
     name: simulation-resource-scheduler
   profiles:
-    active: aliyun
+    active: dev

Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff