@@ -3,14 +3,6 @@ package service
33import  (
44	"bytes" 
55	"fmt" 
6- 	"os/exec" 
7- 	"os/user" 
8- 	"path" 
9- 	"strconv" 
10- 	"strings" 
11- 	"sync" 
12- 	"time" 
13- 
146	"github.com/1Panel-dev/1Panel/backend/app/dto/request" 
157	"github.com/1Panel-dev/1Panel/backend/app/dto/response" 
168	"github.com/1Panel-dev/1Panel/backend/buserr" 
@@ -22,6 +14,11 @@ import (
2214	"github.com/1Panel-dev/1Panel/backend/utils/systemctl" 
2315	"github.com/pkg/errors" 
2416	"gopkg.in/ini.v1" 
17+ 	"os/exec" 
18+ 	"os/user" 
19+ 	"path" 
20+ 	"strconv" 
21+ 	"strings" 
2522)
2623
2724type  HostToolService  struct {}
@@ -34,7 +31,6 @@ type IHostToolService interface {
3431	GetToolLog (req  request.HostToolLogReq ) (string , error )
3532	OperateSupervisorProcess (req  request.SupervisorProcessConfig ) error 
3633	GetSupervisorProcessConfig () ([]response.SupervisorProcessConfig , error )
37- 	LoadProcessStatus () ([]response.ProcessStatus , error )
3834	OperateSupervisorProcessFile (req  request.SupervisorProcessFileReq ) (string , error )
3935}
4036
@@ -377,56 +373,6 @@ func (h *HostToolService) OperateSupervisorProcess(req request.SupervisorProcess
377373	return  nil 
378374}
379375
380- func  (h  * HostToolService ) LoadProcessStatus () ([]response.ProcessStatus , error ) {
381- 	var  res  []response.ProcessStatus 
382- 	statusLines , _  :=  cmd .Exec ("supervisorctl status" )
383- 	if  len (statusLines ) ==  0  {
384- 		return  res , nil 
385- 	}
386- 	lines  :=  strings .Split (statusLines , "\n " )
387- 	for  _ , line  :=  range  lines  {
388- 		fields  :=  strings .Fields (line )
389- 		if  len (fields ) >  1  {
390- 			res  =  append (res , response.ProcessStatus {Name : fields [0 ]})
391- 		}
392- 	}
393- 
394- 	var  wg  sync.WaitGroup 
395- 	wg .Add (len (res ))
396- 	for  i  :=  0 ; i  <  len (res ); i ++  {
397- 		go  func (index  int ) {
398- 			for  t  :=  0 ; t  <  3 ; t ++  {
399- 				status , err  :=  cmd .ExecWithTimeOut (fmt .Sprintf ("supervisorctl status %s" , res [index ].Name ), 2 * time .Second )
400- 				if  err  !=  nil  {
401- 					time .Sleep (2  *  time .Second )
402- 					continue 
403- 				}
404- 				fields  :=  strings .Fields (status )
405- 				if  len (fields ) <  5  {
406- 					time .Sleep (2  *  time .Second )
407- 					continue 
408- 				}
409- 				res [index ].Name  =  fields [0 ]
410- 				res [index ].Status  =  fields [1 ]
411- 				if  fields [1 ] !=  "RUNNING"  {
412- 					res [index ].Msg  =  strings .Join (fields [2 :], " " )
413- 					break 
414- 				}
415- 				res [index ].PID  =  strings .TrimSuffix (fields [3 ], "," )
416- 				res [index ].Uptime  =  fields [5 ]
417- 				break 
418- 			}
419- 			if  len (res [index ].Status ) ==  0  {
420- 				res [index ].Status  =  "FATAL" 
421- 				res [index ].Msg  =  "Timeout for getting process status" 
422- 			}
423- 			wg .Done ()
424- 		}(i )
425- 	}
426- 	wg .Wait ()
427- 	return  res , nil 
428- }
429- 
430376func  (h  * HostToolService ) GetSupervisorProcessConfig () ([]response.SupervisorProcessConfig , error ) {
431377	var  (
432378		result  []response.SupervisorProcessConfig 
@@ -463,6 +409,7 @@ func (h *HostToolService) GetSupervisorProcessConfig() ([]response.SupervisorPro
463409			if  numprocs , _  :=  section .GetKey ("numprocs" ); numprocs  !=  nil  {
464410				config .Numprocs  =  numprocs .Value ()
465411			}
412+ 			_  =  getProcessStatus (& config )
466413			result  =  append (result , config )
467414		}
468415	}
@@ -590,3 +537,29 @@ func getProcessName(name, numprocs string) []string {
590537	}
591538	return  processNames 
592539}
540+ 
541+ func  getProcessStatus (config  * response.SupervisorProcessConfig ) error  {
542+ 	var  (
543+ 		processNames  =  []string {"status" }
544+ 	)
545+ 	processNames  =  append (processNames , getProcessName (config .Name , config .Numprocs )... )
546+ 	output , _  :=  exec .Command ("supervisorctl" , processNames ... ).Output ()
547+ 	lines  :=  strings .Split (string (output ), "\n " )
548+ 	for  _ , line  :=  range  lines  {
549+ 		fields  :=  strings .Fields (line )
550+ 		if  len (fields ) >=  5  {
551+ 			status  :=  response.ProcessStatus {
552+ 				Name :   fields [0 ],
553+ 				Status : fields [1 ],
554+ 			}
555+ 			if  fields [1 ] ==  "RUNNING"  {
556+ 				status .PID  =  strings .TrimSuffix (fields [3 ], "," )
557+ 				status .Uptime  =  fields [5 ]
558+ 			} else  {
559+ 				status .Msg  =  strings .Join (fields [2 :], " " )
560+ 			}
561+ 			config .Status  =  append (config .Status , status )
562+ 		}
563+ 	}
564+ 	return  nil 
565+ }
0 commit comments