root 2 anos atrás
pai
commit
3d16dbace3

+ 2 - 2
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/configuration/redis/CustomRedisClient.java

@@ -62,11 +62,11 @@ public class CustomRedisClient {
     /**
      * 加锁,有阻塞
      *
-     * @param expire  锁的过期时间,防止程序崩溃后无法释放锁
      * @param timeout 间隔多久获取一次锁
+     * @param expire  锁的过期时间,防止程序崩溃后无法释放锁
      */
     @SneakyThrows
-    public void lock(String name, long expire, long timeout) {
+    public void lock(String name, long timeout, long expire) {
         Boolean success = false;
         while (!success) {
             success = tryLock(name, expire);

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

@@ -30,7 +30,7 @@ public class TaskService {
     @SneakyThrows
     public void taskState(String taskId, String state, String podName) {
         String lockName = "taskId:" + taskId + ":state:" + state + ":pod-name:" + podName;
-        customRedisClient.lock(lockName, 30 * 60L, 7);
+        customRedisClient.lock(lockName, 1L, 30 * 60L);
         try {
             TaskEntity taskEntity = taskMapper.selectById(taskId);
             String projectId = taskEntity.getPId(); // 项目 id

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

@@ -0,0 +1,22 @@
+package com.css.simulation.resource.video.configuration.init;
+
+import api.common.util.LinuxUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
+import org.springframework.stereotype.Component;
+
+@Component
+@Slf4j
+public class CustomApplicationRunner implements ApplicationRunner {
+    @Value("${scheduler.Xvfb-command}")
+    private String XvfbCommand;
+
+
+    @Override
+    public void run(ApplicationArguments args) {
+        log.info("启动 Xvfb。");
+        LinuxUtil.execute(XvfbCommand);
+    }
+}

+ 2 - 2
simulation-resource-video/src/main/java/com/css/simulation/resource/video/configuration/redis/CustomRedisClient.java

@@ -62,11 +62,11 @@ public class CustomRedisClient {
     /**
      * 加锁,有阻塞
      *
-     * @param expire  锁的过期时间,防止程序崩溃后无法释放锁
      * @param timeout 间隔多久获取一次锁
+     * @param expire  锁的过期时间,防止程序崩溃后无法释放锁
      */
     @SneakyThrows
-    public void lock(String name, long expire, long timeout) {
+    public void lock(String name, long timeout, long expire) {
         Boolean success = false;
         while (!success) {
             success = tryLock(name, expire);

+ 5 - 19
simulation-resource-video/src/main/java/com/css/simulation/resource/video/service/VideoService.java

@@ -33,7 +33,6 @@ import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.Iterator;
 import java.util.Set;
-import java.util.concurrent.TimeUnit;
 
 @Service
 @Slf4j
@@ -58,8 +57,6 @@ public class VideoService {
     //* -------------------------------- Comment --------------------------------
     @Value("${scheduler.linux-path.temp}")
     private String linuxTempPath;
-    @Value("${scheduler.Xvfb-command}")
-    private String XvfbCommand;
     @Value("${scheduler.esmini-command}")
     private String esminiCommand;
     @Value("${scheduler.python-command}")
@@ -75,7 +72,6 @@ public class VideoService {
     /**
      * 生成视频
      */
-    @SneakyThrows
     public void generateVideo(String projectId, String projectType, String maxSimulationTime, String taskId) {
         final Set<String> cpuNodeParallelismKeys = customRedisClient.keys("cpu-node*");
         // 获取本地 hostname
@@ -87,13 +83,8 @@ public class VideoService {
                 cpuNodeParallelismKey = tempCpuNodeParallelismKey;
             }
         }
-        // 检查是否有并行度可用
-        while (parallelism < 1) {
-            parallelism = Integer.parseInt(customRedisClient.get(cpuNodeParallelismKey));
-            TimeUnit.SECONDS.sleep(7);
-        }
         String lockName = "video-lock-" + taskId;
-        customRedisClient.lock(lockName, Long.parseLong(maxSimulationTime) + 10L, 1000L);
+        customRedisClient.lock(lockName, 1000L, (Long.parseLong(maxSimulationTime) + 10) * 1000L);
         customRedisClient.decrement(cpuNodeParallelismKey, 1);
         try {
             String rootDirectoryPathOfMinio = projectResultPathOfMinio + projectId + "/" + taskId + "/";
@@ -118,7 +109,6 @@ public class VideoService {
             String pictureDirectoryPath = rootDirectoryPathOfLinux + "picture";
             FileUtil.createDirectory(pictureDirectoryPath);
             String esminiCommandTemp = esminiCommand + " " + xoscPath + " " + pictureDirectoryPath + "/screenshot " + StringUtil.doubleToString(Double.parseDouble(maxSimulationTime), 2);
-            LinuxUtil.execute(XvfbCommand);
             LinuxUtil.execute(esminiCommandTemp);
             log.info("删除 esmini 进程。");
             LinuxUtil.kill(esminiCommandTemp);
@@ -132,14 +122,9 @@ public class VideoService {
             String videoTargetPathOfLinux = rootDirectoryPathOfLinux + "video/" + videoName;
             FileUtil.createParentDirectory(videoTargetPathOfLinux);
             String videoTargetPathOfMinio = rootDirectoryPathOfMinio + videoName;
-
-            String execute = LinuxUtil.execute("ffmpeg"
-                    + " -f image2 -framerate 30 "
-                    + "-start_number " + num
+            String execute = LinuxUtil.execute("ffmpeg -f image2 -framerate 30 -start_number " + num
                     + " -i " + pictureDirectoryPath + "/screenshot_%05d.tga"
-                    + " -c:v libx264 -vf format=yuv420p -crf 20 "
-                    + videoTargetPathOfLinux
-            );
+                    + " -c:v libx264 -vf format=yuv420p -crf 20 " + videoTargetPathOfLinux);
             //6 将视频上传到 minio
             MinioUtil.uploadFromFile(minioClient, videoTargetPathOfLinux, bucketName, videoTargetPathOfMinio);
             log.info("上传成功:" + videoTargetPathOfMinio);
@@ -152,7 +137,8 @@ public class VideoService {
 //        FileUtil.rm(osgbPathOfLinux);
 //        FileUtil.rm(csv1PathOfLinux);
 //        FileUtil.rm(csv2PathOfLinux);
-
+        } catch (Exception e) {
+            throw new RuntimeException("视频生成失败。");
         } finally {
             customRedisClient.decrement(cpuNodeParallelismKey, 1);
             customRedisClient.unlock(lockName);