Explorar o código

重构构建部署接口...

reghao %!s(int64=5) %!d(string=hai) anos
pai
achega
8f40cc97d2

+ 8 - 1
common/src/main/java/cn/reghao/autodop/common/utils/data/db/CrudOps.java

@@ -13,7 +13,14 @@ public interface CrudOps<T> {
     default void batchaddOrUpdate(List<T> list) {
     }
 
-    PageList<T> getByPage(int page, int size);
+    default PageList<T> getByPage(int page, int size) {
+        return null;
+    }
+
+    default PageList<T> getByPage(int page, int size, String field) {
+        return null;
+    }
+
     default void get(String uniqueKey) {
     }
 

+ 17 - 18
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/entity/log/BuildDeployApp.java

@@ -4,6 +4,7 @@ import cn.reghao.autodop.common.orm.BaseEntity;
 import cn.reghao.autodop.dmaster.app.entity.config.orchestration.AppOrchestration;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
+import lombok.NoArgsConstructor;
 
 import javax.persistence.*;
 import java.time.LocalDateTime;
@@ -16,16 +17,14 @@ import java.util.stream.Collectors;
  * @author reghao
  * @date 2020-05-20 22:09:53
  */
+@NoArgsConstructor
 @Data
 @EqualsAndHashCode(callSuper = false)
 @Entity
 public class BuildDeployApp extends BaseEntity {
-    @Column(nullable = false, unique = true)
-    private String appId;
-    @Column(nullable = false)
-    private String env;
-    @Column(nullable = false)
-    private boolean enable;
+    @OneToOne(cascade = CascadeType.DETACH)
+    @JoinColumn(name = "app_orchestration_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
+    private AppOrchestration app;
     private String commitId;
     private String bdType;
     private LocalDateTime bdTime;
@@ -33,27 +32,29 @@ public class BuildDeployApp extends BaseEntity {
     private int statusCode;
     private String result;
 
-    public static BuildDeployApp of(AppOrchestration app) {
+    public BuildDeployApp(AppOrchestration app) {
+        this.app = app;
+    }
+
+    /*public static BuildDeployApp of(AppOrchestration app) {
         BuildDeployApp buildDeployApp = new BuildDeployApp();
         buildDeployApp.setAppId(app.getAppId());
         buildDeployApp.setEnv(app.getEnv());
         buildDeployApp.setEnable(app.isEnable());
         return buildDeployApp;
-    }
+    }*/
 
-    public static BuildDeployApp of(BuildLog buildLog) {
+    public static BuildDeployApp of(AppOrchestration app, BuildLog buildLog) {
         BuildDeployApp buildDeployApp = new BuildDeployApp();
-        buildDeployApp.setAppId(buildLog.getAppId());
-        buildDeployApp.setEnv(buildLog.getEnv());
-        buildDeployApp.setEnable(true);
+        buildDeployApp.setApp(app);
         buildDeployApp.setCommitId(buildLog.getCommitId());
         buildDeployApp.setBdType("构建");
         buildDeployApp.setBdTime(buildLog.getBuildTime());
         buildDeployApp.setStatusCode(buildLog.getStatusCode());
         if (buildLog.getStatusCode() != 0) {
             String errDetail = buildLog.getErrDetail();
-            if (errDetail.length() > 100) {
-                buildDeployApp.setResult(errDetail.substring(0, 100));
+            if (errDetail.length() > 500) {
+                buildDeployApp.setResult(errDetail.substring(0, 500));
             } else {
                 buildDeployApp.setResult(errDetail);
             }
@@ -64,11 +65,9 @@ public class BuildDeployApp extends BaseEntity {
         return buildDeployApp;
     }
 
-    public static BuildDeployApp of(DeployResult deployResult) {
+    public static BuildDeployApp of(AppOrchestration app, DeployResult deployResult) {
         BuildDeployApp buildDeployApp = new BuildDeployApp();
-        buildDeployApp.setAppId(deployResult.getAppId());
-        buildDeployApp.setEnv(deployResult.getEnv());
-        buildDeployApp.setEnable(true);
+        buildDeployApp.setApp(app);
         buildDeployApp.setCommitId(deployResult.getCommitId());
         buildDeployApp.setBdType("部署");
         buildDeployApp.setBdTime(deployResult.getDeployTime());

+ 2 - 5
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/repository/log/BuildDeployAppRepository.java

@@ -1,8 +1,7 @@
 package cn.reghao.autodop.dmaster.app.repository.log;
 
+import cn.reghao.autodop.dmaster.app.entity.config.orchestration.AppOrchestration;
 import cn.reghao.autodop.dmaster.app.entity.log.BuildDeployApp;
-import org.springframework.data.domain.Page;
-import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.repository.JpaRepository;
 
 /**
@@ -10,7 +9,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
  * @date 2020-01-21 14:53:03
  */
 public interface BuildDeployAppRepository extends JpaRepository<BuildDeployApp, Long> {
-    BuildDeployApp findByAppId(String appId);
-    BuildDeployApp findByIsDeleteFalseAndAppId(String appId);
-    Page<BuildDeployApp> findByIsDeleteFalseAndEnableIsTrueAndEnv(String env, Pageable pageable);
+    BuildDeployApp findByApp(AppOrchestration app);
 }

+ 9 - 3
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/BuildDeployDispatcher.java

@@ -79,9 +79,13 @@ public class BuildDeployDispatcher {
         if (buildLog.getStatusCode() == 0 && isDeploy) {
             DeployResult deployResult = deployApp(buildLog);
             assert deployResult != null;
-            return BuildDeployApp.of(deployResult);
+            BuildDeployApp buildDeployApp = BuildDeployApp.of(appIntegrate.app(), deployResult);
+            buildDeployLogConsumer.addBuildDeployApp(buildDeployApp);
+            return buildDeployApp;
         } else {
-            return BuildDeployApp.of(buildLog);
+            BuildDeployApp buildDeployApp = BuildDeployApp.of(appIntegrate.app(), buildLog);
+            buildDeployLogConsumer.addBuildDeployApp(buildDeployApp);
+            return buildDeployApp;
         }
     }
 
@@ -169,7 +173,9 @@ public class BuildDeployDispatcher {
         BuildLog buildLog = cache.findByAppIdAndCommitId(appId, commitId);
         DeployResult deployResult = deployApp(buildLog);
         assert deployResult != null;
-        return BuildDeployApp.of(deployResult);
+        BuildDeployApp buildDeployApp = BuildDeployApp.of(cache.findByAppId(appId), deployResult);
+        buildDeployLogConsumer.addBuildDeployApp(buildDeployApp);
+        return buildDeployApp;
     }
 
     public void deployNotify(NotifierConfig notifierConfig, DeployLog deployLog) {

+ 1 - 1
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/BuildService.java

@@ -48,7 +48,7 @@ public class BuildService {
     }
 
     public PageList<BuildDeployAppVO> buildList(int page, int size, String env) {
-        PageList<BuildDeployApp> pageList = buildDeployAppCrudService.getByPage(page, size);
+        PageList<BuildDeployApp> pageList = buildDeployAppCrudService.getByPage(page, size, env);
         PageList<BuildDeployAppVO> vos = new PageList<>();
         vos.setTotalPages(pageList.getTotalPages());
         vos.setTotalSize(pageList.getTotalSize());

+ 5 - 5
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/StatusManager.java

@@ -54,17 +54,17 @@ public class StatusManager {
     }
 
     public List<AppStatus> status(String env, PageRequest pageRequest) {
-        Page<BuildDeployApp> page = buildDeployAppRepository.findByIsDeleteFalseAndEnableIsTrueAndEnv(env, pageRequest);
+        //Page<BuildDeployApp> page = buildDeployAppRepository.findByIsDeleteFalseAndEnableIsTrueAndEnv(env, pageRequest);
         List<AppStatus> appStatusList = new ArrayList<>();
         // TODO 异步处理
-        try {
+        /*try {
             for (BuildDeployApp buildDeployApp : page.getContent()) {
-                /*for (String host : app.getApp().getAppDeploy().getHosts()) {
-                }*/
+                for (String host : app.getApp().getAppDeploy().getHosts()) {
+                }
             }
         } catch (Exception e) {
             e.printStackTrace();
-        }
+        }*/
 
         return appStatusList;
     }

+ 42 - 18
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/crud/BuildDeployAppCrudService.java

@@ -2,14 +2,20 @@ package cn.reghao.autodop.dmaster.app.service.crud;
 
 import cn.reghao.autodop.common.utils.data.db.CrudOps;
 import cn.reghao.autodop.common.utils.data.db.PageList;
+import cn.reghao.autodop.dmaster.app.entity.config.orchestration.AppOrchestration;
 import cn.reghao.autodop.dmaster.app.entity.log.BuildDeployApp;
 import cn.reghao.autodop.dmaster.app.repository.log.BuildDeployAppRepository;
+import cn.reghao.autodop.dmaster.app.repository.orchestration.AppOrchestrationRepository;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.Sort;
 import org.springframework.stereotype.Service;
 
 import java.time.LocalDateTime;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 /**
  * @author reghao
@@ -17,15 +23,18 @@ import java.time.LocalDateTime;
  */
 @Service
 public class BuildDeployAppCrudService implements CrudOps<BuildDeployApp> {
+    private AppOrchestrationRepository appRepository;
     private BuildDeployAppRepository buildDeployAppRepository;
 
-    public BuildDeployAppCrudService(BuildDeployAppRepository buildDeployAppRepository) {
+    public BuildDeployAppCrudService(AppOrchestrationRepository appRepository,
+                                     BuildDeployAppRepository buildDeployAppRepository) {
+        this.appRepository = appRepository;
         this.buildDeployAppRepository = buildDeployAppRepository;
     }
 
     @Override
     public void addOrUpdate(BuildDeployApp buildDeployApp) throws Exception {
-        BuildDeployApp entity = buildDeployAppRepository.findByAppId(buildDeployApp.getAppId());
+        BuildDeployApp entity = buildDeployAppRepository.findByApp(buildDeployApp.getApp());
         if (entity != null) {
             buildDeployApp.setId(entity.getId());
             buildDeployApp.setCreateTime(entity.getCreateTime());
@@ -36,27 +45,42 @@ public class BuildDeployAppCrudService implements CrudOps<BuildDeployApp> {
     }
 
     @Override
-    public PageList<BuildDeployApp> getByPage(int page, int size) {
-        // 默认按更新时间倒序
-        PageRequest pageRequest =
-                PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC, "updateTime"));
-        Page<BuildDeployApp> page1 =
-                buildDeployAppRepository.findByIsDeleteFalseAndEnableIsTrueAndEnv("dev", pageRequest);
+    public PageList<BuildDeployApp> getByPage(int page, int size, String env) {
+        PageRequest pageRequest = PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC, "bdTime"));
+        Page<BuildDeployApp> buildDeployApps = buildDeployAppRepository.findAll(pageRequest);
+
         PageList<BuildDeployApp> pageList = new PageList<>();
-        pageList.setTotalSize(page1.getTotalElements());
-        pageList.setTotalPages(page1.getTotalPages());
-        pageList.setList(page1.getContent());
+        int minus = size - buildDeployApps.getContent().size();
+        if (minus > 0) {
+            PageRequest pageRequest1 =
+                    PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC, "updateTime"));
+            Page<AppOrchestration> apps = appRepository.findByIsDeleteFalseAndEnableIsTrueAndEnv(env, pageRequest1);
+            if (minus == size) {
+                pageList.setTotalSize(apps.getTotalElements());
+                pageList.setTotalPages(apps.getTotalPages());
+                pageList.setList(apps.getContent().stream().map(BuildDeployApp::new).collect(Collectors.toList()));
+            } else {
+                Set<BuildDeployApp> set = new HashSet<>(buildDeployApps.getContent());
+                List<BuildDeployApp> list = apps.getContent().stream()
+                        .map(BuildDeployApp::new).collect(Collectors.toList());
+
+                for (BuildDeployApp buildDeployApp : list) {
+                    if (!set.contains(buildDeployApp) && minus != 0) {
+
+                        minus--;
+                    }
+                }
+            }
+        } else {
+            pageList.setTotalSize(buildDeployApps.getTotalElements());
+            pageList.setTotalPages(buildDeployApps.getTotalPages());
+            pageList.setList(buildDeployApps.getContent());
+        }
+
         return pageList;
     }
 
     @Override
     public void delete(String uniqueKey) throws Exception {
-        BuildDeployApp entity = buildDeployAppRepository.findByIsDeleteFalseAndAppId(uniqueKey);
-        if (entity == null) {
-            throw new Exception(uniqueKey + " 不存在");
-        } else {
-            entity.setIsDelete(true);
-            buildDeployAppRepository.save(entity);
-        }
     }
 }

+ 2 - 2
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/crud/orchestarte/AppCrudService.java

@@ -50,7 +50,7 @@ public class AppCrudService implements CrudOps<AppOrchestration> {
         }
         app.setIsDelete(false);
         appRepository.save(app);
-        buildDeployAppCrudService.addOrUpdate(BuildDeployApp.of(app));
+        //buildDeployAppCrudService.addOrUpdate(BuildDeployApp.of(app));
     }
 
     public void copy(String from, String to) throws Exception {
@@ -64,7 +64,7 @@ public class AppCrudService implements CrudOps<AppOrchestration> {
             toApp = (AppOrchestration) fromApp.clone();
             copyToDifferentApp(toApp, to);
             appRepository.save(toApp);
-            buildDeployAppCrudService.addOrUpdate(BuildDeployApp.of(toApp));
+            //buildDeployAppCrudService.addOrUpdate(BuildDeployApp.of(toApp));
         }
     }
 

+ 9 - 4
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/log/BuildDeployLogConsumer.java

@@ -1,9 +1,6 @@
 package cn.reghao.autodop.dmaster.app.service.log;
 
-import cn.reghao.autodop.dmaster.app.entity.log.BuildLog;
-import cn.reghao.autodop.dmaster.app.entity.log.DeployLog;
-import cn.reghao.autodop.dmaster.app.entity.log.CommitLog;
-import cn.reghao.autodop.dmaster.app.entity.log.DeployResult;
+import cn.reghao.autodop.dmaster.app.entity.log.*;
 import lombok.extern.slf4j.Slf4j;
 
 import java.util.concurrent.BlockingQueue;
@@ -36,6 +33,10 @@ public class BuildDeployLogConsumer implements Runnable {
         logQueue.add(deployResult);
     }
 
+    public void addBuildDeployApp(BuildDeployApp buildDeployApp) {
+        logQueue.add(buildDeployApp);
+    }
+
     @Override
     public void run() {
         log.info("日志持久化线程已启动...");
@@ -54,6 +55,10 @@ public class BuildDeployLogConsumer implements Runnable {
                     DeployResult deployResult = (DeployResult) object;
                     logService.saveDeployLog(deployResult);
                     log.info("持久化 {} 部署日志完成...", deployResult.getAppId());
+                } else if (object instanceof BuildDeployApp) {
+                    BuildDeployApp buildDeployApp = (BuildDeployApp) object;
+                    logService.saveBuildDeployApp(buildDeployApp);
+                    log.info("持久化 {} 的构建部署记录完成...", buildDeployApp.getApp().getAppId());
                 }
             } catch (InterruptedException e) {
                 // 中断线程

+ 5 - 2
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/log/BuildDeployLogService.java

@@ -41,12 +41,15 @@ public class BuildDeployLogService {
     @Transactional(rollbackFor = {Exception.class})
     public void saveBuildLog(BuildLog buildLog) throws Exception {
         buildLogRepository.save(buildLog);
-        buildDeployAppCrudService.addOrUpdate(BuildDeployApp.of(buildLog));
     }
 
     @Transactional(rollbackFor = {Exception.class})
     public void saveDeployLog(DeployResult deployResult) throws Exception {
         deployResult.getDeployLogs().forEach(deployLog -> deployLogRepository.save(deployLog));
-        buildDeployAppCrudService.addOrUpdate(BuildDeployApp.of(deployResult));
+    }
+
+    @Transactional(rollbackFor = {Exception.class})
+    public void saveBuildDeployApp(BuildDeployApp buildDeployApp) throws Exception {
+        buildDeployAppCrudService.addOrUpdate(buildDeployApp);
     }
 }

+ 3 - 2
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/vo/log/BuildDeployAppVO.java

@@ -21,8 +21,9 @@ public class BuildDeployAppVO {
 
     public static BuildDeployAppVO of(BuildDeployApp buildDeployApp) {
         BuildDeployAppVO vo = new BuildDeployAppVO();
-        vo.setAppId(buildDeployApp.getAppId());
-        vo.setEnv(buildDeployApp.getEnv());
+        vo.setAppId(buildDeployApp.getApp().getAppId());
+        vo.setEnv(buildDeployApp.getApp().getEnv());
+
         vo.setCommitId(buildDeployApp.getCommitId());
         vo.setBdType(buildDeployApp.getBdType());
         if (buildDeployApp.getBdTime() != null) {

+ 1 - 1
dmaster/src/main/java/cn/reghao/autodop/dmaster/common/exception/ExceptionUtil.java

@@ -13,7 +13,7 @@ public class ExceptionUtil {
         sb.append(e.getMessage());
         Throwable throwable = e.getCause();
         if (throwable != null) {
-            sb.append(throwable.getMessage());
+            sb.append(System.lineSeparator()).append(throwable.getMessage());
         }
 
         return sb.toString();