|
@@ -3,6 +3,8 @@ package cn.reghao.devops.web.mgr.app.service.bd.impl;
|
|
|
import cn.reghao.devops.web.admin.account.model.po.User;
|
|
import cn.reghao.devops.web.admin.account.model.po.User;
|
|
|
import cn.reghao.devops.web.mgr.app.model.po.AppBuilding;
|
|
import cn.reghao.devops.web.mgr.app.model.po.AppBuilding;
|
|
|
import cn.reghao.devops.web.mgr.app.model.po.config.AppConfig;
|
|
import cn.reghao.devops.web.mgr.app.model.po.config.AppConfig;
|
|
|
|
|
+import cn.reghao.devops.web.mgr.app.model.vo.AppTask;
|
|
|
|
|
+import cn.reghao.devops.web.mgr.app.model.vo.BuildTask;
|
|
|
import cn.reghao.devops.web.mgr.app.service.PermissionCheck;
|
|
import cn.reghao.devops.web.mgr.app.service.PermissionCheck;
|
|
|
import cn.reghao.devops.web.mgr.app.service.bd.BuildApp;
|
|
import cn.reghao.devops.web.mgr.app.service.bd.BuildApp;
|
|
|
import cn.reghao.devops.web.mgr.app.service.bd.BuildDeployNotify;
|
|
import cn.reghao.devops.web.mgr.app.service.bd.BuildDeployNotify;
|
|
@@ -19,11 +21,10 @@ import cn.reghao.jutil.tool.id.IdGenerator;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
-import java.util.HashMap;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @author reghao
|
|
* @author reghao
|
|
@@ -34,7 +35,7 @@ import java.util.concurrent.ExecutorService;
|
|
|
public class BuildAppImpl implements BuildApp {
|
|
public class BuildAppImpl implements BuildApp {
|
|
|
private final IdGenerator idGenerator = new IdGenerator(10, "build-log-id");
|
|
private final IdGenerator idGenerator = new IdGenerator(10, "build-log-id");
|
|
|
private final ExecutorService threadPool = ThreadPoolWrapper.threadPool("build-pool", 10);
|
|
private final ExecutorService threadPool = ThreadPoolWrapper.threadPool("build-pool", 10);
|
|
|
- private final Map<String, CompletableFuture<HandlerResult>> buildMap = new HashMap<>(10);
|
|
|
|
|
|
|
+ private final Map<String, AppTask> taskMap = new HashMap<>(10);
|
|
|
private final BuildStat buildStat;
|
|
private final BuildStat buildStat;
|
|
|
private final DeployApp deployApp;
|
|
private final DeployApp deployApp;
|
|
|
private final BuildDeployNotify buildDeployNotify;
|
|
private final BuildDeployNotify buildDeployNotify;
|
|
@@ -48,15 +49,15 @@ public class BuildAppImpl implements BuildApp {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public void buildAndDeploy(String appId, boolean deploy) throws Exception {
|
|
|
|
|
|
|
+ public Result buildAndDeploy(String appId, boolean deploy) throws Exception {
|
|
|
if (buildStat.isBuilding(appId)) {
|
|
if (buildStat.isBuilding(appId)) {
|
|
|
- log.info("{} 正在构建中", appId);
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ String msg = String.format("%s 正在构建中", appId);
|
|
|
|
|
+ return Result.result(ResultStatus.FAIL, msg);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (buildMap.size() >= 10) {
|
|
|
|
|
- log.info("已有 {} 个 app 正在构建中", buildMap.size());
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ if (taskMap.size() >= 10) {
|
|
|
|
|
+ String msg = String.format("已有 %s 个 app 正在构建中", taskMap.size());
|
|
|
|
|
+ return Result.result(ResultStatus.FAIL, msg);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
AppConfig appConfig = buildStat.getAppConfig(appId);
|
|
AppConfig appConfig = buildStat.getAppConfig(appId);
|
|
@@ -68,6 +69,7 @@ public class BuildAppImpl implements BuildApp {
|
|
|
String buildBy = user.getScreenName();
|
|
String buildBy = user.getScreenName();
|
|
|
AppDto appDto = appConfig.getAppDto();
|
|
AppDto appDto = appConfig.getAppDto();
|
|
|
localBuild(appDto, buildBy, deploy);
|
|
localBuild(appDto, buildBy, deploy);
|
|
|
|
|
+ return Result.success("构建部署任务已提交");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void localBuild(AppDto appDto, String buildBy, boolean deploy) throws Exception {
|
|
private void localBuild(AppDto appDto, String buildBy, boolean deploy) throws Exception {
|
|
@@ -86,7 +88,7 @@ public class BuildAppImpl implements BuildApp {
|
|
|
Result result = buildChainResult.getResult();
|
|
Result result = buildChainResult.getResult();
|
|
|
AppBuilding appBuilding = buildStat.afterBuild(buildChainResult);
|
|
AppBuilding appBuilding = buildStat.afterBuild(buildChainResult);
|
|
|
log.info("{} 异步构建完成", appId);
|
|
log.info("{} 异步构建完成", appId);
|
|
|
- buildMap.remove(appId);
|
|
|
|
|
|
|
+ taskMap.remove(appId);
|
|
|
if (appBuilding == null) {
|
|
if (appBuilding == null) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -97,20 +99,37 @@ public class BuildAppImpl implements BuildApp {
|
|
|
buildDeployNotify.buildNotify(appBuilding);
|
|
buildDeployNotify.buildNotify(appBuilding);
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
- buildMap.put(appId, future);
|
|
|
|
|
|
|
+ taskMap.put(appId, new AppTask(appDto, future));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void getBuildStat() {
|
|
|
|
|
- buildMap.forEach((appId, future) -> {
|
|
|
|
|
- if (future.isDone()) {
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<BuildTask> getBuildTasks() {
|
|
|
|
|
+ ThreadPoolExecutor tpe = (ThreadPoolExecutor) threadPool;
|
|
|
|
|
+ int active = tpe.getActiveCount();
|
|
|
|
|
+ long total = tpe.getTaskCount();
|
|
|
|
|
+ log.info("total -> {}, active -> {}", total, active);
|
|
|
|
|
+
|
|
|
|
|
+ List<BuildTask> list = new ArrayList<>();
|
|
|
|
|
+ taskMap.forEach((appId, appTask) -> {
|
|
|
|
|
+ AppDto appDto = appTask.getApp();
|
|
|
|
|
+ if (appTask.getFuture().isDone()) {
|
|
|
|
|
+ list.add(new BuildTask(appDto, "已完成"));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ list.add(new BuildTask(appDto, "进行中"));
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
+
|
|
|
|
|
+ return list;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public synchronized void cancel(String appId) {
|
|
|
|
|
- CompletableFuture<HandlerResult> future = buildMap.get(appId);
|
|
|
|
|
- if (future != null && !future.isDone()) {
|
|
|
|
|
- future.cancel(true);
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public synchronized void cancelTask(String appId) {
|
|
|
|
|
+ AppTask appTask = taskMap.get(appId);
|
|
|
|
|
+ if (appTask != null) {
|
|
|
|
|
+ CompletableFuture<HandlerResult> future = appTask.getFuture();
|
|
|
|
|
+ if (!future.isDone()) {
|
|
|
|
|
+ future.cancel(true);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|