|
|
@@ -6,32 +6,16 @@ import cn.reghao.autodop.common.message.CallResult;
|
|
|
import cn.reghao.autodop.common.message.ops.AppOps;
|
|
|
import cn.reghao.autodop.common.mqtt.DefaultMqttClient;
|
|
|
import cn.reghao.autodop.common.utils.serializer.JsonConverter;
|
|
|
-import cn.reghao.autodop.dmaster.app.db.query.config.AppQuery;
|
|
|
-import cn.reghao.autodop.dmaster.app.entity.AppDeploying;
|
|
|
-import cn.reghao.autodop.dmaster.app.entity.AppRunning;
|
|
|
-import cn.reghao.autodop.dmaster.app.entity.config.AppOrchestration;
|
|
|
-import cn.reghao.autodop.dmaster.app.entity.log.BuildLog;
|
|
|
-import cn.reghao.autodop.dmaster.app.entity.log.DeployLog;
|
|
|
-import cn.reghao.autodop.dmaster.app.repository.AppDeployingRepository;
|
|
|
-import cn.reghao.autodop.dmaster.app.repository.AppRunningRepository;
|
|
|
-import cn.reghao.autodop.dmaster.app.repository.log.BuildLogRepository;
|
|
|
-import cn.reghao.autodop.dmaster.app.repository.log.DeployLogRepository;
|
|
|
-import cn.reghao.autodop.dmaster.app.service.bd.DeployNotifyMsg;
|
|
|
+import cn.reghao.autodop.dmaster.app.service.AppOpsResultService;
|
|
|
import cn.reghao.autodop.common.message.ops.OpsProcessor;
|
|
|
-import cn.reghao.autodop.dmaster.notification.entity.NotifyGroup;
|
|
|
-import cn.reghao.autodop.dmaster.notification.entity.NotifyType;
|
|
|
-import cn.reghao.autodop.dmaster.notification.service.notifier.ding.DingMsg;
|
|
|
-import cn.reghao.autodop.dmaster.notification.service.NotifyService;
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.lang.reflect.Type;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Optional;
|
|
|
|
|
|
/**
|
|
|
- * 分发 App 相关的消息
|
|
|
+ * 分发 App Result 相关的消息
|
|
|
*
|
|
|
* @author reghao
|
|
|
* @date 2020-12-30 10:26:47
|
|
|
@@ -39,27 +23,11 @@ import java.util.Optional;
|
|
|
@Slf4j
|
|
|
@Component
|
|
|
public class AppOpsProcessor implements OpsProcessor {
|
|
|
- private BuildLogRepository buildLogRepository;
|
|
|
- private DeployLogRepository deployLogRepository;
|
|
|
- private AppDeployingRepository deployingRepository;
|
|
|
- private AppRunningRepository runningRepository;
|
|
|
- private AppQuery appQuery;
|
|
|
- private NotifyService notifyService;
|
|
|
+ private AppOpsResultService appOpsResultService;
|
|
|
private DefaultMqttClient mqttClient;
|
|
|
|
|
|
- public AppOpsProcessor(BuildLogRepository buildLogRepository,
|
|
|
- DeployLogRepository deployLogRepository,
|
|
|
- AppDeployingRepository deployingRepository,
|
|
|
- AppRunningRepository runningRepository,
|
|
|
- AppQuery appQuery,
|
|
|
- NotifyService notifyService,
|
|
|
- DefaultMqttClient mqttClient) {
|
|
|
- this.buildLogRepository = buildLogRepository;
|
|
|
- this.deployLogRepository = deployLogRepository;
|
|
|
- this.deployingRepository = deployingRepository;
|
|
|
- this.runningRepository = runningRepository;
|
|
|
- this.appQuery = appQuery;
|
|
|
- this.notifyService = notifyService;
|
|
|
+ public AppOpsProcessor(AppOpsResultService appOpsResultService, DefaultMqttClient mqttClient) {
|
|
|
+ this.appOpsResultService = appOpsResultService;
|
|
|
this.mqttClient = mqttClient;
|
|
|
}
|
|
|
|
|
|
@@ -71,18 +39,16 @@ public class AppOpsProcessor implements OpsProcessor {
|
|
|
processAppDeployResult(payload);
|
|
|
break;
|
|
|
case appStatusResult:
|
|
|
- processAppStatus(payload);
|
|
|
+ processAppStatusResult(payload);
|
|
|
break;
|
|
|
case appRestartResult:
|
|
|
- processAppStatus(payload);
|
|
|
+ processAppRestartResult(payload);
|
|
|
break;
|
|
|
case appStopResult:
|
|
|
- processAppStatus(payload);
|
|
|
+ processAppStopResult(payload);
|
|
|
break;
|
|
|
case appStartResult:
|
|
|
- processAppStatus(payload);
|
|
|
- break;
|
|
|
- case appLogResult:
|
|
|
+ processAppStartResult(payload);
|
|
|
break;
|
|
|
default:
|
|
|
log.error("AppOps 中没有相应类型...");
|
|
|
@@ -92,76 +58,46 @@ public class AppOpsProcessor implements OpsProcessor {
|
|
|
|
|
|
private void processAppDeployResult(String payload) {
|
|
|
DeployResult deployResult = JsonConverter.jsonToObject(payload, DeployResult.class);
|
|
|
- DeployLog deployLog = DeployLog.from(deployResult);
|
|
|
- deployLogRepository.save(deployLog);
|
|
|
-
|
|
|
- String buildId = deployResult.getBuildLogId();
|
|
|
- // TODO 部署时将 BuildLog 放到缓存中
|
|
|
- Optional<BuildLog> optional = buildLogRepository.findById(buildId);
|
|
|
- if (optional.isPresent()) {
|
|
|
- BuildLog buildLog = optional.get();
|
|
|
- updateDeployingStatus(buildLog, deployResult);
|
|
|
- updateRunningStatus(buildLog, deployResult);
|
|
|
- deployNotify(buildLog, deployResult);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void updateDeployingStatus(BuildLog buildLog, DeployResult deployResult) {
|
|
|
- String appId = buildLog.getAppId();
|
|
|
- String machineId = deployResult.getMachineId();
|
|
|
- AppDeploying deploying = deployingRepository.findByAppIdAndMachineId(appId, machineId);
|
|
|
- if (deploying != null) {
|
|
|
- deploying.update(buildLog, deployResult);
|
|
|
- deployingRepository.save(deploying);
|
|
|
- }
|
|
|
+ appOpsResultService.deployResult(deployResult);
|
|
|
}
|
|
|
|
|
|
- private void updateRunningStatus(BuildLog buildLog, DeployResult deployResult) {
|
|
|
- String appId = buildLog.getAppId();
|
|
|
- String machineId = deployResult.getMachineId();
|
|
|
- AppRunning running = runningRepository.findByAppIdAndMachineId(appId, machineId);
|
|
|
- if (running != null) {
|
|
|
- running.update(buildLog, deployResult.getAppStatus());
|
|
|
- runningRepository.save(running);
|
|
|
+ private void processAppStatusResult(String payload) {
|
|
|
+ AppStatus appStatus = processCallResult(payload);
|
|
|
+ if (appStatus != null) {
|
|
|
+ appOpsResultService.statusResult(appStatus);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void deployNotify(BuildLog buildLog, DeployResult deployResult) {
|
|
|
- AppOrchestration app = appQuery.findByAppId(buildLog.getAppId());
|
|
|
- List<NotifyGroup> notifyGroups = app.getNotifyGroups();
|
|
|
- notifyGroups.forEach(notifyGroup -> {
|
|
|
- String notifyType = notifyGroup.getNotifyType();
|
|
|
- switch (NotifyType.valueOf(notifyType)) {
|
|
|
- case ding:
|
|
|
- DingMsg dingMsg = new DeployNotifyMsg(buildLog, deployResult).dingMsg();
|
|
|
- notifyService.notify(notifyGroup, dingMsg);
|
|
|
- break;
|
|
|
- case email:
|
|
|
- break;
|
|
|
- case sms:
|
|
|
- break;
|
|
|
- default:
|
|
|
- log.error("通知类型不存在...");
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- private void processAppStatus(String payload) {
|
|
|
+ private AppStatus processCallResult(String payload) {
|
|
|
Type type = new TypeToken<CallResult<AppStatus>>(){}.getType();
|
|
|
CallResult<AppStatus> callResult = JsonConverter.jsonToObject(payload, type);
|
|
|
if (callResult.getCode() != 0) {
|
|
|
// TODO 处理调用失败的情况
|
|
|
log.error("调用失败, 原因: {}", callResult.getMsg());
|
|
|
- return;
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ return callResult.getData();
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ private void processAppRestartResult(String payload) {
|
|
|
+ AppStatus appStatus = processCallResult(payload);
|
|
|
+ if (appStatus != null) {
|
|
|
+ appOpsResultService.restartResult(appStatus);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void processAppStopResult(String payload) {
|
|
|
+ AppStatus appStatus = processCallResult(payload);
|
|
|
+ if (appStatus != null) {
|
|
|
+ appOpsResultService.stopResult(appStatus);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- AppStatus appStatus = callResult.getData();
|
|
|
- String appId = appStatus.getAppId();
|
|
|
- String machineId = appStatus.getMachineId();
|
|
|
- AppRunning appRunning = runningRepository.findByAppIdAndMachineId(appId, machineId);
|
|
|
- if (appRunning != null) {
|
|
|
- appRunning.update(appStatus);
|
|
|
- runningRepository.save(appRunning);
|
|
|
+ private void processAppStartResult(String payload) {
|
|
|
+ AppStatus appStatus = processCallResult(payload);
|
|
|
+ if (appStatus != null) {
|
|
|
+ appOpsResultService.startResult(appStatus);
|
|
|
}
|
|
|
}
|
|
|
}
|