-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
168 lines (141 loc) · 3.17 KB
/
manager.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
package sanji
import (
"errors"
"strings"
"sync"
"sync/atomic"
"github.com/bitleak/lmstfy/client"
)
var ManagerStartedErr = errors.New("Luffy manager has started")
type imanager interface {
start()
quitPrepare()
quit()
manage()
loadWorkers()
prcessingWorkerSize() int
getQueues() string
reset()
}
type HandlersChain []JobFunc
type manager struct {
wait *sync.WaitGroup
captain *Luffy
fetch ifetcher
queue string
queues []string
queuesStr string
is_priority bool
is_started int32
concurrency int
jobTTR uint32
job JobFunc
handlers HandlersChain
workers []iworker
workersMutex sync.Mutex
confirm chan *client.Job
stop chan struct{}
exit chan struct{}
}
func (m *manager) insertMid(mids ...JobFunc) {
m.handlers = append(m.handlers, mids...)
}
func (m *manager) start() {
if m.started() {
return
}
atomic.StoreInt32(&m.is_started, 1)
m.wait.Add(1)
m.insertMid(m.job)
m.loadWorkers()
go m.manage()
}
func (m *manager) started() bool {
return atomic.LoadInt32(&m.is_started) > 0
}
func (m *manager) quitPrepare() {
if !m.fetch.Closed() {
m.fetch.Close()
}
}
func (m *manager) quit() {
Logger.Println("quitting queue", m.queue, ", waiting for ", m.prcessingWorkerSize(), "/", len(m.workers), " workers")
m.quitPrepare()
m.workersMutex.Lock()
for _, worker := range m.workers {
worker.quit()
}
m.workersMutex.Unlock()
m.stop <- struct{}{}
<-m.exit
m.reset()
m.wait.Done()
}
func (m *manager) manage() {
Logger.Println("prcessing namespace", m.captain.config.Namespace, " queue", m.queue, " with ", m.concurrency, " workers.")
for _, worker := range m.workers {
worker.start()
}
go m.fetch.Fetch()
for {
select {
case job := <-m.confirm:
m.fetch.Acknowledge(job)
case <-m.stop:
m.exit <- struct{}{}
break
}
}
}
func (m *manager) loadWorkers() {
m.workersMutex.Lock()
for i := 0; i < m.concurrency; i++ {
m.workers[i] = newWorker(m)
}
m.workersMutex.Unlock()
}
func (m *manager) prcessingWorkerSize() int {
count := 0
for _, worker := range m.workers {
if worker.processing() {
count++
}
}
return count
}
func (m *manager) getQueues() string {
return m.queuesStr
}
func (m *manager) reset() {
m.fetch = newFetcher(m, m.queuesStr, make(chan *client.Job), make(chan struct{}))
}
func newManager(captain *Luffy, queue string, job JobFunc, concurrency int, priority bool, ttr uint32) *manager {
m := &manager{
wait: new(sync.WaitGroup),
captain: captain,
queue: queue,
job: job,
jobTTR: ttr,
concurrency: concurrency,
is_priority: priority,
workers: make([]iworker, concurrency),
confirm: make(chan *client.Job),
stop: make(chan struct{}),
exit: make(chan struct{}),
handlers: captain.handlers,
}
if m.is_priority {
m.queues = []string{
m.captain.priorityQueue(m.queue, PriorityLevelHight),
m.captain.priorityQueue(m.queue, PriorityLevelMid),
m.captain.priorityQueue(m.queue, PriorityLevelLow),
}
m.queuesStr = strings.Join(m.queues, ",")
} else {
qStr := m.captain.priorityQueue(m.queue, PriorityLevelHight)
m.queues = []string{qStr}
m.queuesStr = qStr
}
m.reset()
return m
}