From ed3d5870468b13b0920a3eb3656586f52e8f5355 Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Tue, 5 Sep 2023 16:12:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E5=AE=88=E6=8A=A4?= =?UTF-8?q?=E8=BF=9B=E7=A8=8B=E5=81=9C=E6=AD=A2=E4=B9=8B=E5=90=8E=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E8=AF=BB=E5=8F=96=E5=BC=82=E5=B8=B8=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#2186)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs https://github.com/1Panel-dev/1Panel/issues/2167 --- backend/app/api/v1/host_tool.go | 15 --- backend/app/service/host_tool.go | 91 +++++++------------ backend/router/ro_host.go | 1 - frontend/src/api/modules/host-tool.ts | 2 +- .../src/views/host/tool/supervisor/index.vue | 48 ++++++---- 5 files changed, 63 insertions(+), 94 deletions(-) diff --git a/backend/app/api/v1/host_tool.go b/backend/app/api/v1/host_tool.go index 41ba6e883..6e55b70e2 100644 --- a/backend/app/api/v1/host_tool.go +++ b/backend/app/api/v1/host_tool.go @@ -189,21 +189,6 @@ func (b *BaseApi) GetProcess(c *gin.Context) { helper.SuccessWithData(c, configs) } -// @Tags Host tool -// @Summary Load Supervisor process status -// @Description 获取 Supervisor 进程状态 -// @Success 200 {array} response.ProcessStatus -// @Security ApiKeyAuth -// @Router /host/tool/supervisor/process/load [post] -func (b *BaseApi) LoadProcessStatus(c *gin.Context) { - datas, err := hostToolService.LoadProcessStatus() - if err != nil { - helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) - return - } - helper.SuccessWithData(c, datas) -} - // @Tags Host tool // @Summary Get Supervisor process config // @Description 操作 Supervisor 进程文件 diff --git a/backend/app/service/host_tool.go b/backend/app/service/host_tool.go index d3b6e48ed..91198d7c8 100644 --- a/backend/app/service/host_tool.go +++ b/backend/app/service/host_tool.go @@ -3,14 +3,6 @@ package service import ( "bytes" "fmt" - "os/exec" - "os/user" - "path" - "strconv" - "strings" - "sync" - "time" - "github.com/1Panel-dev/1Panel/backend/app/dto/request" "github.com/1Panel-dev/1Panel/backend/app/dto/response" "github.com/1Panel-dev/1Panel/backend/buserr" @@ -22,6 +14,11 @@ import ( "github.com/1Panel-dev/1Panel/backend/utils/systemctl" "github.com/pkg/errors" "gopkg.in/ini.v1" + "os/exec" + "os/user" + "path" + "strconv" + "strings" ) type HostToolService struct{} @@ -34,7 +31,6 @@ type IHostToolService interface { GetToolLog(req request.HostToolLogReq) (string, error) OperateSupervisorProcess(req request.SupervisorProcessConfig) error GetSupervisorProcessConfig() ([]response.SupervisorProcessConfig, error) - LoadProcessStatus() ([]response.ProcessStatus, error) OperateSupervisorProcessFile(req request.SupervisorProcessFileReq) (string, error) } @@ -377,56 +373,6 @@ func (h *HostToolService) OperateSupervisorProcess(req request.SupervisorProcess return nil } -func (h *HostToolService) LoadProcessStatus() ([]response.ProcessStatus, error) { - var res []response.ProcessStatus - statusLines, _ := cmd.Exec("supervisorctl status") - if len(statusLines) == 0 { - return res, nil - } - lines := strings.Split(statusLines, "\n") - for _, line := range lines { - fields := strings.Fields(line) - if len(fields) > 1 { - res = append(res, response.ProcessStatus{Name: fields[0]}) - } - } - - var wg sync.WaitGroup - wg.Add(len(res)) - for i := 0; i < len(res); i++ { - go func(index int) { - for t := 0; t < 3; t++ { - status, err := cmd.ExecWithTimeOut(fmt.Sprintf("supervisorctl status %s", res[index].Name), 2*time.Second) - if err != nil { - time.Sleep(2 * time.Second) - continue - } - fields := strings.Fields(status) - if len(fields) < 5 { - time.Sleep(2 * time.Second) - continue - } - res[index].Name = fields[0] - res[index].Status = fields[1] - if fields[1] != "RUNNING" { - res[index].Msg = strings.Join(fields[2:], " ") - break - } - res[index].PID = strings.TrimSuffix(fields[3], ",") - res[index].Uptime = fields[5] - break - } - if len(res[index].Status) == 0 { - res[index].Status = "FATAL" - res[index].Msg = "Timeout for getting process status" - } - wg.Done() - }(i) - } - wg.Wait() - return res, nil -} - func (h *HostToolService) GetSupervisorProcessConfig() ([]response.SupervisorProcessConfig, error) { var ( result []response.SupervisorProcessConfig @@ -463,6 +409,7 @@ func (h *HostToolService) GetSupervisorProcessConfig() ([]response.SupervisorPro if numprocs, _ := section.GetKey("numprocs"); numprocs != nil { config.Numprocs = numprocs.Value() } + _ = getProcessStatus(&config) result = append(result, config) } } @@ -590,3 +537,29 @@ func getProcessName(name, numprocs string) []string { } return processNames } + +func getProcessStatus(config *response.SupervisorProcessConfig) error { + var ( + processNames = []string{"status"} + ) + processNames = append(processNames, getProcessName(config.Name, config.Numprocs)...) + output, _ := exec.Command("supervisorctl", processNames...).Output() + lines := strings.Split(string(output), "\n") + for _, line := range lines { + fields := strings.Fields(line) + if len(fields) >= 5 { + status := response.ProcessStatus{ + Name: fields[0], + Status: fields[1], + } + if fields[1] == "RUNNING" { + status.PID = strings.TrimSuffix(fields[3], ",") + status.Uptime = fields[5] + } else { + status.Msg = strings.Join(fields[2:], " ") + } + config.Status = append(config.Status, status) + } + } + return nil +} diff --git a/backend/router/ro_host.go b/backend/router/ro_host.go index f01ae611f..b4217b155 100644 --- a/backend/router/ro_host.go +++ b/backend/router/ro_host.go @@ -55,7 +55,6 @@ func (s *HostRouter) InitHostRouter(Router *gin.RouterGroup) { hostRouter.POST("/tool/operate", baseApi.OperateTool) hostRouter.POST("/tool/config", baseApi.OperateToolConfig) hostRouter.POST("/tool/log", baseApi.GetToolLog) - hostRouter.POST("/tool/supervisor/process/load", baseApi.LoadProcessStatus) hostRouter.POST("/tool/supervisor/process", baseApi.OperateProcess) hostRouter.GET("/tool/supervisor/process", baseApi.GetProcess) hostRouter.POST("/tool/supervisor/process/file", baseApi.GetProcessFile) diff --git a/frontend/src/api/modules/host-tool.ts b/frontend/src/api/modules/host-tool.ts index c8ddd3a62..c365704c0 100644 --- a/frontend/src/api/modules/host-tool.ts +++ b/frontend/src/api/modules/host-tool.ts @@ -34,7 +34,7 @@ export const LoadProcessStatus = () => { }; export const GetSupervisorProcess = () => { - return http.get(`/hosts/tool/supervisor/process`); + return http.get(`/hosts/tool/supervisor/process`); }; export const OperateSupervisorProcessFile = (req: HostTool.ProcessFileReq) => { diff --git a/frontend/src/views/host/tool/supervisor/index.vue b/frontend/src/views/host/tool/supervisor/index.vue index 9d88e3a85..c285dbc77 100644 --- a/frontend/src/views/host/tool/supervisor/index.vue +++ b/frontend/src/views/host/tool/supervisor/index.vue @@ -157,7 +157,7 @@ import ConfigSuperVisor from './config/index.vue'; import { computed, onMounted } from 'vue'; import Create from './create/index.vue'; import File from './file/index.vue'; -import { GetSupervisorProcess, LoadProcessStatus, OperateSupervisorProcess } from '@/api/modules/host-tool'; +import { GetSupervisorProcess, OperateSupervisorProcess } from '@/api/modules/host-tool'; import { GlobalStore } from '@/store'; import i18n from '@/lang'; import { HostTool } from '@/api/interface/host-tool'; @@ -216,34 +216,46 @@ const search = async () => { return; } loading.value = true; - loadStatus(); + let needLoadStatus = false; try { const res = await GetSupervisorProcess(); data.value = res.data; + for (const process of data.value) { + if (process.status && process.status.length > 0) { + process.hasLoad = true; + } else { + process.hasLoad = false; + needLoadStatus = true; + } + } + if (needLoadStatus) { + setTimeout(loadStatus, 1000); + } } catch (error) {} loading.value = false; }; const loadStatus = async () => { - 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); + let needLoadStatus = false; + try { + const res = await GetSupervisorProcess(); + const stats = res.data || []; + for (const process of data.value) { + for (const item of stats) { + if (process.name === item.name) { + if (item.status && item.status.length > 0) { + process.status = item.status; + process.hasLoad = true; + } else { + needLoadStatus = true; } } - 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; - } - }); + } + if (needLoadStatus) { + setTimeout(loadStatus, 2000); + } + } catch (error) {} }; const mobile = computed(() => {