forked from xiaonanln/goworld
/
status.go
111 lines (96 loc) · 2.77 KB
/
status.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
package main
import (
"os"
"path/filepath"
"strings"
"github.com/keybase/go-ps"
"github.com/xiaonanln/goworld/engine/config"
)
// ServerStatus represents the status of a server
type ServerStatus struct {
NumDispatcherRunning int
NumGatesRunning int
NumGamesRunning int
DispatcherProcs []ps.Process
GateProcs []ps.Process
GameProcs []ps.Process
ServerID ServerID
}
// IsRunning returns if a server is running
func (ss *ServerStatus) IsRunning() bool {
return ss.NumDispatcherRunning > 0 || ss.NumGatesRunning > 0 || ss.NumGamesRunning > 0
}
func getProcPath(proc ps.Process) (string, error) {
path, err := proc.Path()
if err == nil {
return path, nil
}
if pathErr, ok := err.(*os.PathError); ok {
path = pathErr.Path
if strings.HasSuffix(path, " (deleted)") {
path = path[:len(path)-10]
return path, nil
}
}
return "", err
}
func detectServerStatus() *ServerStatus {
ss := &ServerStatus{}
procs, err := ps.Processes()
checkErrorOrQuit(err, "list processes failed")
for _, proc := range procs {
path, err := getProcPath(proc)
if err != nil {
continue
}
relpath, err := filepath.Rel(env.GoWorldRoot, path)
if err != nil || strings.HasPrefix(relpath, "..") {
continue
}
dir, file := filepath.Split(relpath)
if file == "dispatcher"+BinaryExtension {
ss.NumDispatcherRunning++
ss.DispatcherProcs = append(ss.DispatcherProcs, proc)
} else if file == "gate"+BinaryExtension {
ss.NumGatesRunning++
ss.GateProcs = append(ss.GateProcs, proc)
} else {
if strings.HasSuffix(dir, string(filepath.Separator)) {
dir = dir[:len(dir)-1]
}
serverid := ServerID(strings.Join(strings.Split(dir, string(filepath.Separator)), "/"))
if strings.HasPrefix(string(serverid), "cmd/") || strings.HasPrefix(string(serverid), "components/") || string(serverid) == "examples/test_client" {
// this is a cmd or a component, not a game
continue
}
ss.NumGamesRunning++
ss.GameProcs = append(ss.GameProcs, proc)
if ss.ServerID == "" {
ss.ServerID = serverid
}
}
}
return ss
}
func status() {
ss := detectServerStatus()
showServerStatus(ss)
}
func showServerStatus(ss *ServerStatus) {
showMsg("%d dispatcher running, %d/%d gates running, %d/%d games (%s) running", ss.NumDispatcherRunning,
ss.NumGatesRunning, len(config.GetGateIDs()),
ss.NumGamesRunning, len(config.GetGameIDs()),
ss.ServerID,
)
var listProcs []ps.Process
listProcs = append(listProcs, ss.DispatcherProcs...)
listProcs = append(listProcs, ss.GameProcs...)
listProcs = append(listProcs, ss.GateProcs...)
for _, proc := range listProcs {
path, err := getProcPath(proc)
if err != nil {
path = "[" + proc.Executable() + "]"
}
showMsg("\t%-10d%-16s%s", proc.Pid(), proc.Executable(), path)
}
}