From e67d9055aa8920152be48a28e3f86cecc0fe4daa Mon Sep 17 00:00:00 2001
From: ssongliu <73214554+ssongliu@users.noreply.github.com>
Date: Thu, 17 Aug 2023 14:46:09 +0800
Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E8=BF=9B=E7=A8=8B?=
 =?UTF-8?q?=E7=8A=B6=E6=80=81=E4=B8=80=E7=9B=B4=E5=8A=A0=E8=BD=BD=E4=B8=AD?=
 =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=20(#1983)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Refs #1968
---
 backend/app/api/v1/host_tool.go               |  7 +++-
 backend/app/service/host_tool.go              | 10 +++---
 frontend/src/lang/modules/en.ts               |  1 +
 frontend/src/lang/modules/tw.ts               |  1 +
 frontend/src/lang/modules/zh.ts               |  1 +
 .../src/views/host/tool/supervisor/index.vue  | 35 +++++++++++--------
 6 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/backend/app/api/v1/host_tool.go b/backend/app/api/v1/host_tool.go
index d7bed8048..41ba6e883 100644
--- a/backend/app/api/v1/host_tool.go
+++ b/backend/app/api/v1/host_tool.go
@@ -196,7 +196,12 @@ func (b *BaseApi) GetProcess(c *gin.Context) {
 // @Security ApiKeyAuth
 // @Router /host/tool/supervisor/process/load [post]
 func (b *BaseApi) LoadProcessStatus(c *gin.Context) {
-	helper.SuccessWithData(c, hostToolService.LoadProcessStatus())
+	datas, err := hostToolService.LoadProcessStatus()
+	if err != nil {
+		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
+		return
+	}
+	helper.SuccessWithData(c, datas)
 }
 
 // @Tags Host tool
diff --git a/backend/app/service/host_tool.go b/backend/app/service/host_tool.go
index e7f57f56f..d8d62e0c9 100644
--- a/backend/app/service/host_tool.go
+++ b/backend/app/service/host_tool.go
@@ -34,7 +34,7 @@ type IHostToolService interface {
 	GetToolLog(req request.HostToolLogReq) (string, error)
 	OperateSupervisorProcess(req request.SupervisorProcessConfig) error
 	GetSupervisorProcessConfig() ([]response.SupervisorProcessConfig, error)
-	LoadProcessStatus() []response.ProcessStatus
+	LoadProcessStatus() ([]response.ProcessStatus, error)
 	OperateSupervisorProcessFile(req request.SupervisorProcessFileReq) (string, error)
 }
 
@@ -377,11 +377,11 @@ func (h *HostToolService) OperateSupervisorProcess(req request.SupervisorProcess
 	return nil
 }
 
-func (h *HostToolService) LoadProcessStatus() []response.ProcessStatus {
+func (h *HostToolService) LoadProcessStatus() ([]response.ProcessStatus, error) {
 	var datas []response.ProcessStatus
-	statuLines, err := cmd.Exec("supervisorctl status")
+	statuLines, err := cmd.Exec("supervisorct status")
 	if err != nil {
-		return datas
+		return datas, fmt.Errorf("exec `supervisorctl status` failed, err: %v", statuLines)
 	}
 	lines := strings.Split(string(statuLines), "\n")
 	for _, line := range lines {
@@ -424,7 +424,7 @@ func (h *HostToolService) LoadProcessStatus() []response.ProcessStatus {
 		}(i)
 	}
 	wg.Wait()
-	return datas
+	return datas, nil
 }
 
 func (h *HostToolService) GetSupervisorProcessConfig() ([]response.SupervisorProcessConfig, error) {
diff --git a/frontend/src/lang/modules/en.ts b/frontend/src/lang/modules/en.ts
index 5a67c3165..b921ad3f6 100644
--- a/frontend/src/lang/modules/en.ts
+++ b/frontend/src/lang/modules/en.ts
@@ -1666,6 +1666,7 @@ const message = {
     },
     tool: {
         supervisor: {
+            loadStatusErr: 'Failed to retrieve process status, please check the status of the supervisor service.',
             notSupport: 'Supervisor is not detected, please refer to the official document for installation',
             list: 'Daemon process',
             config: 'Supervisor configuration',
diff --git a/frontend/src/lang/modules/tw.ts b/frontend/src/lang/modules/tw.ts
index 54d46fd8b..3ae0e6691 100644
--- a/frontend/src/lang/modules/tw.ts
+++ b/frontend/src/lang/modules/tw.ts
@@ -1580,6 +1580,7 @@ const message = {
     },
     tool: {
         supervisor: {
+            loadStatusErr: '獲取進程狀態失敗,請檢查 supervisor 服務狀態',
             notSupport: '未檢測到 Supervisor,請參考官方文檔進行安裝',
             list: '守護進程',
             config: 'Supervisor 配置',
diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts
index 01c40d189..b5f1f6be7 100644
--- a/frontend/src/lang/modules/zh.ts
+++ b/frontend/src/lang/modules/zh.ts
@@ -1582,6 +1582,7 @@ const message = {
     },
     tool: {
         supervisor: {
+            loadStatusErr: '获取进程状态失败,请检查 supervisor 服务状态',
             notSupport: '未检测到 Supervisor,请参考官方文档进行安装',
             list: '守护进程',
             config: 'Supervisor 配置',
diff --git a/frontend/src/views/host/tool/supervisor/index.vue b/frontend/src/views/host/tool/supervisor/index.vue
index afd7be634..65996c978 100644
--- a/frontend/src/views/host/tool/supervisor/index.vue
+++ b/frontend/src/views/host/tool/supervisor/index.vue
@@ -221,22 +221,27 @@ const search = async () => {
 };
 
 const loadStatus = async () => {
-    const res = await LoadProcessStatus();
-    let stats = res.data || [];
-    if (stats.length === 0) {
-        return;
-    }
-    for (const process of data.value) {
-        process.status = [];
-        for (const item of stats) {
-            if (process.name === item.name.split(':')[0]) {
-                process.status.push(item);
+    await LoadProcessStatus()
+        .then((res) => {
+            let stats = res.data || [];
+            for (const process of data.value) {
+                process.status = [];
+                for (const item of stats) {
+                    if (process.name === item.name.split(':')[0]) {
+                        process.status.push(item);
+                    }
+                }
+                if (process.status.length !== 0) {
+                    process.hasLoad = true;
+                }
             }
-        }
-        if (process.status.length !== 0) {
-            process.hasLoad = true;
-        }
-    }
+        })
+        .catch(() => {
+            for (const process of data.value) {
+                process.status = [{ name: '-', status: 'FATAL', msg: i18n.global.t('tool.supervisor.loadStatusErr') }];
+                process.hasLoad = true;
+            }
+        });
 };
 
 const mobile = computed(() => {