Skip to content
This repository was archived by the owner on Mar 5, 2026. It is now read-only.

Commit 7b62d04

Browse files
committed
update doc & test case
1 parent a209658 commit 7b62d04

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Currently, child has three type of restart strategy:
6868
* NO_RESTART, supervisor will don't restart children for any reason.
6969

7070
Children will be started after they are added to supervisor.
71+
Chid can be added many times to many supervisor but child can control only by the last supervior.
7172

7273
In restart case, children will re-use last parameters (if task don't change it) of task.
7374

@@ -118,11 +119,12 @@ sup := easyworker.NewSupervisor()
118119

119120
// add direct child to supervisor.
120121
sup.NewChild(easyworker.ERROR_RESTART, loop, 5)
121-
sup.NewChild(easyworker.NO_RESTART, loopWithPanic, 5, "test panic")
122-
122+
sup.NewChild(easyworker.NO_RESTART, func() {
123+
fmt.Println("Hello")
124+
})
123125

124126
// create a child.
125-
child, _ := easyworker.NewChild(easyworker.ALWAYS_RESTART, loopWithPanic, 5, "other panic")
127+
child, _ := easyworker.NewChild(easyworker.ALWAYS_RESTART, loopWithPanic, 5, "test panic")
126128

127129
// add exists child.
128130
sup.AddChild(&child)
@@ -171,8 +173,6 @@ sup := NewSupervisorWithContext(context.Background())
171173
sup.NewChild(easyworker.NO_RESTART, loopWithContext, 10)
172174
```
173175

174-
For other APIs please go to [pkg.go](https://pkg.go.dev/github.com/manhvu/easyworker)
175-
176176
### EasyTask
177177

178178
This is simple way to run parallel tasks.
@@ -352,3 +352,5 @@ if sig.Signal != easyWorker.SIGNAL_DONE {
352352
g.Run()
353353
}
354354
```
355+
356+
For other APIs please go to [pkg.go](https://pkg.go.dev/github.com/manhvu/easyworker)

child.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ type Child struct {
6262
fun any
6363
params []any
6464
ctx context.Context
65+
66+
result []any
6567
}
6668

6769
/*
@@ -131,7 +133,7 @@ func (c *Child) run_task() {
131133
l:
132134
for {
133135
// call user define function.
134-
_, err = invokeFun(c.fun, c.params...)
136+
c.result, err = invokeFun(c.fun, c.params...)
135137

136138
if err != nil {
137139
c.incFailed()
@@ -201,3 +203,13 @@ func (c *Child) GetStats() (status int64, restarted int64, failed int64) {
201203

202204
return
203205
}
206+
207+
/*
208+
Get result from last run.
209+
Result is slice of any.
210+
Length of slice is number of parameter return from user function.
211+
Cast type to get right value.
212+
*/
213+
func (c *Child) GetResult() []any {
214+
return c.result
215+
}

gomonitor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (g *Go) run_task() {
216216
Return state of Go.
217217
Kind of state:
218218
- RUNNING: Task is running.
219-
- STOPPED: Stop by user. In this state user can run again.
219+
- STOPPED: Stop by user. In this state user cannot run again.
220220
- STANDBY: Task is standby wait for start or just done task.
221221
*/
222222
func (g *Go) State() int64 {
@@ -227,7 +227,7 @@ func (g *Go) State() int64 {
227227
Get result from last run.
228228
Result is slice of any.
229229
Length of slice is number of parameter return from user function.
230-
Cast type to get right value.
230+
Cast to right type for value.
231231
*/
232232
func (g *Go) GetResult() []any {
233233
g.lock.Lock()

supervisor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import (
99
type key int
1010

1111
const (
12+
// Use to get supervisor id from context.
1213
CTX_SUP_ID key = iota
14+
15+
// Use to get child id from context.
1316
CTX_CHILD_ID
1417
)
1518

@@ -103,6 +106,7 @@ func (s *Supervisor) NewChild(restart int, fun any, params ...any) (id int64, er
103106
s.children[child.id] = child
104107
child.cmdCh = s.cmdCh
105108

109+
// start child to run task.
106110
child.run()
107111

108112
id = child.id

supervisor_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestSupAlwaysRestart1(t *testing.T) {
6969

7070
sup := NewSupervisor()
7171

72-
child, _ := NewChild(ALWAYS_RESTART, loopRun, 5, ch)
72+
child, _ := NewChild(ALWAYS_RESTART, loopRun, 15, ch)
7373

7474
sup.AddChild(&child)
7575

@@ -254,23 +254,25 @@ func TestSupStopChild(t *testing.T) {
254254
func TestSupMultiWorkers(t *testing.T) {
255255
sup := NewSupervisor()
256256

257-
for i := 0; i < 100; i++ {
258-
sup.NewChild(ALWAYS_RESTART, simpleLoopWithPanic, i)
257+
num := 500
258+
259+
for i := 0; i < num; i++ {
260+
sup.NewChild(ALWAYS_RESTART, simpleLoopWithPanic, 5)
259261
}
260262

261-
for i := 0; i < 100; i++ {
262-
sup.NewChild(ERROR_RESTART, simpleLoop, i)
263+
for i := 0; i < num; i++ {
264+
sup.NewChild(ERROR_RESTART, simpleLoop, 5)
263265
}
264266

265-
time.Sleep(2 * time.Second)
267+
time.Sleep(3 * time.Second)
266268

267269
total, running, stopped, restarting := sup.Stats()
268270

269271
sup.Stop()
270272

271273
fmt.Printf("Total: %d, Running: %d, Stopped: %d, Restarting: %d\n", total, running, stopped, restarting)
272274

273-
if stopped != 100 || restarting+running != 100 {
275+
if stopped != num || restarting+running != num {
274276
t.Error("has children status failed")
275277
}
276278
}

0 commit comments

Comments
 (0)