martin преди 3 години
родител
ревизия
150e957c7e

+ 1 - 1
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/consumer/ManualProjectConsumer.java

@@ -124,7 +124,7 @@ public class ManualProjectConsumer {
         if (CollectionUtil.isNotEmpty(accidentIdList)) {
             sceneList.addAll(sceneMapper.selectAccidentByIdList(accidentIdList));
         }
-        projectMapper.updateTaskNumber(projectId, sceneList.size()); // 有多少场景就有多少任务
+        projectMapper.updateTaskNumber(projectId, sceneList.size()); // 有多少场景就有多少任务,更新项目的任务数量,且初始化已完成任务数量为0
         // -------------------------------- 2 模型 --------------------------------
         // 根据车辆配置id vehicleConfigId, 获取 模型信息和传感器信息
         String vehicleConfigId = projectMessageDTO.getVehicleConfigId();// 模型配置 id

+ 10 - 1
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/controller/TaskController.java

@@ -21,7 +21,7 @@ public class TaskController {
     private String hello;
 
     @RequestMapping("/hello")
-    public String hello(){
+    public String hello() {
         return hello;
     }
 
@@ -50,4 +50,13 @@ public class TaskController {
     }
 
 
+    /**
+     * pod 执行结束后需要关闭
+     */
+    @GetMapping("/shutdown")
+    public Boolean taskShutdown(@RequestParam("taskId") String taskId, @RequestParam("podName") String podName) {
+        return taskService.taskShutdown(taskId, podName);
+    }
+
+
 }

+ 6 - 1
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/mapper/ProjectMapper.java

@@ -26,10 +26,15 @@ public interface ProjectMapper {
 
 
     @Update("update simulation_manual_project\n" +
-            "set task_number = #{taskNumber}\n" +
+            "set task_number = #{taskNumber}, task_completed = 0\n" +
             "where id = #{id}")
     void updateTaskNumber(@Param("id") String id, @Param("taskNumber") int taskNumber);
 
+    @Update("update simulation_manual_project\n" +
+            "set task_completed = #{taskCompleted}\n" +
+            "where id = #{id}")
+    void updateTaskCompleted(@Param("id") String id, @Param("taskCompleted") int taskCompleted);
+
 
     @Select("select task_number\n" +
             "from simulation_manual_project\n" +

+ 12 - 0
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/service/TaskService.java

@@ -62,6 +62,8 @@ public class TaskService {
     String pyPath;
     @Value("${scheduler.linux-temp-path}")
     String linuxTempPath;
+    @Autowired
+    ProjectMapper projectMapper;
 
     public void taskTick(String taskId) {
         log.info("------- /tick 接收到任务 " + taskId + "的心跳!");
@@ -81,11 +83,13 @@ public class TaskService {
         log.info("------- /state 任务 " + taskId + " 的父项目为:" + projectId);
         int taskNum = projectMapper.selectTaskNumById(projectId);
         int endTaskNum = projectMapper.selectEndTaskNum(projectId);    // 查询已结束的任务 'Aborted', 'PendingAnalysis', 'Terminated'
+        projectMapper.updateTaskCompleted(projectId, endTaskNum);
         log.info("------- /state 项目 " + projectId + " 完成进度为:" + endTaskNum + "/" + taskNum);
         if (taskNum != endTaskNum) {  // 已结束任务数等于所有任务数量,才会准备打分;否则退出。
             return;
         }
         projectMapper.updateProjectState(projectId, DictConstants.PROJECT_COMPLETED);   // 修改该 project 的状态为已完成
+        LinuxUtil.execute("kubectl delete job project-" + projectId);
         List<TaskPO> taskList = taskMapper.selectTaskListByProjectId(projectId);  // 所有任务信息
         // -------------------------------- 查询叶子指标 --------------------------------
         List<IndexTemplatePO> leafIndexTemplateList = indexTemplateMapper.selectLeafIndexWithRuleDetailsByPackageId(scenePackageId);
@@ -202,6 +206,14 @@ public class TaskService {
         return DictConstants.TASK_PENDING.equals(state);
     }
 
+    @SneakyThrows
+    public Boolean taskShutdown(String taskId, String podName) {
+        // 查询 task 如果不是 pending 则不执行
+        LinuxUtil.execute("kubectl delete pod " + podName);
+        return true;
+    }
+
+
     public double compute(List<IndexTemplatePO> leaf) {
         double result = 0.0;
         Map<String, List<IndexTemplatePO>> groups = leaf.stream().collect(Collectors.groupingBy(IndexTemplatePO::getParentId));

+ 4 - 0
simulation-resource-scheduler/src/main/resources/kubernetes/template/job/job-template.yaml

@@ -20,6 +20,10 @@ spec:
           imagePullPolicy: Never
           command: [ "/Controller/VTDController", "/Controller/config/docker_cloud.ini" ]
           env:
+            - name: PodName
+              valueFrom:
+                fieldRef:
+                  fieldPath: metadata.name
             - name: LM_LICENSE_FILE
               value: 27500@172.14.1.103
           volumeMounts: