martin 3 rokov pred
rodič
commit
9bcf402a5a

+ 20 - 0
api-common/src/main/java/api/common/util/IoUtil.java

@@ -0,0 +1,20 @@
+package api.common.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
+
+public class IoUtil {
+    public static void copyBytes(InputStream in, OutputStream out, int bufferSize) throws IOException {
+        byte[] buffer = new byte[bufferSize];
+        for (int bytesRead = in.read(buffer); bytesRead >= 0; bytesRead = in.read(buffer)) {
+            out.write(buffer, 0, bytesRead);
+        }
+    }
+
+    public static InputStream stringToInputStream(String string) {
+        return new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8));
+    }
+}

+ 8 - 5
simulation-oauth-client/src/main/java/com/css/simulation/oauth/client/controller/SignController.java

@@ -58,12 +58,12 @@ public class SignController {
     @RequestMapping("/entry")
     @SneakyThrows
     public String entry(@RequestParam("ticket") String ticket) {
-        log.info("------- 接收到的 ticket 为:" + ticket);
+        log.info("------- /entry 接收到的 ticket 为:" + ticket);
         String encodeTicket = URLEncoder.encode(ticket, "utf-8");
-        log.info("------- ticket 编码之后为:" + encodeTicket);
-        log.info("------- 重定向地址为:" + oauthParameter.getZoogooyRedirectUri());
+        log.info("------- /entry ticket 编码之后为:" + encodeTicket);
+        log.info("------- /entry 重定向地址为:" + oauthParameter.getZoogooyRedirectUri());
         String encodeRedirect = URLEncoder.encode(oauthParameter.getZoogooyRedirectUri(), "utf-8");
-        log.info("------- 重定向地址编码之后为:" + encodeRedirect);
+        log.info("------- /entry 重定向地址编码之后为:" + encodeRedirect);
 
         String zoogooyAuthorizeUrl = oauthParameter.getZoogooyAuthorizeUri() +
                 "?appid=" + oauthParameter.getZoogooyAppid() +
@@ -71,7 +71,7 @@ public class SignController {
                 "&ticket=" + encodeTicket +
                 "&response_type=code" +
                 "&scope=snsapi_userinfo";
-        log.info("------- 需要访问的授权地址为:" + zoogooyAuthorizeUrl);
+        log.info("------- /entry 需要访问的授权地址为:" + zoogooyAuthorizeUrl);
         return "redirect:" + zoogooyAuthorizeUrl;
     }
 
@@ -84,8 +84,11 @@ public class SignController {
     @SneakyThrows
     @ResponseBody
     public ResponseBodyVO<SimulationTokenVO> single(@RequestBody SignSingleParameter signSingleParameter) {
+
         String code = signSingleParameter.getCode();
         String ticket = signSingleParameter.getTicket();
+        log.info("------- /single 接收到的 code 为:" + code);
+        log.info("------- /single 接收到的 ticket 为:" + ticket);
         //1 根据统一凭条 code 获取统一平台 access_token
 
         String zoogooyTokenUrl = oauthParameter.getZoogooyTokenUri() + "?appid=" + oauthParameter.getZoogooyAppid() +

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

@@ -17,6 +17,7 @@ import io.kubernetes.client.openapi.ApiException;
 import io.kubernetes.client.openapi.apis.BatchV1Api;
 import io.kubernetes.client.openapi.models.*;
 import io.kubernetes.client.util.Yaml;
+import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.kafka.clients.consumer.ConsumerRecord;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -68,8 +69,9 @@ public class ManualProjectConsumer {
         System.out.println("------- 消费成功:" + projectRecord.value());
     }
 
-    //    @KafkaListener(groupId = "simulation-resource-scheduler", topics = "${scheduler.manual-project.topic}")
-    public void parseProject(ConsumerRecord<String, String> projectRecord) throws IOException, ApiException {
+    @KafkaListener(groupId = "simulation-resource-scheduler", topics = "${scheduler.manual-project.topic}")
+    @SneakyThrows
+    public void parseProject(ConsumerRecord<String, String> projectRecord) {
 //    public void parseProject(String projectJson) throws IOException, ApiException {
         System.out.println("------- 接收到消息为:" + projectRecord);
 //        System.out.println("------- 接收到消息为:" + projectJson);
@@ -253,11 +255,10 @@ public class ManualProjectConsumer {
         //4-4 创建
         yaml.setSpec(job);
         batchV1Api.createNamespacedJob("simulation-task", yaml, null, null, null);
-
     }
 
 
-//    public void testParseProject(ConsumerRecord<String, String> projectRecord) throws IOException, ApiException {
+    //    public void testParseProject(ConsumerRecord<String, String> projectRecord) throws IOException, ApiException {
     public void testParseProject(String projectJson) throws IOException, ApiException {
 //        System.out.println("------- 接收到消息为:" + projectRecord);
         System.out.println("------- 接收到消息为:" + projectJson);
@@ -445,5 +446,4 @@ public class ManualProjectConsumer {
     }
 
 
-
 }

+ 3 - 2
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/scheduler/TickScheduler.java

@@ -8,6 +8,7 @@ import com.css.simulation.resource.scheduler.mapper.TaskMapper;
 import com.css.simulation.resource.scheduler.pojo.po.TaskPO;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
 import java.util.List;
@@ -23,6 +24,7 @@ public class TickScheduler {
     @Autowired
     private TaskMapper taskMapper;
 
+    @Scheduled(fixedDelay = 2000)
     public void tick() {
         //1 查询出所有执行中的任务(除了 等待中 和 已完成)
         List<TaskPO> executingTaskList = taskMapper.selectExecuting();
@@ -32,10 +34,9 @@ public class TickScheduler {
             String projectId = task.getPId();
             Long maxSimulationTime = task.getMaxSimulationTime();
             long tickTime = Long.parseLong(commonService.get(RedisParameter.builder().key(manualProjectTopic + ":" + projectId + ":" + taskId).build()).getInfo());
-            if (TimeUtil.getNow()-tickTime > maxSimulationTime){
+            if (TimeUtil.getNow() - tickTime > maxSimulationTime) {
                 //3 判断如果心跳时间距离当前时间已经超时,则修改任务状态为失败,并销毁 pod
                 taskMapper.updateState(taskId, DictConstants.TASK_ABORTED);
-
             }
         });
 

+ 9 - 4
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/service/TaskService.java

@@ -18,11 +18,13 @@ import com.css.simulation.resource.scheduler.pojo.po.TaskPO;
 import com.css.simulation.resource.scheduler.pojo.to.ScoreTO;
 import feign.Response;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.IOUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.io.BufferedInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.*;
@@ -90,8 +92,11 @@ public class TaskService {
         // -------------------------------- 查询叶子指标 --------------------------------
         List<IndexTemplatePO> leafIndexTemplateList = indexTemplateMapper.selectLeafIndexWithRuleDetailsByPackageId(scenePackageId);
         List<TaskIndexPO> leafTaskIndexList = new ArrayList<>();
-        for (IndexTemplatePO indexTemplatePO : leafIndexTemplateList) {
-//            String ruleDetails = indexTemplatePO.getRuleDetails();    // 打分脚本路径
+        for (int i = 0; i < leafTaskIndexList.size(); i++) {
+            // -------------------------------- 将叶子节点对应的打分规则保存到临时目录 --------------------------------
+            String ruleDetails = indexTemplatePO.getRuleDetails();    // 打分脚本内容
+            String ruleDetailsPath = linuxTempPath + "rule-script/" + projectId + "_1";
+            FileUtil.writeInputStreamToLocalFile(IoUtil.stringToInputStream(ruleDetails), ruleDetailsPath);
             // -------------------------------- 查询每个叶子指标包括的场景 --------------------------------
             Set<String> sceneIdSet = new HashSet<>();
             String naturalIds = indexTemplatePO.getSceneNaturalIds();
@@ -118,8 +123,8 @@ public class TaskService {
                         ScoreTO score;
                         String runResultMinio = task2.getRunResult();
                         String runResultLinux = linuxTempPath + runResultMinio;
-                        String command = "python3 " + pyPath + " " + runResultLinux + " " + task2.getSceneType();  // 默认使用场景名称找打分脚本
-//                        String command = "python3 " + pyPath + " " + runResultLinux + " " + task2.getSceneType() + " " + ruleDetails; // 指定打分脚本
+//                        String command = "python3 " + pyPath + " " + runResultLinux + " " + task2.getSceneType();  // 默认使用场景名称找打分脚本
+                        String command = "python3 " + pyPath + " " + runResultLinux + " " + task2.getSceneType() + " " + ruleDetailsPath; // 指定打分脚本
                         try {
                             Response download = commonService.download(MinioParameter.builder().objectName(runResultMinio).build());
                             Response.Body body = download.body();