-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
149 lines (115 loc) · 3.63 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package appiumService
import (
"encoding/json"
"fmt"
agentConf "github.com/aaronchen2k/tester/internal/agent/conf"
deviceService "github.com/aaronchen2k/tester/internal/agent/service/device"
_const "github.com/aaronchen2k/tester/internal/pkg/const"
_domain "github.com/aaronchen2k/tester/internal/pkg/domain"
_httpUtils "github.com/aaronchen2k/tester/internal/pkg/libs/http"
_logUtils "github.com/aaronchen2k/tester/internal/pkg/libs/log"
_shellUtils "github.com/aaronchen2k/tester/internal/pkg/libs/shell"
"strconv"
"strings"
)
func CheckService(devices []_domain.DeviceInst) {
serials := deviceService.GetDeviceSerials(devices)
appiumObjMap, appiumPortMap := getServices(serials)
for i := 0; i < len(devices); i++ {
serial := devices[i].Serial
isAppiumOn := false
if _, ok := appiumObjMap[serial]; ok {
isAppiumOn = true
}
if isAppiumOn {
devices[i].AppiumPort = appiumObjMap[serial].AppiumPort
devices[i].AppiumStatus = _const.ServiceOn
} else {
validPort := getValidPort(appiumPortMap)
appium := _domain.Appium{DeviceSerial: serial, AppiumPort: validPort, ComputerIp: devices[0].ComputerIp}
appiumObjMap[serial] = appium
result := restartService(serial, validPort)
if result {
devices[i].AppiumStatus = _const.ServiceOn
devices[i].AppiumPort = validPort
} else {
devices[i].AppiumStatus = _const.ServiceOff
}
}
checkService(&devices[i])
}
}
func getServices(serials []string) (map[string]_domain.Appium, map[int]bool) {
appiumMap := map[string]_domain.Appium{}
portMap := map[int]bool{}
processMap := getAppiumProcesses(serials)
for serial, port := range processMap {
appium := _domain.Appium{DeviceSerial: serial, AppiumPort: port, ComputerIp: agentConf.Inst.Ip}
appiumMap[serial] = appium
portMap[port] = true
}
return appiumMap, portMap
}
func restartService(serial string, port int) bool {
_logUtils.Info("start appium")
killCmd := fmt.Sprintf("ps -ef | grep appium | grep %s | grep -v 'grep' | awk '{print $2}' | xargs kill -9", serial)
_, err := _shellUtils.ExeShell(killCmd)
if err != nil {
return false
}
startCmd := fmt.Sprintf("nohup appium -p %d --default-capabilities '{\"udid\":\"%s\"}' "+
"--log-timestamp --log-level debug:error "+
"> appium.log 2>&1 &", port, serial)
_, err = _shellUtils.ExeShell(startCmd)
if err != nil {
return false
}
return true
}
func getAppiumProcesses(serials []string) map[string]int {
cmd := "ps -ef | grep appium | grep default-capabilities | grep -v 'grep' | awk '{print $11,$13}'"
output, err := _shellUtils.ExeShell(cmd) // 4723 {"udid":"a0c865fd"}
ret := map[string]int{}
if err != nil || output == "" {
return ret
}
lines := strings.Split(output, "\n")
for _, line := range lines {
cols := strings.Split(line, " ")
port, _ := strconv.Atoi(cols[0])
jsonObj := map[string]string{}
json.Unmarshal([]byte(cols[1]), &jsonObj)
udid := jsonObj["udid"]
ret[udid] = port
}
return ret
}
func getValidPort(appiumPortMap map[int]bool) int {
newPort := 0
for i := 1; i < 100; i++ {
port := 50000 + i
if _, ok := appiumPortMap[port]; !ok {
newPort = port
break
}
}
appiumPortMap[newPort] = true
return newPort
}
func checkService(dev *_domain.DeviceInst) bool {
if dev.AppiumStatus != _const.ServiceOn {
return false
}
url := fmt.Sprintf("http://%s:%d/wd/hub/status", dev.ComputerIp, dev.AppiumPort)
result, ok := _httpUtils.GetObj(url, "appium")
if ok && result != nil {
resp := result.(map[string]interface{})
status := resp["status"]
if status.(float64) == 0 {
dev.DeviceStatus = _const.DeviceActive
dev.AppiumStatus = _const.ServiceActive
return true
}
}
return false
}