Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions backend/app/api/v1/host_tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 进程文件
Expand Down
91 changes: 32 additions & 59 deletions backend/app/service/host_tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{}
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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
}
1 change: 0 additions & 1 deletion backend/router/ro_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/api/modules/host-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const LoadProcessStatus = () => {
};

export const GetSupervisorProcess = () => {
return http.get<HostTool.SupersivorProcess>(`/hosts/tool/supervisor/process`);
return http.get<HostTool.SupersivorProcess[]>(`/hosts/tool/supervisor/process`);
};

export const OperateSupervisorProcessFile = (req: HostTool.ProcessFileReq) => {
Expand Down
48 changes: 30 additions & 18 deletions frontend/src/views/host/tool/supervisor/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(() => {
Expand Down