fix: 优化supervisor管理器

This commit is contained in:
耗子
2026-04-27 19:26:46 +08:00
parent d565d3bbb8
commit b3b29d79c5
2 changed files with 71 additions and 38 deletions
+61 -36
View File
@@ -114,31 +114,35 @@ func (s *App) UpdateConfig(w http.ResponseWriter, r *http.Request) {
// Processes 进程列表
func (s *App) Processes(w http.ResponseWriter, r *http.Request) {
out, err := shell.Execf(`supervisorctl status | awk '{print $1}'`)
if err != nil {
out, err := shell.Execf(`supervisorctl status`)
if err != nil && out == "" {
service.Error(w, http.StatusInternalServerError, "%v", err)
return
}
var processes []Process
for line := range strings.SplitSeq(out, "\n") {
if len(line) == 0 {
fields := strings.Fields(line)
if len(fields) < 2 {
continue
}
var p Process
p.Name = line
if status, err := shell.Execf(`supervisorctl status '%s' | awk '{print $2}'`, line); err == nil {
p.Status = status
p := Process{
Name: fields[0],
Status: fields[1],
Pid: "-",
Uptime: "-",
}
// RUNNING 行格式:name RUNNING pid 1234, uptime 1:23:45 (超过 1 天为 "1 day, 1:23:45"
if p.Status == "RUNNING" {
pid, _ := shell.Execf(`supervisorctl status '%s' | awk '{print $4}'`, line)
p.Pid = strings.ReplaceAll(pid, ",", "")
uptime, _ := shell.Execf(`supervisorctl status '%s' | awk '{print $6}'`, line)
p.Uptime = uptime
} else {
p.Pid = "-"
p.Uptime = "-"
if _, rest, ok := strings.Cut(line, "pid "); ok {
if pid, _, ok := strings.Cut(rest, ","); ok {
p.Pid = pid
}
}
if _, uptime, ok := strings.Cut(line, "uptime "); ok {
p.Uptime = uptime
}
}
processes = append(processes, p)
}
@@ -159,7 +163,7 @@ func (s *App) StartProcess(w http.ResponseWriter, r *http.Request) {
return
}
if out, err := shell.Execf(`supervisorctl start %s`, req.Process); err != nil {
if out, err := shell.Execf(`supervisorctl start '%s'`, req.Process); err != nil {
service.Error(w, http.StatusInternalServerError, "%v %s", err, out)
return
}
@@ -175,7 +179,7 @@ func (s *App) StopProcess(w http.ResponseWriter, r *http.Request) {
return
}
if out, err := shell.Execf(`supervisorctl stop %s`, req.Process); err != nil {
if out, err := shell.Execf(`supervisorctl stop '%s'`, req.Process); err != nil {
service.Error(w, http.StatusInternalServerError, "%v %s", err, out)
return
}
@@ -191,7 +195,7 @@ func (s *App) RestartProcess(w http.ResponseWriter, r *http.Request) {
return
}
if out, err := shell.Execf(`supervisorctl restart %s`, req.Process); err != nil {
if out, err := shell.Execf(`supervisorctl restart '%s'`, req.Process); err != nil {
service.Error(w, http.StatusInternalServerError, "%v %s", err, out)
return
}
@@ -207,11 +211,12 @@ func (s *App) ProcessLog(w http.ResponseWriter, r *http.Request) {
return
}
name := programName(req.Process)
var logPath string
if os.IsRHEL() {
logPath, err = shell.Execf(`cat '/etc/supervisord.d/%s.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`, req.Process)
logPath, err = shell.Execf(`cat '/etc/supervisord.d/%s.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`, name)
} else {
logPath, err = shell.Execf(`cat '/etc/supervisor/conf.d/%s.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`, req.Process)
logPath, err = shell.Execf(`cat '/etc/supervisor/conf.d/%s.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`, name)
}
if err != nil {
@@ -230,11 +235,12 @@ func (s *App) ClearProcessLog(w http.ResponseWriter, r *http.Request) {
return
}
name := programName(req.Process)
var logPath string
if os.IsRHEL() {
logPath, err = shell.Execf(`cat '/etc/supervisord.d/%s.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`, req.Process)
logPath, err = shell.Execf(`cat '/etc/supervisord.d/%s.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`, name)
} else {
logPath, err = shell.Execf(`cat '/etc/supervisor/conf.d/%s.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`, req.Process)
logPath, err = shell.Execf(`cat '/etc/supervisor/conf.d/%s.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`, name)
}
if err != nil {
@@ -258,11 +264,12 @@ func (s *App) ProcessConfig(w http.ResponseWriter, r *http.Request) {
return
}
name := programName(req.Process)
var config string
if os.IsRHEL() {
config, err = io.Read(`/etc/supervisord.d/` + req.Process + `.conf`)
config, err = io.Read(`/etc/supervisord.d/` + name + `.conf`)
} else {
config, err = io.Read(`/etc/supervisor/conf.d/` + req.Process + `.conf`)
config, err = io.Read(`/etc/supervisor/conf.d/` + name + `.conf`)
}
if err != nil {
@@ -281,10 +288,11 @@ func (s *App) UpdateProcessConfig(w http.ResponseWriter, r *http.Request) {
return
}
name := programName(req.Process)
if os.IsRHEL() {
err = io.Write(`/etc/supervisord.d/`+req.Process+`.conf`, req.Config, 0644)
err = io.Write(`/etc/supervisord.d/`+name+`.conf`, req.Config, 0644)
} else {
err = io.Write(`/etc/supervisor/conf.d/`+req.Process+`.conf`, req.Config, 0644)
err = io.Write(`/etc/supervisor/conf.d/`+name+`.conf`, req.Config, 0644)
}
if err != nil {
@@ -294,7 +302,7 @@ func (s *App) UpdateProcessConfig(w http.ResponseWriter, r *http.Request) {
_, _ = shell.Execf(`supervisorctl reread`)
_, _ = shell.Execf(`supervisorctl update`)
_, _ = shell.Execf(`supervisorctl restart '%s'`, req.Process)
_, _ = shell.Execf(`supervisorctl restart '%s:'`, name)
service.Success(w, nil)
}
@@ -307,9 +315,14 @@ func (s *App) CreateProcess(w http.ResponseWriter, r *http.Request) {
return
}
processName := `%(program_name)s`
if req.Num > 1 {
processName = `%(program_name)s_%(process_num)02d`
}
config := `[program:` + req.Name + `]
command=` + req.Command + `
process_name=%(program_name)s
process_name=` + processName + `
directory=` + req.Path + `
autostart=true
autorestart=true
@@ -320,20 +333,26 @@ stdout_logfile=/var/log/supervisor/` + req.Name + `.log
stdout_logfile_maxbytes=2MB
`
var confPath string
if os.IsRHEL() {
err = io.Write(`/etc/supervisord.d/`+req.Name+`.conf`, config, 0644)
confPath = `/etc/supervisord.d/` + req.Name + `.conf`
} else {
err = io.Write(`/etc/supervisor/conf.d/`+req.Name+`.conf`, config, 0644)
confPath = `/etc/supervisor/conf.d/` + req.Name + `.conf`
}
if err != nil {
if io.Exists(confPath) {
service.Error(w, http.StatusConflict, s.t.Get("process %s already exists", req.Name))
return
}
if err = io.Write(confPath, config, 0644); err != nil {
service.Error(w, http.StatusUnprocessableEntity, "%v", err)
return
}
_, _ = shell.Execf(`supervisorctl reread`)
_, _ = shell.Execf(`supervisorctl update`)
_, _ = shell.Execf(`supervisorctl start '%s'`, req.Name)
_, _ = shell.Execf(`supervisorctl start '%s:'`, req.Name)
service.Success(w, nil)
}
@@ -346,29 +365,30 @@ func (s *App) DeleteProcess(w http.ResponseWriter, r *http.Request) {
return
}
if out, err := shell.Execf(`supervisorctl stop '%s'`, req.Process); err != nil {
name := programName(req.Process)
if out, err := shell.Execf(`supervisorctl stop '%s:'`, name); err != nil {
service.Error(w, http.StatusInternalServerError, "%v %s", err, out)
return
}
var logPath string
if os.IsRHEL() {
logPath, err = shell.Execf(`cat '/etc/supervisord.d/%s.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`, req.Process)
logPath, err = shell.Execf(`cat '/etc/supervisord.d/%s.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`, name)
if err != nil {
service.Error(w, http.StatusInternalServerError, s.t.Get("failed to get log path for process %s: %v", req.Process, err))
return
}
if err = io.Remove(`/etc/supervisord.d/` + req.Process + `.conf`); err != nil {
if err = io.Remove(`/etc/supervisord.d/` + name + `.conf`); err != nil {
service.Error(w, http.StatusInternalServerError, "%v", err)
return
}
} else {
logPath, err = shell.Execf(`cat '/etc/supervisor/conf.d/%s.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`, req.Process)
logPath, err = shell.Execf(`cat '/etc/supervisor/conf.d/%s.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`, name)
if err != nil {
service.Error(w, http.StatusInternalServerError, s.t.Get("failed to get log path for process %s: %v", req.Process, err))
return
}
if err = io.Remove(`/etc/supervisor/conf.d/` + req.Process + `.conf`); err != nil {
if err = io.Remove(`/etc/supervisor/conf.d/` + name + `.conf`); err != nil {
service.Error(w, http.StatusInternalServerError, "%v", err)
return
}
@@ -383,3 +403,8 @@ func (s *App) DeleteProcess(w http.ResponseWriter, r *http.Request) {
service.Success(w, nil)
}
func programName(process string) string {
name, _, _ := strings.Cut(process, ":")
return name
}
+10 -2
View File
@@ -299,6 +299,8 @@ const handleSaveProcessConfig = () => {
useRequest(
supervisor.saveProcessConfig(editProcessModel.value.process, editProcessModel.value.config)
).onSuccess(() => {
editProcessModal.value = false
refresh()
window.$message.success($gettext('Saved successfully'))
})
}
@@ -435,8 +437,14 @@ onUnmounted(() => {
size="huge"
:bordered="false"
:segmented="false"
@close="handleSaveProcessConfig"
>
<common-editor v-model:value="editProcessModel.config" height="60vh" />
<n-flex vertical>
<common-editor v-model:value="editProcessModel.config" height="60vh" />
<n-flex>
<n-button type="primary" @click="handleSaveProcessConfig">
{{ $gettext('Save') }}
</n-button>
</n-flex>
</n-flex>
</n-modal>
</template>