-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_mission.go
97 lines (75 loc) · 1.49 KB
/
ex_mission.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
package main
import (
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/l4go/task"
"github.com/l4go/timer"
)
func init() {
log.SetFlags(log.Lmicroseconds)
}
const (
WORKERS = 3
SUBWORKERS = 3
)
func main() {
log.Println("START")
defer log.Println("END")
m := task.NewMission()
defer m.Done()
signal_ch := make(chan os.Signal, 1)
signal.Notify(signal_ch, syscall.SIGINT, syscall.SIGTERM)
time.AfterFunc(10*time.Second, m.Cancel)
for w := 0; w < WORKERS; w++ {
go worker(m.New(), w)
}
select {
case <-m.Recv():
case <-signal_ch:
m.Cancel()
}
}
func worker(m *task.Mission, w int) {
log.Println("worker", w, "START")
defer log.Println("worker", w, "END")
defer m.Done()
for sw := 0; sw < SUBWORKERS; sw++ {
if task.IsCanceled(m) {
return
}
go subworker(m.New(), w, sw)
}
}
func subworker(m *task.Mission, w int, sw int) {
log.Println("subworker", w, sw, "START")
defer log.Println("subworker", w, sw, "END")
defer m.Done()
work_time := time.Duration(w) * time.Second
if task.IsCanceled(m) {
return
}
wt := timer.NewTimer()
defer wt.Stop()
wt.Start(work_time)
job(m.NewCancel(), w, sw)
select {
case <-wt.Recv():
case <-m.RecvCancel():
}
}
func job(cc task.Canceller, w int, sw int) {
log.Println("job", w, sw, "START")
defer log.Println("job", w, sw, "END")
defer cc.Cancel()
job_time := time.Duration(w) * time.Second / 2
tt := timer.NewTimer()
defer tt.Stop()
tt.Start(job_time)
select {
case <-tt.Recv():
case <-cc.RecvCancel():
}
}