|
|
@@ -3,6 +3,7 @@ package cn.reghao.devops.manager.app.service.bd.impl;
|
|
|
import cn.reghao.devops.common.agent.app.dto.AppInfo;
|
|
|
import cn.reghao.devops.common.msg.constant.NodeStatus;
|
|
|
import cn.reghao.devops.common.msg.event.EvtAppStatResult;
|
|
|
+import cn.reghao.devops.manager.app.db.repository.AppDeployingRepository;
|
|
|
import cn.reghao.devops.manager.app.db.repository.log.BuildLogRepository;
|
|
|
import cn.reghao.devops.manager.app.db.repository.log.DeployLogRepository;
|
|
|
import cn.reghao.devops.manager.app.model.constant.DeployStatus;
|
|
|
@@ -16,20 +17,18 @@ import cn.reghao.devops.manager.app.model.vo.AppDeployingVO;
|
|
|
import cn.reghao.devops.manager.app.model.vo.AppRunning;
|
|
|
import cn.reghao.devops.manager.app.model.vo.AppRunningVO;
|
|
|
import cn.reghao.devops.manager.app.service.bd.BuildDeployNotify;
|
|
|
-import cn.reghao.devops.manager.app.service.bd.BuildDeployStat;
|
|
|
import cn.reghao.devops.manager.app.service.bd.DeployStat;
|
|
|
import cn.reghao.devops.manager.machine.model.po.MachineHost;
|
|
|
import cn.reghao.devops.manager.machine.service.MachineService;
|
|
|
import cn.reghao.jutil.jdk.converter.DateTimeConverter;
|
|
|
-import cn.reghao.jutil.jdk.db.PageBound;
|
|
|
import cn.reghao.jutil.jdk.db.PageList;
|
|
|
import cn.reghao.jutil.jdk.result.ResultStatus;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
import java.util.*;
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -39,65 +38,50 @@ import java.util.stream.Collectors;
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class DeployStatImpl implements DeployStat {
|
|
|
- // appId -> machineId -> AppDeploying
|
|
|
- private final static Map<String, Map<String, AppDeploying>> appDeployings = new ConcurrentHashMap<>();
|
|
|
-
|
|
|
- private final BuildDeployStat buildDeployStat;
|
|
|
private final BuildLogRepository buildLogRepository;
|
|
|
private final DeployLogRepository deployLogRepository;
|
|
|
private final BuildDeployNotify buildDeployNotify;
|
|
|
private final MachineService machineService;
|
|
|
+ private final AppDeployingRepository deployingRepository;
|
|
|
|
|
|
- public DeployStatImpl(BuildDeployStat buildDeployStat, BuildLogRepository buildLogRepository,
|
|
|
- DeployLogRepository deployLogRepository, BuildDeployNotify buildDeployNotify,
|
|
|
- MachineService machineService) {
|
|
|
- this.buildDeployStat = buildDeployStat;
|
|
|
+ public DeployStatImpl(BuildLogRepository buildLogRepository, DeployLogRepository deployLogRepository,
|
|
|
+ BuildDeployNotify buildDeployNotify, MachineService machineService,
|
|
|
+ AppDeployingRepository deployingRepository) {
|
|
|
this.buildLogRepository = buildLogRepository;
|
|
|
this.deployLogRepository = deployLogRepository;
|
|
|
this.buildDeployNotify = buildDeployNotify;
|
|
|
this.machineService = machineService;
|
|
|
- }
|
|
|
-
|
|
|
- @PostConstruct
|
|
|
- private void loadFromDataSource() {
|
|
|
- buildDeployStat.getAppDeployings().forEach(appDeploying -> {
|
|
|
- String appId = appDeploying.getAppDeployConfig().getAppConfig().getAppId();
|
|
|
- String machineId = appDeploying.getAppDeployConfig().getMachineHost().getMachineId();
|
|
|
- appDeployings.computeIfAbsent(appId, k -> new HashMap<>()).put(machineId, appDeploying);
|
|
|
- });
|
|
|
+ this.deployingRepository = deployingRepository;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void add(AppDeployConfig appDeployConfig) {
|
|
|
- String appId = appDeployConfig.getAppConfig().getAppId();
|
|
|
- String machineId = appDeployConfig.getMachineHost().getMachineId();
|
|
|
-
|
|
|
AppDeploying appDeploying = new AppDeploying(appDeployConfig);
|
|
|
- appDeployings.computeIfAbsent(appId, k -> new HashMap<>()).putIfAbsent(machineId, appDeploying);
|
|
|
+ deployingRepository.save(appDeploying);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void update(AppDeployConfig appDeployConfig) {
|
|
|
String appId = appDeployConfig.getAppConfig().getAppId();
|
|
|
String machineId = appDeployConfig.getMachineHost().getMachineId();
|
|
|
- appDeployings.get(appId).get(machineId).setAppDeployConfig(appDeployConfig);
|
|
|
+ AppDeploying appDeploying = deployingRepository.findAllByAppDeployConfig_AppConfig_AppIdAndAppDeployConfig_MachineHost_MachineId(appId, machineId);
|
|
|
+ appDeploying.setAppDeployConfig(appDeployConfig);
|
|
|
+ deployingRepository.save(appDeploying);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean delete(AppDeployConfig appDeployConfig) {
|
|
|
String appId = appDeployConfig.getAppConfig().getAppId();
|
|
|
- if (appDeployings.get(appId) == null) {
|
|
|
+ String machineId = appDeployConfig.getMachineHost().getMachineId();
|
|
|
+ AppDeploying appDeploying = deployingRepository.findAllByAppDeployConfig_AppConfig_AppIdAndAppDeployConfig_MachineHost_MachineId(appId, machineId);
|
|
|
+ if (appDeploying == null) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- String machineId = appDeployConfig.getMachineHost().getMachineId();
|
|
|
boolean running = isRunning(appId, machineId);
|
|
|
if (!running) {
|
|
|
- appDeployings.get(appId).remove(machineId);
|
|
|
- if (appDeployings.get(appId).isEmpty()) {
|
|
|
- appDeployings.remove(appId);
|
|
|
- return true;
|
|
|
- }
|
|
|
+ deployingRepository.delete(appDeploying);
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
@@ -106,22 +90,24 @@ public class DeployStatImpl implements DeployStat {
|
|
|
@Override
|
|
|
public void afterBuild(AppBuilding appBuilding) {
|
|
|
String appId = appBuilding.getAppConfig().getAppId();
|
|
|
- Map<String, AppDeploying> map = appDeployings.get(appId);
|
|
|
- if (map == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- map.values().forEach(appDeploying -> {
|
|
|
+ List<AppDeploying> list = deployingRepository.findAllByAppDeployConfig_AppConfig_AppId(appId);
|
|
|
+ list.forEach(appDeploying -> {
|
|
|
appDeploying.afterBuild(appBuilding);
|
|
|
});
|
|
|
+ deployingRepository.saveAll(list);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void beforeDeploy(AppBuilding appBuilding, String machineId, String deployBy) {
|
|
|
String appId = appBuilding.getAppConfig().getAppId();
|
|
|
- Map<String, AppDeploying> map = appDeployings.computeIfAbsent(appId, k -> new HashMap<>());
|
|
|
- AppDeploying appDeploying = map.get(machineId);
|
|
|
- appDeploying.beforeDeploy(appBuilding, deployBy);
|
|
|
+ List<AppDeploying> list = deployingRepository.findAllByAppDeployConfig_AppConfig_AppId(appId);
|
|
|
+ for (AppDeploying appDeploying : list) {
|
|
|
+ String machineId1 = appDeploying.getAppDeployConfig().getMachineHost().getMachineId();
|
|
|
+ if (machineId1.equals(machineId)) {
|
|
|
+ appDeploying.beforeDeploy(appBuilding, deployBy);
|
|
|
+ deployingRepository.save(appDeploying);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -130,20 +116,25 @@ public class DeployStatImpl implements DeployStat {
|
|
|
|
|
|
String appId = deployResult.getAppId();
|
|
|
String machineId = deployResult.getMachineId();
|
|
|
- AppDeploying appDeploying = appDeployings.get(appId).get(machineId);
|
|
|
- appDeploying.afterDeploy(deployResult);
|
|
|
-
|
|
|
- if (deployResult.getResult().getCode() == ResultStatus.SUCCESS.getCode()) {
|
|
|
- setAppStatus(appDeploying, deployResult);
|
|
|
+ List<AppDeploying> list = deployingRepository.findAllByAppDeployConfig_AppConfig_AppId(appId);
|
|
|
+ for (AppDeploying appDeploying : list) {
|
|
|
+ String machineId1 = appDeploying.getAppDeployConfig().getMachineHost().getMachineId();
|
|
|
+ if (machineId1.equals(machineId)) {
|
|
|
+ appDeploying.afterDeploy(deployResult);
|
|
|
+ if (deployResult.getResult().getCode() == ResultStatus.SUCCESS.getCode()) {
|
|
|
+ setAppStatus(appDeploying, deployResult);
|
|
|
+ }
|
|
|
+ deployingRepository.save(appDeploying);
|
|
|
+
|
|
|
+ MachineHost machineHost = appDeploying.getAppDeployConfig().getMachineHost();
|
|
|
+ BuildLog buildLog = buildLogRepository.findByBuildLogId(appDeploying.getAppBuilding().getBuildLogId());
|
|
|
+ DeployLog deployLog = new DeployLog(buildLog, machineHost, deployResult);
|
|
|
+ deployLogRepository.save(deployLog);
|
|
|
+
|
|
|
+ buildDeployNotify.deployNotify(deployLog);
|
|
|
+ log.info("{} 的通知已发送", deployResult.getAppId());
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- MachineHost machineHost = appDeploying.getAppDeployConfig().getMachineHost();
|
|
|
- BuildLog buildLog = buildLogRepository.findByBuildLogId(appDeploying.getAppBuilding().getBuildLogId());
|
|
|
- DeployLog deployLog = new DeployLog(buildLog, machineHost, deployResult);
|
|
|
- deployLogRepository.save(deployLog);
|
|
|
-
|
|
|
- buildDeployNotify.deployNotify(deployLog);
|
|
|
- log.info("{} 的通知已发送", deployResult.getAppId());
|
|
|
}
|
|
|
|
|
|
private void setAppStatus(AppDeploying appDeploying, EvtAppStatResult deployResult) {
|
|
|
@@ -157,15 +148,19 @@ public class DeployStatImpl implements DeployStat {
|
|
|
public void refresh(EvtAppStatResult statResult) {
|
|
|
String appId = statResult.getAppId();
|
|
|
String machineId = statResult.getMachineId();
|
|
|
- AppDeploying appDeploying = appDeployings.get(appId).get(machineId);
|
|
|
- setAppStatus(appDeploying, statResult);
|
|
|
+ List<AppDeploying> list = deployingRepository.findAllByAppDeployConfig_AppConfig_AppId(appId);
|
|
|
+ for (AppDeploying appDeploying : list) {
|
|
|
+ String machineId1 = appDeploying.getAppDeployConfig().getMachineHost().getMachineId();
|
|
|
+ if (machineId1.equals(machineId)) {
|
|
|
+ setAppStatus(appDeploying, statResult);
|
|
|
+ deployingRepository.save(appDeploying);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<AppInfo> getAppsByMachineId(String machineId) {
|
|
|
- return appDeployings.values().stream()
|
|
|
- .map(Map::values)
|
|
|
- .flatMap(Collection::stream)
|
|
|
+ return deployingRepository.findAll().stream()
|
|
|
.filter(appDeploying -> appDeploying.getAppDeployConfig().getMachineHost().getMachineId().equals(machineId))
|
|
|
.map(appDeploying -> {
|
|
|
String packType = appDeploying.getAppDeployConfig().getAppConfig().getPackerConfig().getType();
|
|
|
@@ -177,13 +172,12 @@ public class DeployStatImpl implements DeployStat {
|
|
|
|
|
|
@Override
|
|
|
public List<AppDeployingVO> get(String appId, AppBuilding appBuilding) {
|
|
|
- Map<String, AppDeploying> map = appDeployings.get(appId);
|
|
|
- if (map == null) {
|
|
|
+ List<AppDeploying> list = deployingRepository.findAllByAppDeployConfig_AppConfig_AppId(appId);
|
|
|
+ if (list.isEmpty()) {
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
|
|
|
- return map.values().stream()
|
|
|
- .map(appDeploying -> {
|
|
|
+ return list.stream().map(appDeploying -> {
|
|
|
MachineHost machineHost = appDeploying.getAppDeployConfig().getMachineHost();
|
|
|
String machineStatus = machineService.isAgentOnline(machineHost.getMachineId())
|
|
|
? NodeStatus.Online.name() : NodeStatus.Offline.name();
|
|
|
@@ -208,39 +202,24 @@ public class DeployStatImpl implements DeployStat {
|
|
|
|
|
|
@Override
|
|
|
public PageList<AppRunningVO> getByPage(String env, int pageNumber, int pageSize) {
|
|
|
- List<Map<String, AppDeploying>> list0 = appDeployings.values().stream()
|
|
|
- .map(appDeployingMap -> appDeployingMap.entrySet().stream()
|
|
|
- .filter(entry1 -> entry1.getValue().getAppDeployConfig().getAppConfig().getEnv().equals(env))
|
|
|
- .filter(entry1 -> entry1.getValue().isDeployed())
|
|
|
- .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)))
|
|
|
- .filter(map -> !map.isEmpty())
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
- int total = list0.size();
|
|
|
- if (total == 0) {
|
|
|
- return PageList.empty();
|
|
|
- }
|
|
|
+ PageRequest pageRequest = PageRequest.of(pageNumber, pageSize);
|
|
|
+ Page<AppDeploying> page = deployingRepository.findAllByAppDeployConfig_AppConfig_Env(env, pageRequest);
|
|
|
+ List<AppRunningVO> list2 = page.getContent().stream().map(appDeploying -> {
|
|
|
+ AppConfig appConfig = appDeploying.getAppDeployConfig().getAppConfig();
|
|
|
+ String appId = appConfig.getAppId();
|
|
|
+ String appName = appConfig.getAppName();
|
|
|
+ String bindPorts = appConfig.getBindPorts();
|
|
|
+ String packagePath = appDeploying.getAppBuilding().getPackagePath();
|
|
|
+ int totalDeployed = 0;
|
|
|
+ return new AppRunningVO(appId, appName, bindPorts, packagePath, totalDeployed);
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
|
|
- PageBound pageBound = PageBound.getPageBound(pageNumber, pageSize, total);
|
|
|
- List<AppRunningVO> list2 = list0.subList(pageBound.getStart(), pageBound.getEnd()).stream()
|
|
|
- .map(map -> {
|
|
|
- AppDeploying appDeploying = map.values().iterator().next();
|
|
|
- AppConfig appConfig = appDeploying.getAppDeployConfig().getAppConfig();
|
|
|
- String appId = appConfig.getAppId();
|
|
|
- String appName = appConfig.getAppName();
|
|
|
- String bindPorts = appConfig.getBindPorts();
|
|
|
- String packagePath = appDeploying.getAppBuilding().getPackagePath();
|
|
|
- int totalDeployed = map.size();
|
|
|
- return new AppRunningVO(appId, appName, bindPorts, packagePath, totalDeployed);
|
|
|
- })
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
- return PageList.pageList(pageNumber, pageSize, total, list2);
|
|
|
+ return PageList.pageList(pageNumber, pageSize, (int) page.getTotalElements(), list2);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<AppRunning> getAppRunning(String appId) {
|
|
|
- return appDeployings.get(appId).values().stream()
|
|
|
+ return deployingRepository.findAllByAppDeployConfig_AppConfig_AppId(appId).stream()
|
|
|
.filter(AppDeploying::isDeployed)
|
|
|
//.sorted(Comparator.comparing(AppDeploying::getDeployTime).reversed())
|
|
|
.map(AppRunning::new)
|
|
|
@@ -249,25 +228,22 @@ public class DeployStatImpl implements DeployStat {
|
|
|
|
|
|
@Override
|
|
|
public boolean isDeploying(String appId, String machineId) {
|
|
|
- AppDeploying appDeploying = appDeployings.get(appId).get(machineId);
|
|
|
+ AppDeploying appDeploying = deployingRepository.findAllByAppDeployConfig_AppConfig_AppIdAndAppDeployConfig_MachineHost_MachineId(appId, machineId);
|
|
|
return appDeploying.isOnDeploying();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean isRunning(String appId) {
|
|
|
- Map<String, AppDeploying> map = appDeployings.get(appId);
|
|
|
- return map != null;
|
|
|
+ List<AppDeploying> list = deployingRepository.findAllByAppDeployConfig_AppConfig_AppId(appId);
|
|
|
+ return !list.isEmpty();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean isRunning(String appId, String machineId) {
|
|
|
- Map<String, AppDeploying> map = appDeployings.get(appId);
|
|
|
- if (map != null) {
|
|
|
- AppDeploying appDeploying = map.get(machineId);
|
|
|
- if (appDeploying != null) {
|
|
|
- int pid = appDeploying.getPid();
|
|
|
- return pid != -1;
|
|
|
- }
|
|
|
+ AppDeploying appDeploying = deployingRepository.findAllByAppDeployConfig_AppConfig_AppIdAndAppDeployConfig_MachineHost_MachineId(appId, machineId);
|
|
|
+ if (appDeploying != null) {
|
|
|
+ int pid = appDeploying.getPid();
|
|
|
+ return pid != -1;
|
|
|
}
|
|
|
|
|
|
return false;
|