Forráskód Böngészése

增加计算进度通知

dsx 2 éve
szülő
commit
4db2c401d6

+ 70 - 55
src/main/java/com/fdkankan/contro/mq/service/impl/BuildSceneProgressServiceImpl.java

@@ -1,11 +1,17 @@
 package com.fdkankan.contro.mq.service.impl;
 
+import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.io.watch.WatchMonitor;
 import cn.hutool.core.io.watch.Watcher;
+import cn.hutool.core.thread.ThreadUtil;
+import cn.hutool.core.util.StrUtil;
 import cn.hutool.http.HttpUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.fdkankan.common.constant.SceneVersionType;
+import com.fdkankan.common.util.FileUtils;
 import com.fdkankan.contro.entity.ScenePlus;
 import com.fdkankan.contro.entity.ScenePlusExt;
 import com.fdkankan.contro.mq.service.IBuildSceneProgressService;
@@ -24,9 +30,8 @@ import java.io.File;
 import java.math.BigDecimal;
 import java.nio.file.Path;
 import java.nio.file.WatchEvent;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Objects;
+import java.util.*;
+import java.util.concurrent.*;
 
 @RefreshScope
 @Slf4j
@@ -57,17 +62,19 @@ public class BuildSceneProgressServiceImpl implements IBuildSceneProgressService
         String website = scenePlusExt.getWebSite();
         String title = scenePlus.getTitle();
 
-        String projectJsonPath = path.concat(File.separator).concat("project.json");
-        File file = FileUtil.file(projectJsonPath);
-        WatchMonitor watchMonitor = WatchMonitor.create(file);
-        watchMonitor.setWatcher(new Watcher(){
+        ExecutorService executor = ThreadUtil.newSingleExecutor();
+        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
+            String projectJsonPath = path.concat(File.separator).concat("project.json");
+            File file = FileUtil.file(projectJsonPath);
+            WatchMonitor watchMonitor = WatchMonitor.create(file);
+            watchMonitor.setWatcher(new Watcher() {
 
-            boolean complete = false;
-            int mainProgress = 0;
-            Long totalTime = null;//buildProgressTime
+                boolean complete = false;
+                int mainProgress = 0;
+                Long totalTime = null;//buildProgressTime
 
-            @Override
-            public void onCreate(WatchEvent<?> event, Path currentPath) {
+                @Override
+                public void onCreate(WatchEvent<?> event, Path currentPath) {
 //                log.info("project.json文件创建完毕");
 //                String projectJsonStr = FileUtil.readUtf8String(file);
 //                JSONObject projectJson = JSON.parseObject(projectJsonStr);
@@ -84,64 +91,72 @@ public class BuildSceneProgressServiceImpl implements IBuildSceneProgressService
 //                params.put("progress", mainProgress);
 //                HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000);
 
-            }
-
-            @Override
-            public void onModify(WatchEvent<?> event, Path currentPath) {
-                log.info("project.json文件发生了变化");
-                String projectJsonStr = FileUtil.readUtf8String(file);
-                JSONObject projectJson = JSON.parseObject(projectJsonStr);
-                JSONObject state = projectJson.getJSONObject("state");
-                complete = state.getBoolean("done");
-                log.info("计算完成状态:{}", complete);
-                if(Objects.isNull(totalTime)){
-                    totalTime = state.getLong("expect_time") + buildProgressTime;
-                    redisUtil.set(String.format(RedisKey.SCENE_BUILD_EXPECT_TOTAL_TIME_NUM, num), String.valueOf(totalTime), RedisKey.CAMERA_EXPIRE_7_TIME);
                 }
-                if(complete){
-                    mainProgress = 90;
-                    Map<String, Object> params = new HashMap<>();
-                    params.put("website", website);
-                    params.put("title", title);
-                    params.put("customUserId",customUserId);
-                    params.put("gps", gps);
-                    params.put("totalTime", totalTime);
-                    params.put("progress", mainProgress);
-                    log.info("算法完成发送请求,url:{}, param:{}",buildProgressUrl, JSON.toJSONString(params));
-                    HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000);
-                    watchMonitor.interrupt();
-                }else{
-                    int progress = new BigDecimal(projectJson.getDouble("progress")).multiply(new BigDecimal(100)).intValue();
-                    if(progress - mainProgress >= 10){
-                        mainProgress = progress;
+
+                @Override
+                public void onModify(WatchEvent<?> event, Path currentPath) {
+                    log.info("project.json文件发生了变化");
+                    String projectJsonStr = FileUtil.readUtf8String(file);
+                    JSONObject projectJson = JSON.parseObject(projectJsonStr);
+                    JSONObject state = projectJson.getJSONObject("state");
+                    complete = state.getBoolean("done");
+                    log.info("计算完成状态:{}", complete);
+                    if (Objects.isNull(totalTime)) {
+                        totalTime = state.getLong("expect_time") + buildProgressTime;
+                        redisUtil.set(String.format(RedisKey.SCENE_BUILD_EXPECT_TOTAL_TIME_NUM, num), String.valueOf(totalTime), RedisKey.CAMERA_EXPIRE_7_TIME);
+                    }
+                    if (complete) {
+                        mainProgress = 90;
                         Map<String, Object> params = new HashMap<>();
                         params.put("website", website);
                         params.put("title", title);
-                        params.put("customUserId",customUserId);
+                        params.put("customUserId", customUserId);
                         params.put("gps", gps);
                         params.put("totalTime", totalTime);
-                        params.put("progress", progress);
-                        log.info("算法进行中发送请求,url:{}, param:{}",buildProgressUrl, JSON.toJSONString(params));
+                        params.put("progress", mainProgress);
+                        log.info("算法完成发送请求,url:{}, param:{}", buildProgressUrl, JSON.toJSONString(params));
                         HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000);
+                        watchMonitor.interrupt();
+                        executor.shutdown();
+                    } else {
+                        int progress = new BigDecimal(projectJson.getDouble("progress")).multiply(new BigDecimal(100)).intValue();
+                        if (progress - mainProgress >= 10) {
+                            mainProgress = progress;
+                            Map<String, Object> params = new HashMap<>();
+                            params.put("website", website);
+                            params.put("title", title);
+                            params.put("customUserId", customUserId);
+                            params.put("gps", gps);
+                            params.put("totalTime", totalTime);
+                            params.put("progress", progress);
+                            log.info("算法进行中发送请求,url:{}, param:{}", buildProgressUrl, JSON.toJSONString(params));
+                            HttpUtil.post(buildProgressUrl, JSON.toJSONString(params), 2000);
+                        }
                     }
                 }
-            }
 
-            @Override
-            public void onDelete(WatchEvent<?> event, Path currentPath) {
-                watchMonitor.interrupt();
-            }
+                @Override
+                public void onDelete(WatchEvent<?> event, Path currentPath) {
+                    watchMonitor.interrupt();
+                    executor.shutdown();
+                }
+
+                @Override
+                public void onOverflow(WatchEvent<?> event, Path currentPath) {
+                }
+            });
+            watchMonitor.start();
+        }, executor);
 
-            @Override
-            public void onOverflow(WatchEvent<?> event, Path currentPath) {
-            }
-        });
-        watchMonitor.start();
 
         try {
-            Thread.sleep(100000000);
+            voidCompletableFuture.get(2*24*60*60, TimeUnit.SECONDS);
         } catch (InterruptedException e) {
             e.printStackTrace();
+        } catch (ExecutionException e) {
+            e.printStackTrace();
+        } catch (TimeoutException e) {
+            e.printStackTrace();
         }
 
     }