dengsixing 1 anno fa
parent
commit
aa8d622873

+ 2 - 1
src/main/java/com/fdkankan/jp/xspace/common/constant/UnityConstant.java

@@ -2,6 +2,7 @@ package com.fdkankan.jp.xspace.common.constant;
 
 public class UnityConstant {
 
-    public final static String EXEC_UNITY_FORMAT = "<Unity可执行文件> -accept-apiupdate -batchmode -disable-gpu-skinning -nographics -silent-crashes -projectPath %s -serial %s -username %s -password %s -executeMethod FDKKSceneTiles.Editor.BuildPipeline.StartBuildPipeline -quit";
+    public final static String EXEC_UNITY_FORMAT = "/opt/unity/Editor/Unity -accept-apiupdate -batchmode -disable-gpu-skinning -nographics -silent-crashes -force-free -projectPath /opt/unity/UnityBuildPipelineProject -executeMethod FDKKSceneTiles.Editor.BuildPipeline.StartBuildPipeline -meshPath @workPath@ -serial @serial@ -username @account@ -password @pwd@ -quit";
+
 
 }

+ 39 - 18
src/main/java/com/fdkankan/jp/xspace/listener/RabbitMqListener.java

@@ -1,6 +1,8 @@
 package com.fdkankan.jp.xspace.listener;
 
 import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.thread.ThreadUtil;
+import com.alibaba.fastjson.JSON;
 import com.fdkankan.common.constant.CommonSuccessStatus;
 import com.fdkankan.dingtalk.DingTalkSendUtils;
 import com.fdkankan.jp.xspace.common.constant.NasPathConstant;
@@ -14,16 +16,19 @@ import com.fdkankan.jp.xspace.service.IUnityService;
 import com.fdkankan.jp.xspace.service.IUserService;
 import com.rabbitmq.client.Channel;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.tomcat.util.threads.ThreadPoolExecutor;
 import org.springframework.amqp.core.Message;
 import org.springframework.amqp.rabbit.annotation.Queue;
 import org.springframework.amqp.rabbit.annotation.RabbitListener;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
 import java.nio.charset.StandardCharsets;
 import java.util.Date;
+import java.util.concurrent.*;
 
 /**
  * <p>
@@ -44,6 +49,10 @@ public class RabbitMqListener {
             "**账号**: %s\n\n" +
             "**工程目录**: %s\n\n";
 
+//    public static final
+
+    public static ExecutorService executorService = Executors.newFixedThreadPool(1);
+
     @Value("${spring.profiles.active}")
     private String profile;
     @Resource
@@ -61,40 +70,52 @@ public class RabbitMqListener {
      * queuesToDeclare = @Queue("${queue.modeling.modeling-test}"),  如果队列不不存在会自动创建队列
      * concurrency = "3"    设置消费线程数,每个线程每次只拉取一条消息消费
      */
-//    @RabbitListener(
-//        queuesToDeclare = @Queue(RabbitmqConstant.QUEUE_PACK_XSPACE),
-//        concurrency = "1"
-//    )
+    @RabbitListener(
+        queuesToDeclare = @Queue(RabbitmqConstant.QUEUE_PACK_XSPACE),
+        concurrency = "1"
+    )
     public void packXspace(Channel channel, Message message) throws Exception {
         String messageId = message.getMessageProperties().getMessageId();
         String msg = new String(message.getBody(), StandardCharsets.UTF_8);
         log.info("开始消费消息,id:{},content:{}", messageId, msg);
-        SceneXspaceMqDTO dto = BeanUtil.toBean(msg, SceneXspaceMqDTO.class);
-        SceneXspace bean = BeanUtil.copyProperties(dto, SceneXspace.class);
+        SceneXspace bean = JSON.parseObject(msg, SceneXspace.class);
         String errorReason = null;
 
+        Future submit = executorService.submit(()-> unityService.packXspace(bean));
+
         try {
-            unityService.packXspace(bean);
+            submit.get(5L, TimeUnit.SECONDS);
             bean.setStatus(CommonSuccessStatus.SUCCESS.code());
-        }catch (PackException e){
+        }catch (TimeoutException e){
             bean.setStatus(CommonSuccessStatus.FAIL.code());
-            errorReason = e.getMessage();
+            errorReason = "打包超时";
+            submit.cancel(true);
         } catch (Exception e) {
-            bean.setStatus(CommonSuccessStatus.FAIL.code());
-            errorReason = "后端报错";
-        }
-
-        if(bean.getStatus() == CommonSuccessStatus.FAIL.code()){
-            User user = userService.getById(bean.getUserId());
-            String workPath = NasPathConstant.UNITY_WORK_PATH + bean.getNum() + "/" + bean.getSerial() + "/";
-            String content = String.format(DINGTALK_MSG_PATTERN, profile, errorReason, bean.getNum(), user.getUserName(), workPath);
-            dingTalkSendUtils.sendActioncardMsgToDingRobot(content, "场景同步失败");
+            Throwable cause = e.getCause();
+            if(cause instanceof  PackException){
+                bean.setStatus(CommonSuccessStatus.FAIL.code());
+                errorReason = cause.getMessage();
+            } else{
+                bean.setStatus(CommonSuccessStatus.FAIL.code());
+                errorReason = "后端报错";
+            }
         }
 
         //更新状态
         bean.setUpdateTime(new Date());
         sceneXspaceService.updateById(bean);
 
+        try {
+            if(bean.getStatus() == CommonSuccessStatus.FAIL.code()){
+                User user = userService.getById(bean.getUserId());
+                String workPath = NasPathConstant.UNITY_WORK_PATH + bean.getNum() + "/" + bean.getSerial() + "/";
+                String content = String.format(DINGTALK_MSG_PATTERN, profile, errorReason, bean.getNum(), user.getUserName(), workPath);
+                dingTalkSendUtils.sendActioncardMsgToDingRobot(content, "场景同步失败");
+            }
+        }catch (Exception e){
+            log.error("发送钉钉失败", e);
+        }
+
         log.info("结束消费消息,id:{}", messageId);
         channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
     }

