중지할 프로세스를 동시에 처리

This commit is contained in:
2023-12-01 19:34:35 +09:00
parent 75992472f3
commit 6e0bd71c80
2 changed files with 31 additions and 53 deletions

View File

@ -22,6 +22,7 @@ import (
"time" "time"
"unsafe" "unsafe"
"repositories.action2quare.com/ayo/gocommon"
"repositories.action2quare.com/ayo/gocommon/logger" "repositories.action2quare.com/ayo/gocommon/logger"
"repositories.action2quare.com/ayo/houston/shared" "repositories.action2quare.com/ayo/houston/shared"
"repositories.action2quare.com/ayo/houston/shared/protos" "repositories.action2quare.com/ayo/houston/shared/protos"
@ -431,22 +432,14 @@ func NewClient(standalone bool) (HoustonClient, error) {
id64, _ := strconv.ParseInt(idstr, 10, 0) id64, _ := strconv.ParseInt(idstr, 10, 0)
id := int32(id64) id := int32(id64)
var killing *procmeta hc.childProcs = gocommon.ShrinkSlice(hc.childProcs, func(e *procmeta) bool {
var remains []*procmeta if e.id == id {
for _, meta := range hc.childProcs { e.cmd.Wait()
if meta.id == id { e.cmd.Process.Release()
killing = meta return true
} else {
remains = append(remains, meta)
} }
} return false
})
if killing != nil {
killing.cmd.Wait()
killing.cmd.Process.Release()
}
hc.childProcs = remains
op.Refresh(context.Background(), hc.makeOperationQueryRequest()) op.Refresh(context.Background(), hc.makeOperationQueryRequest())
} }
@ -523,7 +516,7 @@ func (hc *houstonClient) Start() {
if client != nil { if client != nil {
err := hc.checkOperation(client) err := hc.checkOperation(client)
if err != nil { if err != nil {
logger.Println("hc.checkUpdate failed :", err) logger.Println("grpc.DialContext hc.checkOperation failed :", err)
client = nil client = nil
} }

View File

@ -422,8 +422,6 @@ func (hc *houstonClient) startChildProcess(req *shared.StartProcessRequest, op p
return nil return nil
} }
var errNoRunningProcess = errors.New("no running processed")
func (hc *houstonClient) stopChildProcess(req *shared.StopProcessRequest, op protos.OperationClient) error { func (hc *houstonClient) stopChildProcess(req *shared.StopProcessRequest, op protos.OperationClient) error {
if req.Version == "latest" { if req.Version == "latest" {
// 최신 버전을 찾음 // 최신 버전을 찾음
@ -435,8 +433,23 @@ func (hc *houstonClient) stopChildProcess(req *shared.StopProcessRequest, op pro
req.Version = latest req.Version = latest
} }
var remains []*procmeta killer := func(proc *procmeta) {
var killing []*procmeta proc.setState(protos.ProcessState_Stopping)
if err := proc.cmd.Process.Signal(syscall.SIGTERM); err != nil {
proc.cmd.Process.Signal(os.Kill)
}
go func() {
proc.cmd.Wait()
hc.operationChan <- &protos.OperationQueryResponse{
Operation: string(shared.Exception),
Args: map[string]string{
"id": fmt.Sprintf("%d", proc.id),
},
}
}()
}
for _, proc := range hc.childProcs { for _, proc := range hc.childProcs {
if !proc.isState(protos.ProcessState_Running) { if !proc.isState(protos.ProcessState_Running) {
continue continue
@ -445,50 +458,22 @@ func (hc *houstonClient) stopChildProcess(req *shared.StopProcessRequest, op pro
if req.Pid != 0 { if req.Pid != 0 {
if req.Pid == int32(proc.cmd.Process.Pid) { if req.Pid == int32(proc.cmd.Process.Pid) {
// 해당 pid만 제거 // 해당 pid만 제거
killing = append(killing, proc) killer(proc)
} else {
remains = append(remains, proc)
} }
} else if proc.name == req.Name { } else if proc.name == req.Name {
if len(req.Version) == 0 { if len(req.Version) == 0 {
// program 다 정지 // program 다 정지
killing = append(killing, proc) killer(proc)
} else if req.Version == proc.version { } else if req.Version == proc.version {
// program의 특정 버전만 정지 // program의 특정 버전만 정지
killing = append(killing, proc) killer(proc)
} else {
// 해당 사항 없음
remains = append(remains, proc)
} }
} else {
// 해당 사항 없음
remains = append(remains, proc)
} }
} }
if len(killing) > 0 { op.Refresh(context.Background(), hc.makeOperationQueryRequest())
for _, proc := range killing {
proc.setState(protos.ProcessState_Stopping)
if err := proc.cmd.Process.Signal(syscall.SIGTERM); err != nil {
proc.cmd.Process.Signal(os.Kill)
}
}
op.Refresh(context.Background(), hc.makeOperationQueryRequest()) return nil
for _, proc := range killing {
proc.cmd.Wait()
proc.cmd.Process.Release()
}
hc.childProcs = remains
op.Refresh(context.Background(), hc.makeOperationQueryRequest())
return nil
}
return errNoRunningProcess
} }
func (hc *houstonClient) restartChildProcess(req *shared.RestartProcessRequest, op protos.OperationClient) error { func (hc *houstonClient) restartChildProcess(req *shared.RestartProcessRequest, op protos.OperationClient) error {