-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgomonitor.go
246 lines (199 loc) · 4.21 KB
/
gomonitor.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package easyworker
import (
"errors"
"log"
"sync"
"sync/atomic"
)
const (
// call user function success.
SIGNAL_DONE = iota
// call user function failed/panic
SIGNAL_FAILED
)
const ()
// Signal was sent when Goroutine is no longer run (done/failed/panic).
type GoSignal struct {
// Monitor reference id.
RefId int64
// Kind of signal (SIGNAL_DONE, SIGNAL_FAILED)
Signal int
}
// channel for send signal.
type monitorChan chan GoSignal
// A struct wrap goroutine to handle panic and can re-run easily.
type Go struct {
lock sync.Mutex
state atomic.Int64
id int64
panicListeners map[int64]monitorChan
fun any
params []any
// store result
result []any
}
var (
lastRefId atomic.Int64
)
func getNewRefId() int64 {
return lastRefId.Add(1)
}
/*
Create new Go struct.
The first parameter is user function.
The second parameter and more is parameter for parameter of user function.
*/
func NewGo(fun any, params ...any) (ret *Go, retErr error) {
if retErr = verifyFunc(fun); retErr != nil {
return
}
id := getNewRefId()
ret = &Go{
id: id,
fun: fun,
params: params,
panicListeners: make(map[int64]monitorChan),
}
ret.state.Store(STANDBY)
return
}
/*
Create Go and run.
Same with function NewGo but run after create Go struct.
*/
func NewGoAndRun(fun any, params ...any) (ret *Go, retErr error) {
ret, retErr = NewGo(fun, params...)
if retErr != nil {
return
}
retErr = ret.Run()
return
}
/*
Used for receiving a signal when Go done or failed.
Function return unique reference id and a channel for receiving signal.
*/
func (g *Go) Monitor() (int64, <-chan GoSignal) {
refId := getNewRefId()
// always set buffer to 1 for async and run_task can exit.
ch := make(monitorChan, 1)
g.lock.Lock()
defer g.lock.Unlock()
g.panicListeners[refId] = ch
return refId, ch
}
/*
Remove a monitor reference.
After demonitor channel will be closed.
*/
func (g *Go) Demonitor(refId int64) {
g.lock.Lock()
defer g.lock.Unlock()
ch, existed := g.panicListeners[refId]
if existed {
close(ch)
delete(g.panicListeners, refId)
}
}
/*
Run and wait for task done.
Return true if task done in normally. False for failed. Error is cannot run task.
*/
func (g *Go) RunAndWait() (bool, error) {
_, ch := g.Monitor()
err := g.Run()
if err != nil {
return false, err
}
msg := <-ch
return msg.Signal == SIGNAL_DONE, nil
}
func (g *Go) pushSignal(msg GoSignal) {
g.lock.Lock()
defer g.lock.Unlock()
for refId, ch := range g.panicListeners {
msg.RefId = refId
ch <- msg
}
}
/*
Stop Go just for clean data in internal struct.
Call Stop after Go process task done.
*/
func (g *Go) Stop() {
g.lock.Lock()
defer g.lock.Unlock()
for refId, ch := range g.panicListeners {
close(ch)
delete(g.panicListeners, refId)
}
g.result = nil
g.state.Store(STOPPED)
}
/*
Start Go to process task.
The function can call many.
*/
func (g *Go) Run() error {
if g.state.Load() == STOPPED {
return errors.New("Go cannot run, it stopped")
}
go g.run_task()
return nil
}
func (g *Go) run_task() {
g.state.Store(RUNNING)
msg := GoSignal{}
defer func() {
// catch if panic by child code.
if r := recover(); r != nil {
msg.Signal = SIGNAL_FAILED
if printLog {
log.Println(g.id, ", Go was panic, ", r)
}
}
g.pushSignal(msg)
g.state.Store(STANDBY)
}()
var (
err error
result []any
)
//log.Println("Go run, params:", g.params)
// call user define function.
result, err = invokeFun(g.fun, g.params...)
g.lock.Lock()
if err != nil {
g.result = append(g.result, err)
} else {
g.result = result
}
g.lock.Unlock()
if err != nil {
msg.Signal = SIGNAL_FAILED
if printLog {
log.Println(g.id, "Go call user function failed, reason:", err)
}
}
}
/*
Return state of Go.
Kind of state:
- RUNNING: Task is running.
- STOPPED: Stop by user. In this state user cannot run again.
- STANDBY: Task is standby wait for start or just done task.
*/
func (g *Go) State() int64 {
return g.state.Load()
}
/*
Get result from last run.
Result is slice of any.
Length of slice is number of parameter return from user function.
Cast to right type for value.
*/
func (g *Go) GetResult() []any {
g.lock.Lock()
defer g.lock.Unlock()
return g.result
}