This repository was archived by the owner on Mar 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupervisor.go
More file actions
259 lines (219 loc) · 4.84 KB
/
supervisor.go
File metadata and controls
259 lines (219 loc) · 4.84 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
package easyworker
import (
"context"
"fmt"
"log"
)
type key int
const (
// Use to get supervisor id from context in user function.
CTX_SUP_ID key = iota
// Use to get child id from context in user function.
CTX_CHILD_ID
)
/*
A supervisor that controll children(workers).
Supervisor privides interface to user with simple APIs.
You can run multi instance supervisor in your application.
*/
type Supervisor struct {
id int64
children map[int64]*Child
cmdCh chan msg
ctx context.Context
}
/*
Create new supervisor.
*/
func NewSupervisor() (ret Supervisor) {
newId := getNewSupId()
ret = Supervisor{
id: newId,
children: make(map[int64]*Child),
cmdCh: make(chan msg),
}
listSup.add(&ret)
ret.start()
return
}
/*
Create new supervisor with context.
*/
func NewSupervisorWithContext(ctx context.Context) (ret Supervisor) {
if ctx == nil {
panic("context for supervisor is nil")
}
ret = NewSupervisor()
ret.ctx = context.WithValue(ctx, CTX_SUP_ID, ret.id)
listSup.add(&ret)
ret.start()
return
}
func (s *Supervisor) GetId() int64 {
return s.id
}
/*
Add directly child to a supervisor.
*/
func (s *Supervisor) NewChild(restart int, fun any, params ...any) (id int64, err error) {
if restart < ALWAYS_RESTART || restart > NO_RESTART {
err = fmt.Errorf("in correct restart type, input: %d", restart)
return
}
if err = verifyFunc(fun); err != nil {
return
}
childId := getNewChildId()
var (
paramsWithCtx []any
ctx context.Context
)
// add context to first param of task
if s.ctx != nil {
ctx = context.WithValue(s.ctx, CTX_CHILD_ID, childId)
paramsWithCtx = make([]any, len(params)+1)
paramsWithCtx[0] = ctx
copy(paramsWithCtx[1:], params)
} else {
paramsWithCtx = params
ctx = nil
}
child := &Child{
id: childId,
restart_type: restart,
fun: fun,
params: paramsWithCtx,
ctx: ctx,
}
s.children[child.id] = child
child.cmdCh = s.cmdCh
// start child to run task.
child.run()
id = child.id
return
}
/*
Add existed child to supervisor.
A child can add to run in one or more supervisor.
*/
func (s *Supervisor) AddChild(child *Child) {
s.children[child.id] = child
child.cmdCh = s.cmdCh
if s.ctx != nil {
// add context to first param of task
ctx := context.WithValue(s.ctx, CTX_CHILD_ID, child.id)
paramsWithCtx := make([]any, len(child.params)+1)
paramsWithCtx[0] = ctx
copy(paramsWithCtx[1:], child.params)
child.params = paramsWithCtx
}
child.run()
}
/*
Remove a child to out of supervisor.
*/
func (s *Supervisor) RemoveChild(child *Child) {
if child != nil {
if child.getState() == RUNNING {
child.stop()
}
delete(s.children, child.id)
}
}
/*
Remove a child to out of supervisor by child id.
*/
func (s *Supervisor) RemoveChildById(id int64) {
if child, existed := s.children[id]; existed {
if child.getState() == RUNNING {
child.stop()
}
delete(s.children, child.id)
}
}
/*
Get child from id.
Return nil if id isn't existed.
*/
func (s *Supervisor) GetChild(id int64) *Child {
child := s.children[id]
return child
}
/*
Make a goroutine to handle event from children. Restart children if needed.
*/
func (s *Supervisor) start() {
go func() {
var (
child *Child
)
for {
event := <-s.cmdCh
switch event.msgType {
case iCHILD_PANIC:
child = s.children[int64(event.id)]
if child.canRun() && (child.restart_type == ALWAYS_RESTART || child.restart_type == ERROR_RESTART) {
child.updateState(RESTARTING)
if printLog {
log.Println("restarting child:", child.id)
}
child.run()
} else {
if printLog {
log.Println("child:", child.id, "stopped")
}
child.updateState(STOPPED)
}
}
}
}()
}
/*
Supervisor will send stop signal to children.
Children after process your function will check the signal and stop.
In this case, ALWAYS_RESTART & ERROR_RESTART will be ignored.
*/
func (s *Supervisor) Stop() {
for _, child := range s.children {
child.stop()
}
}
/*
Supervisor will send stop signal to a child.
Child after process your function will check the signal and stop.
In this case, ALWAYS_RESTART & ERROR_RESTART will be ignored.
*/
func (s *Supervisor) StopChild(id int64) {
if child, existed := s.children[id]; existed {
child.stop()
}
}
/*
Clear all children. Call after Stop.
*/
func (s *Supervisor) Done() {
for k := range s.children {
delete(s.children, k)
}
}
/*
Return statistics of supervisor.
total: Number of children in supervisor.
running: Number of children are running.
stopped: Number of children are stopped.
restarting: Number of children are restarting.
*/
func (s *Supervisor) Stats() (total, running, stopped, restarting int) {
total = len(s.children)
for _, child := range s.children {
switch child.getState() {
case RUNNING:
running++
case RESTARTING:
restarting++
default:
stopped++
}
}
return
}