From 9154383ee65a310f530c678ce0f94aa21f6113e8 Mon Sep 17 00:00:00 2001 From: "jw_wonny.cha" Date: Mon, 25 Mar 2019 17:00:21 +0900 Subject: [PATCH] Clean-up service execution --- src/servicemgr/service_execution.go | 110 +++++++++++++++++----------- 1 file changed, 69 insertions(+), 41 deletions(-) diff --git a/src/servicemgr/service_execution.go b/src/servicemgr/service_execution.go index 8f56380..1c7e400 100644 --- a/src/servicemgr/service_execution.go +++ b/src/servicemgr/service_execution.go @@ -1,6 +1,7 @@ package servicemgr import ( + "encoding/json" "log" "os" "os/exec" @@ -8,20 +9,46 @@ import ( // Service for service that execute by self type Service struct { - serviceID uint64 - serviceName string - paramStr []string + serviceID uint64 + serviceName string + paramStr []string + notificationTargetURL string } -func (p Service) execute() error { +func (p Service) execute() (err error) { log.Println(p.serviceName, p.paramStr) - binary, lookErr := exec.LookPath(p.serviceName) - if lookErr != nil { - return lookErr + cmd, pid, err := p.setService() + if err != nil { + return } + log.Println("Just ran subprocess ", pid) + + p.registerService() - cmd, _ := GetCmd(p.serviceID) + executeCh := make(chan error) + + go func() { + executeCh <- cmd.Wait() + }() + + status := p.waitService(executeCh) + + p.notifyServiceStatus(status) + + DeleteServiceMap(p.serviceID) + p.removeService() + + return nil +} + +func (p Service) setService() (cmd *exec.Cmd, pid int, err error) { + binary, err := exec.LookPath(p.serviceName) + if err != nil { + return + } + + cmd, _ = GetCmd(p.serviceID) if len(p.paramStr) == 0 { cmd = exec.Command(binary) @@ -29,42 +56,32 @@ func (p Service) execute() error { cmd = exec.Command(binary, p.paramStr...) } - cmd.Stdout = os.Stdout - - err := cmd.Start() - if err != nil { - return err + setErr := SetCmd(p.serviceID, cmd) + if setErr != nil { + log.Println(setErr.Error()) } - err = SetCmd(p.serviceID, cmd) + cmd.Stdout = os.Stdout + + err = cmd.Start() if err != nil { - log.Println(err.Error()) + return } - log.Println("Just ran subprocess ", cmd.Process.Pid) - p.registerService() - - executeCh := make(chan error) - - go func() { - executeCh <- cmd.Wait() - }() - - p.waitService(executeCh) + pid = cmd.Process.Pid - return nil + return } func (p Service) registerService() { registerCh := make(chan error) - // portNumber is not meaningful number now, so use magic number temporarily appName, err := GetAppName(p.serviceID) if err != nil { log.Println("[Fail] GetAppName is Failed") } - go RegisterService(appName, p.serviceID, p.serviceName, "todo", registerCh) + go RegisterService(appName, p.serviceID, p.serviceName, ConstServiceStatusStarted, registerCh) if err := <-registerCh; err == nil { log.Println("[Success] Service Register is Success") } else { @@ -81,30 +98,41 @@ func (p Service) removeService() { } } -func (p Service) waitService(executeCh <-chan error) { +func (p Service) waitService(executeCh <-chan error) (status string) { e := <-executeCh - ctlChan, _ := GetCtlChan(p.serviceID) - ctlChan <- true + // ctlChan, _ := GetCtlChan(p.serviceID) + // ctlChan <- true + + status = ConstServiceStatusFinished if e != nil { if e.Error() == os.Kill.String() { log.Println("Success to delete service") } else { + status = ConstServiceStatusFailed log.Println(p.serviceName, "exited with error : ", e) - - // @TODO TargetURL needs to be declared - // pbytes, _ := json.Marshal(common.ErrorType{e.Error()}) - - // err := httpsender.SendPostJSONMsg("targetURL", pbytes) - // if err != nil { - // log.Println(err.Error()) - // } } } else { log.Println(p.serviceName, "is exited with no error") } - DeleteServiceMap(p.serviceID) - p.removeService() + return +} + +func (p Service) notifyServiceStatus(status string) (err error) { + var statusNotificationRequest map[string]interface{} + + statusNotificationRequest = make(map[string]interface{}) + statusNotificationRequest["ServiceID"] = p.serviceID + statusNotificationRequest["Status"] = status + + reqbytes, _ := json.Marshal(statusNotificationRequest) + // @TODO TargetURL needs to be declared + // err = + SendPostJSONMsg(p.notificationTargetURL, reqbytes) + // if err != nil { + // log.Println(err.Error()) + // } + return } -- 2.34.1