-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchild.go
229 lines (184 loc) · 3.62 KB
/
child.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
package easyworker
import (
"context"
"fmt"
"log"
"sync/atomic"
)
const (
// Always restart child for both case, done task normally or panic.
ALWAYS_RESTART = iota
// Just restart if child got an panic.
ERROR_RESTART
// No restart child for any reason.
NO_RESTART
)
const (
iCHILD_PANIC = iota
iCHILD_TASK_DONE
// Child is standby
STANDBY = iota
// Child is running task.
RUNNING
// Child is restarting.
RESTARTING
// Child is stopped.
STOPPED
// Child is force to quit. In this state child wait for finish task before quit.
FORCE_QUIT
)
var (
// use to store last id of child. id is auto_increment.
lastChildId atomic.Int64
printLog bool
)
func getNewChildId() int64 {
return lastChildId.Add(1)
}
/*
A child struct that hold information a bout task, restart strategy.
*/
type Child struct {
id int64
restart_type int
cmdCh chan msg
state atomic.Int64
restarted atomic.Int64
failed atomic.Int64
fun any
params []any
ctx context.Context
result any
}
/*
Create new child.
*/
func NewChild(restart int, fun any, params ...any) (ret *Child, retErr error) {
if restart < ALWAYS_RESTART || restart > NO_RESTART {
retErr = fmt.Errorf("in correct restart type, input: %d", restart)
return
}
if retErr = verifyFunc(fun); retErr != nil {
return
}
childId := getNewChildId()
ret = &Child{
id: childId,
restart_type: restart,
fun: fun,
params: params,
}
ret.state.Store(STANDBY)
return ret, nil
}
/*
Get child's id.
*/
func (c *Child) Id() int64 {
return c.id
}
/*
Start goroutine to execute task.
*/
func (c *Child) run() {
go c.run_task()
}
/*
Run task.
*/
func (c *Child) run_task() {
defer func() {
msg := msg{
id: int(c.id),
msgType: iCHILD_TASK_DONE,
}
// catch if panic by child code.
if r := recover(); r != nil {
if printLog {
log.Println(c.id, ", worker was panic, ", r)
}
msg.msgType = iCHILD_PANIC
msg.data = r
c.incFailed()
} else {
c.updateState(STOPPED)
}
c.cmdCh <- msg
}()
var err error
c.updateState(RUNNING)
l:
for {
// call user define function.
c.result, err = invokeFun(c.fun, c.params...)
if err != nil {
c.incFailed()
if printLog {
log.Println(c.id, "call user function failed, reason:", err)
}
c.result = err
}
switch c.restart_type {
case ALWAYS_RESTART:
if c.getState() == FORCE_QUIT {
break l
}
case ERROR_RESTART:
if err == nil || c.getState() == FORCE_QUIT {
if printLog {
log.Println(c.id, "done, child no re-run")
}
break l
}
case NO_RESTART:
// always exit.
break l
}
c.incRestarted()
}
}
func (c *Child) stop() {
if printLog {
log.Println(c.id, "force stop")
}
c.updateState(FORCE_QUIT)
}
func (c *Child) updateState(newStatus int) {
c.state.Store(int64(newStatus))
}
func (c *Child) getState() int64 {
return c.state.Load()
}
func (c *Child) canRun() bool {
return int(c.state.Load()) != FORCE_QUIT
}
func (c *Child) incRestarted() {
c.restarted.Store(c.restarted.Add(1))
}
func (c *Child) incFailed() {
c.failed.Store(c.failed.Add(1))
}
func (c *Child) getRestarted() int64 {
return c.restarted.Load()
}
func (c *Child) getFailed() int64 {
return c.failed.Load()
}
/*
Return current status & statistic of Child.
*/
func (c *Child) GetStats() (state int64, restarted int64, failed int64) {
state = c.getState()
restarted = c.getRestarted()
failed = c.getFailed()
return
}
/*
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 (c *Child) GetResult() any {
return c.result
}