Ver código fonte

评分失败

root 2 anos atrás
pai
commit
d7e38a8b11

+ 38 - 45
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/manager/TaskManager.java

@@ -32,10 +32,8 @@ import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
 import java.io.File;
-import java.io.IOException;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicReference;
 import java.util.stream.Collectors;
 
 @Component
@@ -229,7 +227,7 @@ public class TaskManager {
         int maxLevel = 1; // 用于计算指标得分
         List<LeafIndexPO> leafIndexList = new ArrayList<>();
         for (int i = 0; i < leafIndexTemplateList.size(); i++) {
-            AtomicReference<String> scoreExplain = new AtomicReference<>(); // 每个叶子指标下的任务的得分说明一样和叶子指标一致
+            String scoreExplain = null; // 每个叶子指标下的任务的得分说明一样和叶子指标一致
             IndexTemplatePO leafIndexTemplate = leafIndexTemplateList.get(i);
             String indexId = leafIndexTemplate.getIndexId(); // 叶子指标id
             String parentId = leafIndexTemplate.getParentId(); // 父 id
@@ -266,44 +264,38 @@ public class TaskManager {
             // taskListOfLeafIndex.forEach(taskOfLeaf -> {});
             for (TaskPO taskOfLeaf : taskListOfLeafIndex) {
                 String task2Id = taskOfLeaf.getId();
-                try {
-                    String runState = taskOfLeaf.getRunState();
-                    log.info("TaskManager--score 任务 " + task2Id + " 的运行状态为:" + runState);
-                    if (DictConstants.TASK_ANALYSIS.equals(runState)) {
-                        taskMapper.updateSuccessStateWithStopTime(task2Id, DictConstants.TASK_ANALYSING, TimeUtil.getNowForMysql());
-                        // 计算每个任务的得分
-                        String result1OfMinio = taskOfLeaf.getRunResultFilePath() + "/Ego.csv";
-                        String result1OfLinux = linuxTempPath + result1OfMinio;
-                        String result2OfMinio = taskOfLeaf.getRunResultFilePath() + "/evaluation.csv";
-                        String result2OfLinux = linuxTempPath + result2OfMinio;
-                        String scoreCommand = "python3 " + scoreDirectoryOfUser + "main.py " + result1OfLinux + " " + result2OfLinux + " " + taskOfLeaf.getSceneType() + " " + ruleName; // 指定打分脚本
-                        String scoreResult;
-                        ScoreTO score;
-                        try {
-                            log.info("TaskService--state 下载 minio 上的结果文件 " + result1OfMinio + " 和 " + result2OfMinio + " 到临时目录:" + linuxTempPath);
-                            MinioUtil.downloadToFile(minioClient, bucketName, result1OfMinio, result1OfLinux);  // 也可改成下载到指定ip的服务器上,需要保证和打分脚本在一台机器上。
-                            MinioUtil.downloadToFile(minioClient, bucketName, result2OfMinio, result2OfLinux);  // 也可改成下载到指定ip的服务器上,需要保证和打分脚本在一台机器上。
-                        } catch (Exception e) {
-                            throw new RuntimeException("------- TaskService--state 下载 minio 上的结果文件出错:" + e.getMessage());
-                        }
-                        try {
-                            log.info("TaskService--state 开始执行打分命令:" + scoreCommand);
-                            scoreResult = SshUtil.execute(session, scoreCommand);
-                            log.info("TaskService--state 项目" + projectId + " 的任务 " + task2Id + " 打分结束,结果为:" + scoreResult);
-                            String replace = StringUtil.replace(scoreResult, "'", "\"");
-                            score = JsonUtil.jsonToBean(replace, ScoreTO.class);
-                            FileUtil.rm(result1OfLinux);
-                            FileUtil.rm(result2OfLinux);
-                            log.info("TaskService--state 已删除运行结果文件 Ego.csv 和 evaluation.csv。");
-                        } catch (IOException e) {
-                            throw new RuntimeException("------- TaskService--state 项目" + projectId + "的任务" + task2Id + " 打分出错,命令为:" + scoreCommand + " 修改状态为:" + DictConstants.TASK_ABORTED + "\n" + e.getMessage());
-                        }
+
+                String runState = taskOfLeaf.getRunState();
+                log.info("TaskManager--score 任务 " + task2Id + " 的运行状态为:" + runState);
+                if (DictConstants.TASK_ANALYSIS.equals(runState)) {
+                    taskMapper.updateSuccessStateWithStopTime(task2Id, DictConstants.TASK_ANALYSING, TimeUtil.getNowForMysql());
+                    // 计算每个任务的得分
+                    String result1OfMinio = taskOfLeaf.getRunResultFilePath() + "/Ego.csv";
+                    String result1OfLinux = linuxTempPath + result1OfMinio;
+                    String result2OfMinio = taskOfLeaf.getRunResultFilePath() + "/evaluation.csv";
+                    String result2OfLinux = linuxTempPath + result2OfMinio;
+                    String scoreCommand = "python3 " + scoreDirectoryOfUser + "main.py " + result1OfLinux + " " + result2OfLinux + " " + taskOfLeaf.getSceneType() + " " + ruleName; // 指定打分脚本
+                    String scoreResult;
+                    ScoreTO score = null;
+                    log.info("TaskService--state 下载 minio 上的结果文件 " + result1OfMinio + " 和 " + result2OfMinio + " 到临时目录:" + linuxTempPath);
+                    MinioUtil.downloadToFile(minioClient, bucketName, result1OfMinio, result1OfLinux);  // 也可改成下载到指定ip的服务器上,需要保证和打分脚本在一台机器上。
+                    MinioUtil.downloadToFile(minioClient, bucketName, result2OfMinio, result2OfLinux);  // 也可改成下载到指定ip的服务器上,需要保证和打分脚本在一台机器上。
+                    log.info("TaskService--state 开始执行打分命令:" + scoreCommand);
+                    scoreResult = SshUtil.execute(session, scoreCommand);
+                    log.info("TaskService--state 项目" + projectId + " 的任务 " + task2Id + " 打分结束,结果为:" + scoreResult);
+                    String replace = StringUtil.replace(scoreResult, "'", "\"");
+                    try {
+                        score = JsonUtil.jsonToBean(replace, ScoreTO.class);
+                    } catch (Exception e) { // 打分失败
+                        log.info("TaskManager--state 项目" + projectId + " 的任务 " + task2Id + " 打分失败:", e);
+                    }
+                    if (score != null) {
                         taskOfLeaf.setReturnSceneId(score.getUnit_scene_ID());
                         taskOfLeaf.setTargetEvaluate(score.getEvaluate_item());
                         taskOfLeaf.setScoreExplain(score.getScore_description());
                         taskOfLeaf.setModifyUserId(userId);
                         taskOfLeaf.setModifyTime(TimeUtil.getNowForMysql());
-                        scoreExplain.set(score.getScore_description());
+                        scoreExplain = score.getScore_description();
                         taskOfLeaf.setRunState(DictConstants.TASK_COMPLETED);
                         double taskScore = score.getUnit_scene_score();
                         if (taskScore == -1.0) {
@@ -314,13 +306,16 @@ public class TaskManager {
                             taskOfLeaf.setScored(true);
                         }
                         taskMapper.updateSuccessState(taskOfLeaf, DictConstants.TASK_COMPLETED);
+                    } else {
+                        //1 修改任务状态为 aborted
+                        taskOfLeaf.setRunState(DictConstants.TASK_ABORTED);
+                        taskOfLeaf.setScore(0.0);
+                        taskOfLeaf.setScored(false);
+                        taskMapper.updateFailStateWithStopTime(task2Id, DictConstants.TASK_ABORTED, TimeUtil.getNowForMysql(), DictConstants.TASK_ERROR_REASON_5);
                     }
-                } catch (Exception e) {
-                    taskOfLeaf.setRunState(DictConstants.TASK_ABORTED);
-                    taskOfLeaf.setScore(0.0);
-                    taskMapper.updateFailStateWithStopTime(task2Id, DictConstants.TASK_ABORTED, TimeUtil.getNowForMysql(), DictConstants.TASK_ERROR_REASON_5);
-                    log.error("TaskManager--score 任务 " + task2Id + " 打分出错!", e.getMessage());
-                    // 如果打分失败则开始下一个打分
+                    // 删除打分使用的文件
+                    FileUtil.rm(result1OfLinux);
+                    FileUtil.rm(result2OfLinux);
                 }
             }
 
@@ -375,7 +370,7 @@ public class TaskManager {
                     .parentId(parentId)
                     .rootId(rootId)
                     .weight(weight)
-                    .scoreExplain(scoreExplain.get())
+                    .scoreExplain(scoreExplain)
                     .packageLevel(packageLevel)
                     .build();
             leafIndex.setCreateUserId(userId);
@@ -391,8 +386,6 @@ public class TaskManager {
         // 保存一级指标分数
         log.info("TaskManager--score 项目 " + projectId + " 的所有任务分数为:" + taskList);
         computeFirst(leafIndexList, allIndexTemplateList, projectId, maxLevel);
-
-
         log.info("TaskManager--score 项目 " + projectId + " 打分完成!");
     }