From 01b4782e78ce4cf110f23932e40acf64582714cb Mon Sep 17 00:00:00 2001 From: mountain Date: Tue, 27 Jun 2023 09:44:56 +0900 Subject: [PATCH] =?UTF-8?q?=ED=94=84=EB=A1=9C=EC=84=B8=EC=8A=A4=20?= =?UTF-8?q?=EC=9E=AC=EC=8B=9C=EC=9E=91=20=EC=A7=80=EC=9B=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/client.go | 23 +++++++++++++------- client/operation.go | 41 ++++++----------------------------- protos/operation.proto | 3 ++- server/http_api.go | 24 +++++++++++++++++++- server/http_handler.go | 4 ++-- shared/operator.go | 2 +- shared/protos/operation.pb.go | 32 +++++++++++++++------------ 7 files changed, 68 insertions(+), 61 deletions(-) diff --git a/client/client.go b/client/client.go index 14597fa..7ccdf7f 100644 --- a/client/client.go +++ b/client/client.go @@ -274,15 +274,22 @@ func NewClient() (HoustonClient, error) { var newprocs []*procmeta for _, proc := range hc.childProcs { if proc.cmd == exited { - if proc.state == protos.ProcessState_Running { - go func(cmd *exec.Cmd) { - if err := cmd.Process.Signal(syscall.SIGTERM); err != nil { - cmd.Process.Signal(os.Kill) + if proc.state == protos.ProcessState_Running || proc.state == protos.ProcessState_Restart { + go func(proc *procmeta) { + if err := proc.cmd.Process.Signal(syscall.SIGTERM); err != nil { + proc.cmd.Process.Signal(os.Kill) } - cmd.Wait() - cmd.Process.Release() - logger.Println("abnormal termination of process :", cmd.Args) - }(proc.cmd) + proc.cmd.Wait() + proc.cmd.Process.Release() + + if proc.state == protos.ProcessState_Restart { + hc.startChildProcess(&shared.StartProcessRequest{ + Version: proc.version, + Name: proc.name, + Args: proc.cmd.Args, + }, op) + } + }(proc) } } else { newprocs = append(newprocs, proc) diff --git a/client/operation.go b/client/operation.go index 9226e93..c07772e 100644 --- a/client/operation.go +++ b/client/operation.go @@ -12,7 +12,6 @@ import ( "os/exec" "path" "path/filepath" - "regexp" "runtime/debug" "strings" "syscall" @@ -165,33 +164,17 @@ func zipLogFiles(storageRoot string, req *shared.UploadRequest, start, except st } func prepareProcessLaunch(storageRoot string, req *shared.StartProcessRequest) *procmeta { - re := regexp.MustCompile(`[^\s"']+|"([^"]*)"|'([^']*)`) - argsTemp := re.FindAllString(req.Args, -1) - var args []string - for _, arg := range argsTemp { - if strings.HasPrefix(arg, `"`) && len(args) > 0 { - lastarg := args[len(args)-1] - if strings.HasSuffix(lastarg, "=") { - args[len(args)-1] = lastarg + arg - } else { - args = append(args, arg) - } - } else { - args = append(args, arg) - } - } - - if len(args) == 0 { + if len(req.Args) == 0 { return nil } verpath := path.Join(storageRoot, req.Name, req.Version) fi, err := os.Stat(verpath) if err == nil && fi.IsDir() { - args[0] = "./" + path.Clean(strings.TrimPrefix(args[0], "/")) - os.Chmod(path.Join(verpath, args[0]), 0777) + req.Args[0] = "./" + path.Clean(strings.TrimPrefix(req.Args[0], "/")) + os.Chmod(path.Join(verpath, req.Args[0]), 0777) - cmd := exec.Command(args[0], args[1:]...) + cmd := exec.Command(req.Args[0], req.Args[1:]...) cmd.Dir = verpath stdin, _ := cmd.StdinPipe() @@ -462,27 +445,17 @@ func (hc *houstonClient) restartChildProcess(req *shared.RestartProcessRequest, } } + proc.state = protos.ProcessState_Restart + op.Refresh(context.Background(), hc.makeOperationQueryRequest()) + if err := proc.cmd.Process.Signal(syscall.SIGTERM); err != nil { proc.cmd.Process.Signal(os.Kill) } - proc.state = protos.ProcessState_Stopping - proc.cmd.Wait() - proc.cmd.Process.Release() - op.Refresh(context.Background(), hc.makeOperationQueryRequest()) - - proc.state = protos.ProcessState_Stopped - op.Refresh(context.Background(), hc.makeOperationQueryRequest()) - - if err := hc.launch(proc); err != nil { - return err - } break } } - op.Refresh(context.Background(), hc.makeOperationQueryRequest()) - return nil } diff --git a/protos/operation.proto b/protos/operation.proto index 3b7fccb..e968052 100644 --- a/protos/operation.proto +++ b/protos/operation.proto @@ -27,7 +27,8 @@ enum ProcessState { Stopped = 0; Stopping = 1; Running = 2; - Error = 3; + Restart = 3; + Error = 4; } message ProcessDescription { diff --git a/server/http_api.go b/server/http_api.go index fce8f11..f24a4b3 100644 --- a/server/http_api.go +++ b/server/http_api.go @@ -9,6 +9,7 @@ import ( "net/http" "os" "path" + "regexp" "strconv" "strings" "time" @@ -294,7 +295,7 @@ func (h *houstonHandler) StartProcess(w http.ResponseWriter, r *http.Request) { // name := r.FormValue("name") version := r.FormValue("version") - args := r.FormValue("args") + argsline := r.FormValue("args") traws := r.FormValue("targets") var targets []string @@ -311,6 +312,27 @@ func (h *houstonHandler) StartProcess(w http.ResponseWriter, r *http.Request) { return } + re := regexp.MustCompile(`[^\s"']+|"([^"]*)"|'([^']*)`) + argsTemp := re.FindAllString(argsline, -1) + var args []string + for _, arg := range argsTemp { + if strings.HasPrefix(arg, `"`) && len(args) > 0 { + lastarg := args[len(args)-1] + if strings.HasSuffix(lastarg, "=") { + args[len(args)-1] = lastarg + arg + } else { + args = append(args, arg) + } + } else { + args = append(args, arg) + } + } + + if len(args) == 0 { + w.WriteHeader(http.StatusBadRequest) + return + } + h.Operation().StartProcess(MakeStartProcessRequest(shared.StartProcessRequest{ Name: name, Version: version, diff --git a/server/http_handler.go b/server/http_handler.go index a73f529..c21b5e2 100644 --- a/server/http_handler.go +++ b/server/http_handler.go @@ -157,10 +157,10 @@ func (h *houstonHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { var operation string if r.Method == "POST" { operation = r.FormValue("operation") - logger.Println("api called :", userinfo, r.Form) + logger.Println("api called :", userinfo["email"], r.Form) } else { operation = r.URL.Query().Get("operation") - logger.Println("api called :", userinfo, r.URL.Query()) + logger.Println("api called :", userinfo["email"], r.URL.Query()) } if len(operation) == 0 { diff --git a/shared/operator.go b/shared/operator.go index 1876ed8..6f20ff0 100644 --- a/shared/operator.go +++ b/shared/operator.go @@ -35,7 +35,7 @@ type WithdrawRequest struct { type StartProcessRequest struct { Name string Version string - Args string + Args []string } type StopProcessRequest struct { diff --git a/shared/protos/operation.pb.go b/shared/protos/operation.pb.go index dface1c..79bc282 100644 --- a/shared/protos/operation.pb.go +++ b/shared/protos/operation.pb.go @@ -26,7 +26,8 @@ const ( ProcessState_Stopped ProcessState = 0 ProcessState_Stopping ProcessState = 1 ProcessState_Running ProcessState = 2 - ProcessState_Error ProcessState = 3 + ProcessState_Restart ProcessState = 3 + ProcessState_Error ProcessState = 4 ) // Enum value maps for ProcessState. @@ -35,13 +36,15 @@ var ( 0: "Stopped", 1: "Stopping", 2: "Running", - 3: "Error", + 3: "Restart", + 4: "Error", } ProcessState_value = map[string]int32{ "Stopped": 0, "Stopping": 1, "Running": 2, - "Error": 3, + "Restart": 3, + "Error": 4, } ) @@ -422,20 +425,21 @@ var file_protos_operation_proto_rawDesc = []byte{ 0x37, 0x0a, 0x09, 0x41, 0x72, 0x67, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, - 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x2a, 0x41, 0x0a, 0x0c, 0x50, 0x72, 0x6f, 0x63, + 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x2a, 0x4e, 0x0a, 0x0c, 0x50, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x53, 0x74, 0x6f, 0x70, 0x70, 0x65, 0x64, 0x10, 0x00, 0x12, 0x0c, 0x0a, 0x08, 0x53, 0x74, 0x6f, 0x70, 0x70, 0x69, 0x6e, 0x67, 0x10, 0x01, 0x12, 0x0b, 0x0a, 0x07, 0x52, 0x75, 0x6e, 0x6e, 0x69, 0x6e, 0x67, 0x10, 0x02, - 0x12, 0x09, 0x0a, 0x05, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0x03, 0x32, 0x78, 0x0a, 0x09, 0x4f, - 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x3e, 0x0a, 0x05, 0x51, 0x75, 0x65, 0x72, - 0x79, 0x12, 0x16, 0x2e, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x51, 0x75, 0x65, - 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x4f, 0x70, 0x65, 0x72, - 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, - 0x73, 0x65, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x12, 0x2b, 0x0a, 0x07, 0x52, 0x65, 0x66, 0x72, - 0x65, 0x73, 0x68, 0x12, 0x16, 0x2e, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x51, - 0x75, 0x65, 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x06, 0x2e, 0x45, 0x6d, - 0x70, 0x74, 0x79, 0x22, 0x00, 0x42, 0x0f, 0x5a, 0x0d, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2f, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x12, 0x0b, 0x0a, 0x07, 0x52, 0x65, 0x73, 0x74, 0x61, 0x72, 0x74, 0x10, 0x03, 0x12, 0x09, 0x0a, + 0x05, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0x04, 0x32, 0x78, 0x0a, 0x09, 0x4f, 0x70, 0x65, 0x72, + 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x3e, 0x0a, 0x05, 0x51, 0x75, 0x65, 0x72, 0x79, 0x12, 0x16, + 0x2e, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, + 0x6f, 0x6e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, + 0x00, 0x28, 0x01, 0x30, 0x01, 0x12, 0x2b, 0x0a, 0x07, 0x52, 0x65, 0x66, 0x72, 0x65, 0x73, 0x68, + 0x12, 0x16, 0x2e, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x51, 0x75, 0x65, 0x72, + 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x06, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, + 0x22, 0x00, 0x42, 0x0f, 0x5a, 0x0d, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2f, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var (