martin 3 năm trước cách đây
mục cha
commit
723418a850

+ 1 - 1
api-common/src/main/java/api/common/util/StringUtil.java

@@ -30,7 +30,7 @@ public class StringUtil {
      */
     public static int countSubString(String source, String target) {
         if (isEmpty(source)|| isEmpty(target)) {
-            throw new RuntimeException("StringUtil-------countSubString 传入字符串不能为空!");
+            return 0;
         }
         int count = 0;
         int index = 0;

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

@@ -72,8 +72,10 @@ public class ManualProjectConsumer {
     String manualProjectTopic;
     @Value("${scheduler.manual-project.result-path-minio}")
     String resultPathMinio;
-    @Value("${scheduler.manual-project.job-template-yaml}")
-    String jobTemplateYaml;
+    @Value("${scheduler.manual-project.job-template}")
+    String jobTemplate;
+    @Value("${scheduler.manual-project.job-yaml}")
+    String jobYaml;
     @Value("${scheduler.linux-temp-path}")
     String linuxTempPath;
 
@@ -412,8 +414,8 @@ public class ManualProjectConsumer {
         int parallelism = projectMessageDTO.getParallelism();    // 并行度
         log.info("------- ManualProjectConsumer 项目 " + projectId + " 的完成度为:" + completions);
         log.info("------- ManualProjectConsumer 项目 " + projectId + " 的并行度为:" + parallelism);
-        String jobTemplateYamlPathSource = "/opt/simulation-cloud/simulation-resource-scheduler/job-template/job-template.yaml";
-        String jobTemplateYamlPathTarget = "/opt/simulation-cloud/simulation-resource-scheduler/job-yaml/" + "project-" + projectId + ".yaml";
+        String jobTemplateYamlPathSource = jobTemplate + "job-template.yaml";
+        String jobTemplateYamlPathTarget = jobYaml + "project-" + projectId + ".yaml";
         String yamlSource = FileUtil.read(jobTemplateYamlPathSource);
         log.info("------- ManualProjectConsumer 模板文件为:" + yamlSource);
         String replace0 = yamlSource.replace("job-cloud-simulation", "project-" + projectId);

+ 4 - 3
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/mapper/ProjectMapper.java

@@ -57,8 +57,9 @@ public interface ProjectMapper {
             "  and p_id = #{projectId}")
     int selectEndTaskNum(@Param("projectId") String projectId);
 
-    @Select("select id\n" +
-            "from simulation_manual_project \n" +
-            "where now_run_state = #{state}")
+    @Select("select *\n" +
+            "from simulation_manual_project\n" +
+            "where is_deleted = '0'\n" +
+            "  and now_run_state = #{state}")
     List<String> selectIdByState(@Param("state") String state);
 }

+ 22 - 5
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/scheduler/TickScheduler.java

@@ -31,6 +31,8 @@ public class TickScheduler {
     TaskMapper taskMapper;
     @Autowired
     ProjectMapper projectMapper;
+    @Value("${scheduler.manual-project.job-yaml}")
+    String jobYaml;
 
     @Scheduled(fixedDelay = 2000)
     public void tick() {
@@ -73,16 +75,31 @@ public class TickScheduler {
     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 (i == 0) {
-                    LinuxUtil.execute("kubectl delete job project-" + projectId);
-                    log.info("TickScheduler-------checkProject 重新执行项目" + projectId);
-                    String jobTemplateYamlPathTarget = "/opt/simulation-cloud/simulation-resource-scheduler/job-yaml/" + "project-" + projectId + ".yaml";
-                    LinuxUtil.execute("kubectl apply -f " + jobTemplateYamlPathTarget);
+                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();