소스 검색

DeployApp 中将节点更新操作放到线程池中异步执行

reghao 2 달 전
부모
커밋
947b1e56ae
1개의 변경된 파일54개의 추가작업 그리고 48개의 파일을 삭제
  1. 54 48
      web/src/main/java/cn/reghao/bnt/web/devops/deployer/service/DeployApp.java

+ 54 - 48
web/src/main/java/cn/reghao/bnt/web/devops/deployer/service/DeployApp.java

@@ -28,6 +28,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ThreadPoolExecutor;
 
 /**
  * @author reghao
@@ -36,7 +37,7 @@ import java.util.concurrent.ExecutorService;
 @Slf4j
 @Service
 public class DeployApp {
-    private final ExecutorService threadPool = ThreadPoolWrapper.threadPool("update-pool", 5);
+    private final ExecutorService threadPool = ThreadPoolWrapper.threadPool("update-pool", 1);
     private final RemoteHostRepository remoteHostRepository;
     private final RemoteAgentConfigRepository remoteAgentConfigRepository;
     private final AppBuildQuery appBuildQuery;
@@ -135,65 +136,70 @@ public class DeployApp {
             return Result.fail(appId + " not built");
         }
 
-        updateApp.getHostList().forEach(host -> {
-            /*UpdateTask updateTask = new UpdateTask(host, appBuilding);
-            threadPool.submit(updateTask);*/
-            RemoteHost remoteHost = remoteHostRepository.findByHost(host);
-            if (remoteHost == null) {
-                log.error("host {} not exists", host);
-                return;
-            }
-
-            String commitId = appBuilding.getCommitId();
-            String remoteDir = remoteHost.getAppDir();
-            try {
-                String nodeType = remoteHost.getNodeType();
-                RemoteAgentConfig remoteAgentConfig = remoteHost.getRemoteAgentConfig();
-                if (remoteAgentConfig == null) {
-                    log.error("RemoteAgentConfig of {} not exists", nodeType);
-                    return;
-                }
-
-                String localDir = String.format("%s/%s/%s_%s/%s", LocalBuildDir.packDir, appId, appId, commitId, nodeType);
-                if (nodeType.equals(NodeType.agent.name())) {
-                    Map<String, String> map = new HashMap<>();
-                    map.put("protocol", remoteAgentConfig.getMgrProtocol());
-                    map.put("host", remoteAgentConfig.getMgrHost());
-                    map.put("port", remoteAgentConfig.getMgrPort() + "");
-
-                    String agentConfigPath = String.format("%s/%s", localDir, "devopsagent.json");
-                    textFile.write(new File(agentConfigPath), JsonConverter.objectToJson(map));
-                } else {
-                    String mgrConfig = remoteAgentConfig.getMgrConfig();
-                    String mgrConfigPath = String.format("%s/%s", localDir, "bntweb.yml");
-                    textFile.write(new File(mgrConfigPath), mgrConfig);
-                }
-
-                Result result = sftp.deploy(localDir, remoteDir, remoteHost);
-                if (result.getCode() == ResultStatus.SUCCESS.getCode()) {
-                    remoteHost.setAppVersion(commitId);
-                    remoteHost.setUpdateTime(LocalDateTime.now());
-                    remoteHostRepository.save(remoteHost);
-                }
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        });
+        ThreadPoolExecutor tpe = (ThreadPoolExecutor) threadPool;
+        int active = tpe.getActiveCount();
+        if (active > 0) {
+            return Result.fail("Another update task is running");
+        }
 
+        UpdateTask updateTask = new UpdateTask(updateApp, appBuilding);
+        threadPool.submit(updateTask);
         return Result.success();
     }
 
     class UpdateTask implements Runnable {
-        private final String host;
+        private final UpdateApp updateApp;
         private final AppBuilding appBuilding;
 
-        public UpdateTask(String host, AppBuilding appBuilding) {
-            this.host = host;
+        public UpdateTask(UpdateApp updateApp, AppBuilding appBuilding) {
+            this.updateApp = updateApp;
             this.appBuilding = appBuilding;
         }
 
         @Override
         public void run() {
+            updateApp.getHostList().forEach(host -> {
+                RemoteHost remoteHost = remoteHostRepository.findByHost(host);
+                if (remoteHost == null) {
+                    log.error("host {} not exists", host);
+                    return;
+                }
+
+                String commitId = appBuilding.getCommitId();
+                String remoteDir = remoteHost.getAppDir();
+                try {
+                    String nodeType = remoteHost.getNodeType();
+                    RemoteAgentConfig remoteAgentConfig = remoteHost.getRemoteAgentConfig();
+                    if (remoteAgentConfig == null) {
+                        log.error("RemoteAgentConfig of {} not exists", nodeType);
+                        return;
+                    }
+
+                    String localDir = String.format("%s/%s/%s_%s/%s", LocalBuildDir.packDir, appId, appId, commitId, nodeType);
+                    if (nodeType.equals(NodeType.agent.name())) {
+                        Map<String, String> map = new HashMap<>();
+                        map.put("protocol", remoteAgentConfig.getMgrProtocol());
+                        map.put("host", remoteAgentConfig.getMgrHost());
+                        map.put("port", remoteAgentConfig.getMgrPort() + "");
+
+                        String agentConfigPath = String.format("%s/%s", localDir, "devopsagent.json");
+                        textFile.write(new File(agentConfigPath), JsonConverter.objectToJson(map));
+                    } else {
+                        String mgrConfig = remoteAgentConfig.getMgrConfig();
+                        String mgrConfigPath = String.format("%s/%s", localDir, "bntweb.yml");
+                        textFile.write(new File(mgrConfigPath), mgrConfig);
+                    }
+
+                    Result result = sftp.deploy(localDir, remoteDir, remoteHost);
+                    if (result.getCode() == ResultStatus.SUCCESS.getCode()) {
+                        remoteHost.setAppVersion(commitId);
+                        remoteHost.setUpdateTime(LocalDateTime.now());
+                        remoteHostRepository.save(remoteHost);
+                    }
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            });
         }
     }
 }