680d42c30a9e273dfcdbd17fc7c7b251d84b4741
[platform/core/system/edge-orchestration.git] / src / servicemgr / service_agent.go
1 package servicemgr
2
3 import (
4         "encoding/json"
5         "log"
6         "os/exec"
7         "strconv"
8         "time"
9 )
10
11 func makeTime() string {
12         t := time.Now()
13         return t.Format(time.RFC3339)
14 }
15
16 // Create function
17 func Create(serviceParam ServiceParam) []byte {
18         instanceCount := serviceParam.Count
19
20         ret := ServiceExecutionResponse{}
21         ret.ServiceList = make([]ServiceExecutionItem, instanceCount)
22
23         for index := 0; index < instanceCount; index++ {
24                 var cmd *exec.Cmd
25
26                 log.Println("servicName: ", serviceParam.ServiceName)
27                 id, msgChan, ctlChan := CreateServiceMap(cmd, serviceParam.ServiceName, serviceParam.AppName)
28                 log.Println("id : ", id)
29
30                 go Push(id, msgChan, ctlChan)
31
32                 serviceInfo := ServiceExecutionItem{id, makeTime()}
33                 ret.ServiceList[index] = serviceInfo
34         }
35
36         sibytes, _ := json.Marshal(ret)
37         return sibytes
38 }
39
40 // Run is for executing service
41 func Run(distService DistService, serviceID uint64) {
42         serviceName, err := GetServiceName(serviceID)
43         if err != nil {
44                 log.Println(err.Error())
45                 return
46         }
47
48         var args []string
49         for _, userParam := range distService.UserParam.Args {
50                 args = append(args, userParam)
51         }
52
53         args = append(args, distService.SystemParam.IP)
54         args = append(args, strconv.Itoa(distService.SystemParam.Port))
55         args = append(args, "--remote")
56
57         service := Service{serviceID, serviceName, args}
58         service.execute()
59 }