Эх сурвалжийг харах

将项目运行节点拆分成GPU节点和CPU节点

LingxinMeng 2 жил өмнө
parent
commit
0556b7db68

+ 6 - 6
api-common/src/main/java/api/common/util/OsUtil.java

@@ -10,6 +10,11 @@ import java.net.InetAddress;
  */
 @Slf4j
 public class OsUtil {
+    @SneakyThrows
+    public static void exec(String command) {
+        log.info("执行命令:{}", command);
+        Runtime.getRuntime().exec(command);
+    }
 
     @SneakyThrows
     public static String getHostName() {
@@ -24,12 +29,7 @@ public class OsUtil {
     }
 
 
-    @SneakyThrows
-    public static void exec(String command) {
-        Runtime.getRuntime().exec(command);
-    }
-
-    public static void kill( String command) {
+    public static void kill(String command) {
         exec("echo kill $(ps -ef | grep '" + command + "' | awk '{ print $2 }')");
     }
 

+ 23 - 23
simulation-resource-video/src/main/java/com/css/simulation/resource/video/configuration/init/CustomApplicationRunner.java

@@ -1,23 +1,23 @@
-package com.css.simulation.resource.video.configuration.init;
-
-import com.css.simulation.resource.video.configuration.redis.CustomRedisClient;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.boot.ApplicationArguments;
-import org.springframework.boot.ApplicationRunner;
-import org.springframework.stereotype.Component;
-
-import javax.annotation.Resource;
-
-@Component
-@Slf4j
-public class CustomApplicationRunner implements ApplicationRunner {
-    @Resource
-    private CustomRedisClient customRedisClient;
-
-
-    @Override
-    public void run(ApplicationArguments args) {
-        log.info("初始化屏幕号。");
-        customRedisClient.set("screen-number", "0");
-    }
-}
+//package com.css.simulation.resource.video.configuration.init;
+//
+//import com.css.simulation.resource.video.configuration.redis.CustomRedisClient;
+//import lombok.extern.slf4j.Slf4j;
+//import org.springframework.boot.ApplicationArguments;
+//import org.springframework.boot.ApplicationRunner;
+//import org.springframework.stereotype.Component;
+//
+//import javax.annotation.Resource;
+//
+//@Component
+//@Slf4j
+//public class CustomApplicationRunner implements ApplicationRunner {
+//    @Resource
+//    private CustomRedisClient customRedisClient;
+//
+//
+//    @Override
+//    public void run(ApplicationArguments args) {
+//        log.info("初始化屏幕号。");
+//        customRedisClient.set("screen-number", "0");
+//    }
+//}

+ 12 - 22
simulation-resource-video/src/main/java/com/css/simulation/resource/video/service/VideoService.java

@@ -68,18 +68,8 @@ public class VideoService {
      * 生成视频
      */
     public void generateVideo(String generateVideoKey, String nodeName, String projectId, String projectType, String maxSimulationTime, String taskId) {
-//        final Set<String> cpuNodeParallelismKeys = customRedisClient.keys("cpu-node*");
-        // 获取本地 hostname
-//        final String hostName = OsUtil.getHostName();
-//        String cpuNodeParallelismKey = "cpu-node:" + nodeName + ":parallelism";
-//        for (String tempCpuNodeParallelismKey : cpuNodeParallelismKeys) {
-//            if (tempCpuNodeParallelismKey.contains(hostName)) {
-//                cpuNodeParallelismKey = tempCpuNodeParallelismKey;
-//            }
-//        }
         String lockName = "video-lock-" + taskId;
         customRedisClient.lock(lockName, 1000L, (Long.parseLong(maxSimulationTime) + 10) * 1000L);
-//        customRedisClient.decrement(cpuNodeParallelismKey, 1);
         try {
             String rootDirectoryPathOfMinio = minioConfiguration.getProjectResultDirectory() + projectId + "/" + taskId + "/";
             String xodrPathOfMinio = rootDirectoryPathOfMinio + taskId + ".xodr";
@@ -102,14 +92,15 @@ public class VideoService {
             String pictureDirectoryPath = rootDirectoryPathOfLinux + "picture";
             FileUtil.createDirectory(pictureDirectoryPath);
             // 获取屏幕号创建新的虚拟屏幕
-            final long newScreenNum = customRedisClient.increment("screen-number", 1);
-            log.info("获取新的屏幕号:" + newScreenNum);
-            final String xvfbCommand2 = constantConfiguration.getXvfbCommand().replaceAll("screen-num", String.valueOf(newScreenNum));
-            final String esminiCommand2 = constantConfiguration.getEsminiCommand().replaceAll("screen-num", String.valueOf(newScreenNum)) + " " + xoscPath + " " + pictureDirectoryPath + "/screenshot " + StringUtil.doubleToString(Double.parseDouble(maxSimulationTime), 2);
-            OsUtil.exec(xvfbCommand2);
-            OsUtil.exec(esminiCommand2);
+//            final long newScreenNum = customRedisClient.increment("screen-number", 1);
+//            log.info("获取新的屏幕号:" + newScreenNum);
+//            final String xvfbCommand = constantConfiguration.getXvfbCommand().replaceAll("screen-num", String.valueOf(newScreenNum));
+//            final String esminiCommand = constantConfiguration.getEsminiCommand().replaceAll("screen-num", String.valueOf(newScreenNum)) + " " + xoscPath + " " + pictureDirectoryPath + "/screenshot " + StringUtil.doubleToString(Double.parseDouble(maxSimulationTime), 2);
+            final String esminiCommand = constantConfiguration.getEsminiCommand() + " " + xoscPath + " " + pictureDirectoryPath + "/screenshot " + StringUtil.doubleToString(Double.parseDouble(maxSimulationTime), 2);
+//            OsUtil.exec(xvfbCommand);
+            OsUtil.exec(esminiCommand);
             log.info("删除 esmini 进程。");
-            OsUtil.kill(esminiCommand2);
+            OsUtil.kill(esminiCommand);
             StringBuilder removeCommand = new StringBuilder();
             removeCommand.append("rm -f ");
             int num = 14;
@@ -123,18 +114,17 @@ public class VideoService {
             String videoTargetPathOfMinio = rootDirectoryPathOfMinio + videoName;
             OsUtil.exec("ffmpeg -f image2 -framerate 30 -start_number " + num + " -i " + pictureDirectoryPath + "/screenshot_%05d.tga" + " -c:v libx264 -vf format=yuv420p -crf 20 " + videoTargetPathOfLinux);
             //删除生成的临时文件
-            String removeAll = "rm -rf " + pictureDirectoryPath;
-            log.info("删除全部图片==" + removeAll);
-            OsUtil.exec(removeAll);
+//            String removeAll = "rm -rf " + pictureDirectoryPath;
+//            log.info("删除全部图片==" + removeAll);
+//            OsUtil.exec(removeAll);
             //6 将视频上传到 minio
             MinioUtil.uploadFromFile(minioClient, videoTargetPathOfLinux, minioConfiguration.getBucketName(), videoTargetPathOfMinio);
             log.info("上传成功:" + videoTargetPathOfMinio);
-            OsUtil.kill(xvfbCommand2);
+//            OsUtil.kill(xvfbCommand);
             //* -------------------------------- 删除临时文件 --------------------------------
         } catch (Exception e) {
             throw new RuntimeException("视频生成失败。", e);
         } finally {
-//            customRedisClient.increment(cpuNodeParallelismKey, 1);
             customRedisClient.unlock(lockName);
             customRedisClient.set(generateVideoKey, "1");
         }