+ 1 - 1
src/main/java/com/fdkankan/jp/xspace/service/IUnityService.java

@@ -4,7 +4,7 @@ import com.fdkankan.jp.xspace.entity.SceneXspace;
 
 public interface IUnityService {
 
-    void packXspace(SceneXspace bean) throws Exception;
+    void packXspace(SceneXspace bean);
 
 
 

+ 1 - 1
src/main/java/com/fdkankan/jp/xspace/service/impl/SceneXspaceServiceImpl.java

@@ -133,7 +133,7 @@ public class SceneXspaceServiceImpl extends ServiceImpl<ISceneXspaceMapper, Scen
 
         this.saveBatch(sceneXspaceList);
         sceneXspaceList.stream().forEach(v->{
-            mqProducer.sendByWorkQueue(RabbitmqConstant.QUEUE_PACK_XSPACE, BeanUtil.toBean(v, SceneXspaceMqDTO.class));
+            mqProducer.sendByWorkQueue(RabbitmqConstant.QUEUE_PACK_XSPACE, v);
         });
 
         return Result.success();

+ 16 - 5
src/main/java/com/fdkankan/jp/xspace/service/impl/UnityServiceImpl.java

@@ -43,7 +43,13 @@ public class UnityServiceImpl implements IUnityService {
 
 
     @Override
-    public void packXspace(SceneXspace bean) throws Exception {
+    public void packXspace(SceneXspace bean){
+
+        try {
+            Thread.sleep(10000L);
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        }
 
         //准备资源
         String workPath = this.pre(bean);
@@ -71,9 +77,10 @@ public class UnityServiceImpl implements IUnityService {
         return workPath;
     }
 
-    private void callUnity(String workPath) throws Exception {
+    private void callUnity(String workPath){
         UnityConfig unityConfig = unityConfigService.getOne(new LambdaQueryWrapper<>());
-        String cmdStr = String.format(UnityConstant.EXEC_UNITY_FORMAT, workPath, unityConfig.getSerial(), unityConfig.getAccount(), unityConfig.getPassword());
+        String cmdStr = UnityConstant.EXEC_UNITY_FORMAT.replace("@workPath@",workPath).replace("@serial@", unityConfig.getSerial()).replace("@account@", unityConfig.getAccount()).replace("@pwd@", unityConfig.getPassword());
+//        String.format(UnityConstant.EXEC_UNITY_FORMAT, workPath, unityConfig.getSerial(), unityConfig.getAccount(), unityConfig.getPassword());
         try {
             CmdUtils.callLine(cmdStr);
         }catch (Exception e){
@@ -94,7 +101,7 @@ public class UnityServiceImpl implements IUnityService {
         }
     }
 
-    private boolean checkComputeCompleted(String uploadJsonPath, int maxCheckTimes, long waitTime) throws Exception{
+    private boolean checkComputeCompleted(String uploadJsonPath, int maxCheckTimes, long waitTime){
         int checkTimes = 1;
         boolean exist = false;
         do {
@@ -102,7 +109,11 @@ public class UnityServiceImpl implements IUnityService {
                 exist = true;
                 break;
             }
-            Thread.sleep(waitTime);
+            try {
+                Thread.sleep(waitTime);
+            } catch (InterruptedException e) {
+                throw new RuntimeException(e);
+            }
             ++checkTimes;
         }while (checkTimes <= maxCheckTimes);