|
|
@@ -1,25 +1,18 @@
|
|
|
package cn.reghao.autodop.dmaster.app.service;
|
|
|
|
|
|
-import cn.reghao.autodop.common.utils.DateTimeConverter;
|
|
|
import cn.reghao.autodop.dmaster.app.cache.BuildDeployCache;
|
|
|
-import cn.reghao.autodop.dmaster.app.constant.BuildDeployResult;
|
|
|
-import cn.reghao.autodop.dmaster.app.constant.BuildStage;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.config.deploy.DeployConfig;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.log.*;
|
|
|
-import cn.reghao.autodop.dmaster.app.entity.status.BuildDeployApp;
|
|
|
import cn.reghao.autodop.dmaster.app.interceptor.AppIntegrateReinitInterceptor;
|
|
|
+import cn.reghao.autodop.dmaster.app.repository.log.BuildLogRepository;
|
|
|
import cn.reghao.autodop.dmaster.app.service.bd.AppIntegrate;
|
|
|
-import cn.reghao.autodop.dmaster.app.service.bd.tools.repo.CommitInfo;
|
|
|
import cn.reghao.autodop.dmaster.app.service.bd.AppDeployer;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.config.AppOrchestration;
|
|
|
-import cn.reghao.autodop.dmaster.app.service.log.BuildDeployLogConsumer;
|
|
|
-import cn.reghao.autodop.common.utils.ExceptionUtil;
|
|
|
import cn.reghao.autodop.dmaster.utils.notifier.NotifyService;
|
|
|
import cn.reghao.autodop.dmaster.common.thread.ThreadPoolWrapper;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.*;
|
|
|
|
|
|
@@ -33,26 +26,25 @@ import java.util.concurrent.*;
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class BuildDeployDispatcher {
|
|
|
+ private BuildLogRepository buildLogRepository;
|
|
|
private AppIntegrateReinitInterceptor reinitInterceptor;
|
|
|
// TODO 多线程访问的集合
|
|
|
private Set<String> onBuilding = new ConcurrentSkipListSet<>();
|
|
|
private Set<String> onDeploying = new ConcurrentSkipListSet<>();
|
|
|
private Map<String, AppIntegrate> integrateMap = new ConcurrentHashMap<>();
|
|
|
private BuildDeployCache cache;
|
|
|
- private BuildDeployLogConsumer logConsumer;
|
|
|
- private NotifyService notifyService;
|
|
|
+ private ExecutorService threadPool;
|
|
|
private AppDeployer appDeployer;
|
|
|
+ private NotifyService notifyService;
|
|
|
|
|
|
public BuildDeployDispatcher(AppIntegrateReinitInterceptor reinitInterceptor, BuildDeployCache cache,
|
|
|
- BuildDeployLogConsumer log, NotifyService notify, AppDeployer appDeployer) {
|
|
|
+ BuildLogRepository buildLogRepository, NotifyService notify, AppDeployer appDeployer) {
|
|
|
this.reinitInterceptor = reinitInterceptor;
|
|
|
this.cache = cache;
|
|
|
- this.logConsumer = log;
|
|
|
+ this.buildLogRepository = buildLogRepository;
|
|
|
this.notifyService = notify;
|
|
|
this.appDeployer = appDeployer;
|
|
|
-
|
|
|
- ExecutorService threadPool = ThreadPoolWrapper.threadPool("build-deploy");
|
|
|
- threadPool.execute(log);
|
|
|
+ this.threadPool = ThreadPoolWrapper.threadPool("build-deploy");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -62,9 +54,9 @@ public class BuildDeployDispatcher {
|
|
|
* @return
|
|
|
* @date 2021-02-06 上午1:46
|
|
|
*/
|
|
|
- public BuildDeployApp buildAndDeploy(String appId, boolean isDeploy) throws Exception {
|
|
|
+ public void buildAndDeploy(String appId, boolean isDeploy) throws Exception {
|
|
|
if (!onBuilding.add(appId)) {
|
|
|
- return BuildDeployApp.onBuilding(appId);
|
|
|
+ //
|
|
|
}
|
|
|
|
|
|
AppIntegrate appIntegrate = integrateMap.get(appId);
|
|
|
@@ -83,74 +75,16 @@ public class BuildDeployDispatcher {
|
|
|
integrateMap.put(appId, appIntegrate);
|
|
|
}
|
|
|
|
|
|
- // TODO 抛出异常时从 onBuilding 和 onDeploying 中移除当前应用
|
|
|
- BuildLog buildLog = buildPipeline(appIntegrate);
|
|
|
- logConsumer.addBuildLog(buildLog);
|
|
|
- onBuilding.remove(appId);
|
|
|
- BuildDeployApp buildDeployApp;
|
|
|
- if (buildLog.getStatusCode() == 0 && isDeploy) {
|
|
|
- buildDeployApp = deployApp(buildLog);
|
|
|
- } else {
|
|
|
- buildDeployApp = BuildDeployApp.of(buildLog);
|
|
|
- }
|
|
|
- logConsumer.addBuildDeployApp(buildDeployApp);
|
|
|
- return buildDeployApp;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 更新 -> 编译 -> 打包 -> 构建流水线
|
|
|
- * TODO 检测超时,避免阻塞
|
|
|
- *
|
|
|
- * @date 2019-11-07 下午10:14
|
|
|
- */
|
|
|
- private BuildLog buildPipeline(AppIntegrate appIntegrate) {
|
|
|
- BuildLog buildLog = BuildLog.of(appIntegrate.app());
|
|
|
- long start = System.currentTimeMillis();
|
|
|
- // TODO 只使用一个 try-catch
|
|
|
- try {
|
|
|
- CommitInfo commitInfo = appIntegrate.update();
|
|
|
- buildLog.setCommitId(commitInfo.getCommitId());
|
|
|
- buildLog.setCommitTime(DateTimeConverter.localDateTime(commitInfo.getMsCommitTime()));
|
|
|
- buildLog.setUpdateTotalTime(System.currentTimeMillis()-start);
|
|
|
-
|
|
|
- CommitLog commitLog = CommitLog.of(appIntegrate.app(), commitInfo);
|
|
|
- logConsumer.addCommitLog(commitLog);
|
|
|
- } catch (Exception e) {
|
|
|
- buildException(buildLog, BuildStage.update, e);
|
|
|
- return buildLog;
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- start = System.currentTimeMillis();
|
|
|
- appIntegrate.compile();
|
|
|
- buildLog.setCompileTotalTime(System.currentTimeMillis()-start);
|
|
|
- } catch (Exception e) {
|
|
|
- buildException(buildLog, BuildStage.compile, e);
|
|
|
- return buildLog;
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- start = System.currentTimeMillis();
|
|
|
- String packagePath = appIntegrate.pack();
|
|
|
- buildLog.setPackagePath(packagePath);
|
|
|
- buildLog.setPackTotalTime(System.currentTimeMillis()-start);
|
|
|
- } catch (Exception e) {
|
|
|
- buildException(buildLog, BuildStage.pack, e);
|
|
|
- return buildLog;
|
|
|
- }
|
|
|
-
|
|
|
- buildLog.setStage(BuildStage.done);
|
|
|
- buildLog.setStatusCode(0);
|
|
|
- buildLog.setResult(BuildDeployResult.buildDone.getName());
|
|
|
- buildLog.setBuildTime(LocalDateTime.now());
|
|
|
- return buildLog;
|
|
|
- }
|
|
|
-
|
|
|
- private void buildException(BuildLog buildLog, BuildStage stage, Exception e) {
|
|
|
- buildLog.setStage(stage);
|
|
|
- buildLog.setStatusCode(1);
|
|
|
- buildLog.setResult(ExceptionUtil.errorMsg(e));
|
|
|
- buildLog.setBuildTime(LocalDateTime.now());
|
|
|
+ BuildSupplier supplier = new BuildSupplier(appIntegrate);
|
|
|
+ CompletableFuture.supplyAsync(supplier, threadPool)
|
|
|
+ .thenApply(buildLog -> {
|
|
|
+ buildLogRepository.save(buildLog);
|
|
|
+ onBuilding.remove(appId);
|
|
|
+ if (isDeploy) {
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ })
|
|
|
+ .complete(true);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -160,27 +94,22 @@ public class BuildDeployDispatcher {
|
|
|
* @return
|
|
|
* @date 2021-02-06 上午2:02
|
|
|
*/
|
|
|
- private BuildDeployApp deployApp(BuildLog buildLog) {
|
|
|
+ private void deployApp(BuildLog buildLog) {
|
|
|
String appId = buildLog.getAppId();
|
|
|
if (!onDeploying.add(appId)) {
|
|
|
- return BuildDeployApp.onDeploying(appId);
|
|
|
}
|
|
|
|
|
|
List<DeployConfig> deployConfigs = cache.findByAppId(buildLog.getAppId()).getDeployConfigs();
|
|
|
if (deployConfigs.isEmpty()) {
|
|
|
- return BuildDeployApp.deployResult(buildLog, 0, BuildDeployResult.noDeployConfig.getName());
|
|
|
}
|
|
|
|
|
|
List<DeployLog> deployLogs = appDeployer.deploy(buildLog, deployConfigs);
|
|
|
onDeploying.remove(buildLog.getAppId());
|
|
|
- deployLogs.forEach(deployLog -> logConsumer.addDeployLog(deployLog));
|
|
|
|
|
|
// TODO 部署完成后发出通知
|
|
|
/*NotifyReceiver notifyReceiver = cache.findByAppId(buildLog.getAppId()).getNotifyReceiver();
|
|
|
DeployNotifyMsg deployNotifyMsg = DeployNotifyMsg.deployNotifyMsg(deployLogs);
|
|
|
notifyService.notify(notifyReceiver, deployNotifyMsg);*/
|
|
|
-
|
|
|
- return BuildDeployApp.of(deployLogs);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -190,10 +119,6 @@ public class BuildDeployDispatcher {
|
|
|
* @return
|
|
|
* @date 2021-02-25 下午10:35
|
|
|
*/
|
|
|
- public BuildDeployApp deploy(String appId, String commitId) {
|
|
|
- BuildLog buildLog = cache.findByAppIdAndCommitId(appId, commitId);
|
|
|
- BuildDeployApp buildDeployApp = deployApp(buildLog);
|
|
|
- logConsumer.addBuildDeployApp(buildDeployApp);
|
|
|
- return buildDeployApp;
|
|
|
+ public void deploy(String appId, String commitId) {
|
|
|
}
|
|
|
}
|