package servicemgr
import (
- "encoding/json"
- "fmt"
"log"
"os"
"os/exec"
// 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()
+
}
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()
}
package servicemgr
-import "os/exec"
-
var logPrefix = "servicemgr"
-// ServiceCmdMap map
-var ServiceCmdMap = map[string](*exec.Cmd){}
-
// ExecutionParam structure
type ExecutionParam struct {
Broker string
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"`
}
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"`
+}