forked from ergo-services/ergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appmon.go
212 lines (176 loc) · 5.51 KB
/
appmon.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package ergo
// TODO: https://github.com/erlang/otp/blob/master/lib/runtime_tools-1.13.1/src/appmon_info.erl
import (
"time"
"github.com/halturin/ergo/etf"
"github.com/halturin/ergo/lib"
)
type appMon struct {
GenServer
}
type appMonState struct {
process *Process
jobs map[etf.Atom][]jobDetails
}
type jobDetails struct {
name etf.Atom
args etf.List
sendTo etf.Pid
}
// Init initializes process state using arbitrary arguments
// Init -> state
func (am *appMon) Init(p *Process, args ...interface{}) interface{} {
lib.Log("APP_MON: Init %#v", args)
from := args[0]
p.Link(from.(etf.Pid))
return appMonState{
process: p,
jobs: make(map[etf.Atom][]jobDetails),
}
}
// HandleCast -> ("noreply", state) - noreply
// ("stop", reason) - stop with reason
func (am *appMon) HandleCast(message etf.Term, state interface{}) (string, interface{}) {
var appState appMonState = state.(appMonState)
lib.Log("APP_MON: HandleCast: %#v", message)
switch message {
case "sendStat":
for cmd, jobs := range state.(appMonState).jobs {
switch cmd {
case "app_ctrl":
// From ! {delivery, self(), Cmd, Aux, Result}
apps := appState.process.Node.WhichApplications()
for i := range jobs {
appList := make(etf.List, len(apps))
for ai, a := range apps {
appList[ai] = etf.Tuple{a.PID, etf.Atom(a.Name),
etf.Tuple{etf.Atom(a.Name), a.Description, a.Version},
}
}
delivery := etf.Tuple{etf.Atom("delivery"), appState.process.Self(), cmd, jobs[i].name, appList}
appState.process.Send(jobs[i].sendTo, delivery)
}
case "app":
for i := range jobs {
appTree := am.makeAppTree(appState.process, jobs[i].name)
if appTree == nil {
continue
}
delivery := etf.Tuple{etf.Atom("delivery"), appState.process.Self(), cmd, jobs[i].name, appTree}
appState.process.Send(jobs[i].sendTo, delivery)
}
}
}
appState.process.CastAfter(appState.process.Self(), "sendStat", 2*time.Second)
return "noreply", state
default:
switch m := message.(type) {
case etf.Tuple:
if len(m) == 5 {
// etf.Tuple{etf.Pid{Node:"erl-demo@127.0.0.1", Id:0x7c, Serial:0x0, Creation:0x1}, "app_ctrl", "demo@127.0.0.1", "true", etf.List{}}
job := jobDetails{
name: m.Element(3).(etf.Atom),
args: m.Element(5).(etf.List),
sendTo: m.Element(1).(etf.Pid),
}
if m.Element(4) == etf.Atom("true") {
// add new job
if len(state.(appMonState).jobs) == 0 {
appState.process.Cast(appState.process.Self(), "sendStat")
}
if jobList, ok := state.(appMonState).jobs[m.Element(2).(etf.Atom)]; ok {
for i := range jobList {
if jobList[i].name == job.name {
return "noreply", appState
}
}
jobList = append(jobList, job)
state.(appMonState).jobs[m.Element(2).(etf.Atom)] = jobList
} else {
state.(appMonState).jobs[m.Element(2).(etf.Atom)] = []jobDetails{job}
}
} else {
// remove a job
if jobList, ok := state.(appMonState).jobs[m.Element(2).(etf.Atom)]; ok {
for i := range jobList {
if jobList[i].name == job.name {
jobList[i] = jobList[0]
jobList = jobList[1:]
if len(jobList) > 0 {
state.(appMonState).jobs[m.Element(2).(etf.Atom)] = jobList
} else {
delete(state.(appMonState).jobs, m.Element(2).(etf.Atom))
}
break
}
}
}
if len(state.(appMonState).jobs) == 0 {
return "stop", "normal"
}
}
return "noreply", appState
}
// etf.Tuple{etf.Atom("EXIT"), Pid, reason}
}
}
return "stop", "normal"
}
// HandleCall serves incoming messages sending via gen_server:call
// HandleCall -> ("reply", message, state) - reply
// ("noreply", _, state) - noreply
// ("stop", reason, _) - normal stop
func (am *appMon) HandleCall(from etf.Tuple, message etf.Term, state interface{}) (string, etf.Term, interface{}) {
lib.Log("APP_MON: HandleCall: %#v, From: %#v", message, from)
// return "reply", reply, state
return "stop", "normal", state
}
// HandleInfo serves all another incoming messages (Pid ! message)
// HandleInfo -> ("noreply", state) - noreply
// ("stop", reason) - normal stop
func (am *appMon) HandleInfo(message etf.Term, state interface{}) (string, interface{}) {
lib.Log("APP_MON: HandleInfo: %#v", message)
return "stop", "normal"
}
// Terminate called when process died
func (am *appMon) Terminate(reason string, state interface{}) {
lib.Log("APP_MON: Terminate: %#v", reason)
}
func (am *appMon) makeAppTree(p *Process, app etf.Atom) etf.Tuple {
appInfo, err := p.Node.GetApplicationInfo(string(app))
if err != nil {
return nil
}
resolver := make(map[etf.Pid]interface{})
tree := makeTree(p, resolver, appInfo.PID)
children := etf.List{etf.Tuple{appInfo.PID, appInfo.PID.Str()}}
for p, n := range resolver {
children = append(children, etf.Tuple{p, n})
}
appTree := etf.Tuple{
appInfo.PID.Str(), // pid or registered name
children,
tree,
etf.List{}, // TODO: links
}
return appTree
}
func makeTree(p *Process, resolver map[etf.Pid]interface{}, pid etf.Pid) etf.List {
pidProcess := p.Node.GetProcessByPid(pid)
if pidProcess == nil {
return etf.List{}
}
if pidProcess.name != "" {
resolver[pid] = pidProcess.name
} else {
resolver[pid] = pid.Str()
}
tree := etf.List{}
for _, cp := range pidProcess.GetChildren() {
children := makeTree(p, resolver, cp)
child := etf.Tuple{resolver[pid], resolver[cp]}
tree = append(tree, child)
tree = append(tree, children...)
}
return tree
}