divide a function with service creation & execution
authorjw_wonny.cha <jw_wonny.cha@samsung.com>
Thu, 14 Mar 2019 05:28:27 +0000 (14:28 +0900)
committerjw_wonny.cha <jw_wonny.cha@samsung.com>
Thu, 14 Mar 2019 05:28:27 +0000 (14:28 +0900)
src/servicemgr/play_service.go
src/servicemgr/service_agent.go
src/servicemgr/types.go

index 2aec40ed3e4f5dc1e27976ccc17129f584d9de09..871f099b6ffd14a3344d047375953505f6eabc86 100644 (file)
@@ -1,8 +1,6 @@
 package servicemgr
 
 import (
-       "encoding/json"
-       "fmt"
        "log"
        "os"
        "os/exec"
@@ -10,53 +8,94 @@ import (
 
 // PlayService for service that execute by self
 type PlayService struct {
+       serviceID   uint64
        serviceName string
        paramStr    []string
 }
 
-func (p PlayService) execute() {
-       fmt.Println(p.serviceName, " ", p.paramStr)
+func (p PlayService) execute() error {
+       log.Println(p.serviceName, p.paramStr)
 
        binary, lookErr := exec.LookPath(p.serviceName)
        if lookErr != nil {
-               panic(lookErr)
+               return lookErr
        }
 
-       var cmd *exec.Cmd
+       cmdItem, _ := ServiceCmdMap.Get(p.serviceID)
+       cmd := cmdItem.(*exec.Cmd)
+
        if len(p.paramStr) == 0 {
                cmd = exec.Command(binary)
        } else {
                cmd = exec.Command(binary, p.paramStr...)
        }
 
-       ServiceCmdMap[p.serviceName] = cmd
-
        cmd.Stdout = os.Stdout
 
        err := cmd.Start()
        if err != nil {
-               log.Fatal(err)
+               return err
        }
 
-       log.Printf("Just ran subprocess %d, exiting\n", cmd.Process.Pid)
+       ServiceCmdMap.Set(p.serviceID, cmd)
+
+       log.Println("Just ran subprocess ", cmd.Process.Pid)
+       p.registerService()
 
-       ch := make(chan error)
+       executeCh := make(chan error)
 
        go func() {
-               ch <- cmd.Wait()
+               executeCh <- cmd.Wait()
        }()
 
-       select {
-       case e := <-ch:
-               if e != nil {
-                       log.Println(p.serviceName, "exited with Error : ", e)
+       p.waitService(executeCh)
 
-                       pbytes, _ := json.Marshal(ErrorType{e.Error()})
-                       // @TODO TargetURL needs to be declared
-                       SendPostJSONMsg("targetURL", pbytes)
+       return nil
+}
+
+func (p PlayService) registerService() {
+       registerCh := make(chan int)
+
+       portNumber := 2838
+       go RegisterService(p.serviceName, "", portNumber, registerCh)
+       if <-registerCh == Pass {
+               log.Println("Service Register is Success")
+       } else {
+               log.Println("Service Register is Failed")
+       }
+}
+
+func (p PlayService) removeService() {
+       res, _ := RemoveService(p.serviceName)
+       if res == Pass {
+               log.Println("Service Unregister is Success")
+       } else {
+               log.Println("Service Unregister is Failed")
+       }
+}
+
+func (p PlayService) waitService(executeCh <-chan error) {
+       e := <-executeCh
+
+       if e != nil {
+               if e.Error() == os.Kill.String() {
+                       log.Println("Success to delete service")
                } else {
-                       log.Println(p.serviceName, "exited")
+                       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())
+                       // }
                }
-               delete(ServiceCmdMap, p.serviceName)
+       } else {
+               log.Println(p.serviceName, "is exited with no error")
        }
+
+       DeleteServiceMap(p.serviceID)
+       p.removeService()
+
 }
index 5054a6dfd9dfa701050dc656b4dbbfb0b333e00e..b032d1d2aea62b3d3f127e4baf9675d0d2078625 100644 (file)
@@ -1,33 +1,61 @@
 package servicemgr
 
 import (
+       "encoding/json"
+       "log"
+       "os/exec"
        "strconv"
+       "time"
 )
 
 // ServiceAgent is interface for basic-service
 type ServiceAgent interface {
-       execute()
+       execute() error
+}
+
+func makeTime() string {
+       t := time.Now()
+       return t.Format(time.RFC3339)
+}
+
+// Create function
+func Create(serviceParam ServiceParam) []byte {
+       instanceCount := serviceParam.Count
+
+       ret := ServiceExecutionResponse{}
+       ret.ServiceList = make([]ServiceExecutionItem, instanceCount)
+
+       for index := 0; index < instanceCount; index++ {
+               var cmd *exec.Cmd
+               log.Println("servicName: ", serviceParam.ServiceName)
+               id := CreateServiceMap(cmd, serviceParam.ServiceName, serviceParam.AppName)
+               log.Println("id : ", id)
+
+               serviceInfo := ServiceExecutionItem{id, makeTime()}
+               ret.ServiceList[index] = serviceInfo
+       }
+
+       sibytes, _ := json.Marshal(ret)
+       return sibytes
 }
 
 // Run is for executing service
-func Run(executionJSON DistService) {
+func Run(distService DistService, serviceID uint64) {
        var service ServiceAgent
-       serviceType := executionJSON.Type
 
        var args []string
-       for _, userParam := range executionJSON.UserParam.Args {
+       for _, userParam := range distService.UserParam.Args {
                args = append(args, userParam)
        }
 
-       args = append(args, executionJSON.SystemParam.IP)
-       args = append(args, strconv.Itoa(executionJSON.SystemParam.Port))
+       args = append(args, distService.SystemParam.IP)
+       args = append(args, strconv.Itoa(distService.SystemParam.Port))
+       args = append(args, "--remote")
 
-       if serviceType == "CPU" {
-               service = PlayService{executionJSON.ServiceName, args}
-       }
-       // else {
-       // service = SaveService{executionJSON.ServiceInfo.SavePath, executionJSON.ServiceInfo.FileItem}
-       // }
+       name, _ := ServiceNameMap.Get(serviceID)
+       serviceName, _ := name.(string)
+
+       service = PlayService{serviceID, serviceName, args}
 
        service.execute()
 }
index 26a057c0f7d4005be58be980a690bb985415a387..764019bb40a0d1be9f092cccb87058de41f0ad20 100644 (file)
@@ -1,12 +1,7 @@
 package servicemgr
 
-import "os/exec"
-
 var logPrefix = "servicemgr"
 
-// ServiceCmdMap map
-var ServiceCmdMap = map[string](*exec.Cmd){}
-
 // ExecutionParam structure
 type ExecutionParam struct {
        Broker string
@@ -33,11 +28,15 @@ type ExecutionType struct {
        ServiceInfo SaveServiceInfo
 }
 
+// ServiceParam structrue
+type ServiceParam struct {
+       AppName     string `json:"AppName"`
+       ServiceName string `json:"ServiceName"`
+       Count       int    `json:"Count"`
+}
+
 // DistService structure
 type DistService struct {
-       ServiceName string      `json:"ServiceName"`
-       Status      string      `json:"Status"`
-       Type        string      `json:"Type"`
        SystemParam SystemParam `json:"SystemParam"`
        UserParam   UserParam   `json:"UserParam"`
 }
@@ -57,3 +56,19 @@ type UserParam struct {
 type ErrorType struct {
        ErrorCode string `json:"ErrorCode"`
 }
+
+// ServiceCreationResponse structure
+type ServiceCreationResponse struct {
+       Return string `json:"return"`
+}
+
+// ServiceExecutionResponse structure
+type ServiceExecutionResponse struct {
+       ServiceList []ServiceExecutionItem `json:"serviceList"`
+}
+
+// ServiceExecutionItem structrue
+type ServiceExecutionItem struct {
+       ID   uint64 `json:"id"`
+       Time string `json:"time"`
+}