|
|
@@ -1,6 +1,9 @@
|
|
|
-package cn.reghao.autodop.dmaster.mqttsub.processor.app;
|
|
|
+package cn.reghao.autodop.dmaster.mqttsub.processor;
|
|
|
|
|
|
+import cn.reghao.autodop.common.dagent.app.api.data.AppStatus;
|
|
|
import cn.reghao.autodop.common.dagent.app.api.data.DeployResult;
|
|
|
+import cn.reghao.autodop.common.message.CallResult;
|
|
|
+import cn.reghao.autodop.common.message.ops.AppOps;
|
|
|
import cn.reghao.autodop.common.utils.serializer.JsonConverter;
|
|
|
import cn.reghao.autodop.dmaster.app.db.query.AppQuery;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.AppDeploying;
|
|
|
@@ -14,18 +17,26 @@ 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.common.message.ops.OpsProcessor;
|
|
|
import cn.reghao.autodop.dmaster.utils.notifier.DingMsg;
|
|
|
import cn.reghao.autodop.dmaster.utils.notifier.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 相关的消息
|
|
|
+ *
|
|
|
* @author reghao
|
|
|
- * @date 2021-06-09 18:48:48
|
|
|
+ * @date 2020-12-30 10:26:47
|
|
|
*/
|
|
|
-public class AppDeployResultProcessor implements Runnable {
|
|
|
- private String payload;
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class AppOpsProcessor implements OpsProcessor {
|
|
|
private BuildLogRepository buildLogRepository;
|
|
|
private DeployLogRepository deployLogRepository;
|
|
|
private AppDeployingRepository deployingRepository;
|
|
|
@@ -33,14 +44,12 @@ public class AppDeployResultProcessor implements Runnable {
|
|
|
private AppQuery appQuery;
|
|
|
private NotifyService notifyService;
|
|
|
|
|
|
- public AppDeployResultProcessor(String payload,
|
|
|
- BuildLogRepository buildLogRepository,
|
|
|
- DeployLogRepository deployLogRepository,
|
|
|
- AppDeployingRepository deployingRepository,
|
|
|
- AppRunningRepository runningRepository,
|
|
|
- AppQuery appQuery,
|
|
|
- NotifyService notifyService) {
|
|
|
- this.payload = payload;
|
|
|
+ public AppOpsProcessor(BuildLogRepository buildLogRepository,
|
|
|
+ DeployLogRepository deployLogRepository,
|
|
|
+ AppDeployingRepository deployingRepository,
|
|
|
+ AppRunningRepository runningRepository,
|
|
|
+ AppQuery appQuery,
|
|
|
+ NotifyService notifyService) {
|
|
|
this.buildLogRepository = buildLogRepository;
|
|
|
this.deployLogRepository = deployLogRepository;
|
|
|
this.deployingRepository = deployingRepository;
|
|
|
@@ -50,7 +59,32 @@ public class AppDeployResultProcessor implements Runnable {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void run() {
|
|
|
+ public void process(String ops, String payload) {
|
|
|
+ switch (AppOps.valueOf(ops)) {
|
|
|
+ case appDeployResult:
|
|
|
+ processAppDeployResult(payload);
|
|
|
+ break;
|
|
|
+ case appStatusResult:
|
|
|
+ processAppStatus(payload);
|
|
|
+ break;
|
|
|
+ case appRestartResult:
|
|
|
+ processAppStatus(payload);
|
|
|
+ break;
|
|
|
+ case appStopResult:
|
|
|
+ processAppStatus(payload);
|
|
|
+ break;
|
|
|
+ case appStartResult:
|
|
|
+ processAppStatus(payload);
|
|
|
+ break;
|
|
|
+ case appLogResult:
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ log.error("AppOps 中没有相应类型...");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void processAppDeployResult(String payload) {
|
|
|
DeployResult deployResult = JsonConverter.jsonToObject(payload, DeployResult.class);
|
|
|
DeployLog deployLog = DeployLog.from(deployResult);
|
|
|
deployLogRepository.save(deployLog);
|
|
|
@@ -92,4 +126,23 @@ public class AppDeployResultProcessor implements Runnable {
|
|
|
List<NotifyReceiver> notifyReceivers = app.getNotifyReceivers();
|
|
|
notifyReceivers.forEach(notifyReceiver -> notifyService.notify(notifyReceiver, dingMsg));
|
|
|
}
|
|
|
+
|
|
|
+ private void processAppStatus(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;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|