-
Notifications
You must be signed in to change notification settings - Fork 1
/
routinepool.go
146 lines (133 loc) · 4.23 KB
/
routinepool.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
// Copyright (C) 2021 WuPeng <wup364@outlook.com>.
// Use of this source code is governed by an MIT-style.
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
package upool
import (
"fmt"
"strconv"
"github.com/wup364/pakku/utils/logs"
"github.com/wup364/pakku/utils/strutil"
)
// Job 负载
type Job interface {
Play()
}
// Worker 流水线
type Worker struct {
Debugger bool
WorkerId string
Workbench chan Job
GWorkbenchQueue chan chan Job
Finished chan bool
}
// NewWorker 新建一条流水线
func NewWorker(WorkbenchQueue chan chan Job, Id string, debugger bool) *Worker {
return &Worker{
Debugger: debugger,
WorkerId: Id,
Workbench: make(chan Job),
GWorkbenchQueue: WorkbenchQueue,
Finished: make(chan bool),
}
}
// Start 开始工作
func (w *Worker) Start() {
go func() {
for {
// 将当前未分配待加工产品的工作台添加到工作台队列中
w.GWorkbenchQueue <- w.Workbench
if w.Debugger {
logs.Infof("Add worker[%s] to workbench\r\n", w.WorkerId)
}
select {
// 接收到了新的WorkerJob
case wJob := <-w.Workbench:
wJob.Play()
case bFinished := <-w.Finished:
if bFinished {
if w.Debugger {
logs.Infof("Worker [%s] closed\r\n", w.WorkerId)
}
return
}
}
}
}()
}
func (w *Worker) Stop() {
// w.QuitChannel <- true
go func() {
w.Finished <- true
}()
}
// Dispatcher 调度器
type Dispatcher struct {
Debugger bool
DispatcherId string // 流水线ID
MaxWorkers int // 流水线上的Worker最大数量
Workers []*Worker // 流水线上所有Worker对象集合
Closed chan bool // 流水线工作状态通道
EndDispatch chan int // 流水线停止工作信号
GJobQueue chan Job // 流水线上的所有待加工Job队列通道
GWorkbenchQueue chan chan Job // 流水线上的所有操作台队列通道
}
// NewDispatcher 初始化调度者
func NewDispatcher(maxWorkers, maxQueue int) *Dispatcher {
Closed := make(chan bool)
EndDispatch := make(chan int)
JobQueue := make(chan Job, maxQueue)
WorkbenchQueue := make(chan chan Job, maxWorkers)
return &Dispatcher{
DispatcherId: strutil.GetUUID(),
MaxWorkers: maxWorkers,
Closed: Closed,
EndDispatch: EndDispatch,
GJobQueue: JobQueue,
GWorkbenchQueue: WorkbenchQueue,
}
}
// Run 开始运行
func (d *Dispatcher) Run() {
for i := 0; i < d.MaxWorkers; i++ {
worker := NewWorker(d.GWorkbenchQueue, fmt.Sprintf("work-%s-%s", strconv.Itoa(i), strutil.GetUUID()), d.Debugger)
if d.Debugger {
logs.Infof("Created worker: %s\r\n", worker.WorkerId)
}
d.Workers = append(d.Workers, worker)
worker.Start()
}
go d.Dispatch()
}
// Dispatch 监控
func (d *Dispatcher) Dispatch() {
FLAG:
for {
select {
case endDispatch := <-d.EndDispatch:
if d.Debugger {
logs.Infof("Dispatcher close signal [%v]", endDispatch)
}
close(d.GJobQueue)
case wJob, Ok := <-d.GJobQueue:
if Ok {
go func(wJob Job) {
// 获取未使用的的工作台 & 将Job放入工作台
Workbench := <-d.GWorkbenchQueue
Workbench <- wJob
}(wJob)
} else {
for _, w := range d.Workers {
w.Stop()
}
d.Closed <- true
break FLAG
}
}
}
}