프로세스 종료시 알림

This commit is contained in:
2023-11-24 00:19:17 +09:00
parent d18fe3e3a1
commit 1a404e5361
9 changed files with 171 additions and 54 deletions

View File

@ -17,6 +17,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
"unsafe"
@ -67,15 +68,30 @@ type HoustonClient interface {
Start()
}
var seq = int32(1)
type procmeta struct {
id int32
cmd *exec.Cmd
name string
args []string
version string
state protos.ProcessState
state int32
stdin io.WriteCloser
}
func (pm *procmeta) isState(s protos.ProcessState) bool {
return atomic.LoadInt32(&pm.state) == int32(s)
}
func (pm *procmeta) getState() protos.ProcessState {
return protos.ProcessState(atomic.LoadInt32(&pm.state))
}
func (pm *procmeta) setState(s protos.ProcessState) {
atomic.StoreInt32(&pm.state, int32(s))
}
type houstonClient struct {
childProcs []*procmeta
extraMetrics unsafe.Pointer // map[string]float32
@ -167,7 +183,7 @@ func (hc *houstonClient) makeOperationQueryRequest() *protos.OperationQueryReque
Name: child.name,
Args: child.args,
Version: child.version,
State: child.state,
State: child.getState(),
Pid: int32(child.cmd.Process.Pid),
})
}
@ -283,7 +299,7 @@ func NewClient(standalone bool) (HoustonClient, error) {
var newprocs []*procmeta
for _, proc := range hc.childProcs {
if proc.cmd == exited {
if proc.state == protos.ProcessState_Running || proc.state == protos.ProcessState_Restart {
if proc.isState(protos.ProcessState_Running) || proc.isState(protos.ProcessState_Restart) {
go func(proc *procmeta) {
if err := proc.cmd.Process.Signal(syscall.SIGTERM); err != nil {
proc.cmd.Process.Signal(os.Kill)
@ -291,7 +307,7 @@ func NewClient(standalone bool) (HoustonClient, error) {
proc.cmd.Wait()
proc.cmd.Process.Release()
if proc.state == protos.ProcessState_Restart {
if proc.isState(protos.ProcessState_Restart) {
hc.startChildProcess(&shared.StartProcessRequest{
Version: proc.version,
Name: proc.name,
@ -407,6 +423,30 @@ func NewClient(standalone bool) (HoustonClient, error) {
if err := hc.uploadFiles(&ur); err != nil {
logger.Println(err)
}
case shared.Exception:
idstr := resp.Args["id"]
id64, _ := strconv.ParseInt(idstr, 10, 0)
id := int32(id64)
var killing *procmeta
var remains []*procmeta
for _, meta := range hc.childProcs {
if meta.id == id {
killing = meta
} else {
remains = append(remains, meta)
}
}
if killing != nil {
killing.cmd.Wait()
killing.cmd.Process.Release()
}
hc.childProcs = remains
op.Refresh(context.Background(), hc.makeOperationQueryRequest())
}
}
}
@ -428,7 +468,7 @@ func (hc *houstonClient) Start() {
for _, proc := range hc.childProcs {
if err := proc.cmd.Process.Signal(syscall.SIGTERM); err != nil {
proc.cmd.Process.Signal(os.Kill)
proc.state = protos.ProcessState_Stopping
proc.setState(protos.ProcessState_Stopping)
}
}