-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
230 lines (213 loc) · 4.98 KB
/
queue.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
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
package mfworker
import (
"github.com/iflamed/mfworker/job"
mflog "github.com/iflamed/mfworker/log"
"github.com/iflamed/mfworker/storage"
"log"
"sync"
"time"
)
type Queue struct {
sync.RWMutex
PersistPath string
MaxItemsInMem uint
WorkerCount uint
mstore *storage.MemoryStorage
pstore storage.Bucket
jobChan chan *job.Job
stopChan chan bool
workers []*worker
handlers map[string]func(job *job.Job)
Logger mflog.Logger
}
type worker struct {
id uint
Quit chan bool
wg *sync.WaitGroup
}
func NewQueue(count, maxItems uint, path, queueName string, logger mflog.Logger) *Queue {
q := &Queue{}
q.PersistPath = path
q.MaxItemsInMem = maxItems
q.WorkerCount = count
q.mstore = storage.NewMemoryStorage(q.MaxItemsInMem)
if path != "" {
if queueName == "" {
queueName = "mfworker"
}
q.pstore = storage.NewDiskQueueStorage(path, queueName, maxItems, logger)
if q.pstore != nil {
q.Logger = q.pstore.GetLogger()
}
}
q.jobChan = make(chan *job.Job, q.WorkerCount)
q.stopChan = make(chan bool, 1)
q.handlers = map[string]func(job *job.Job){}
return q
}
func (s *Queue) Dispatch(job *job.Job) bool {
s.Lock()
defer s.Unlock()
if s.pstore != nil && (s.pstore.Length() > 0 || s.mstore.Length() >= s.MaxItemsInMem) {
if job.Id != "" {
s.pstore.PushJob([]byte(job.Id), job.ToJson())
} else {
s.pstore.Push(job.ToJson())
}
} else if s.mstore.Length() < s.MaxItemsInMem {
s.mstore.Push(job.ToJson())
} else {
return false
}
return true
}
func (s *Queue) DispatchJobs(jobs []*job.Job) bool {
s.Lock()
defer s.Unlock()
var batchJobs []*job.Job
for _, item := range jobs {
if s.pstore != nil && (s.pstore.Length() > 0 || s.mstore.Length() >= s.MaxItemsInMem) {
batchJobs = append(batchJobs, item)
} else if s.mstore.Length() < s.MaxItemsInMem {
s.mstore.Push(item.ToJson())
}
}
if s.pstore != nil && (s.pstore.Length() > 0 || s.mstore.Length() >= s.MaxItemsInMem) {
if len(batchJobs) > 0 {
s.pstore.PushJobs(batchJobs)
}
} else {
return false
}
return true
}
func (s *Queue) Start() {
s.startWorker()
s.startDispatcher()
}
func (s *Queue) startWorker() {
var count uint
for count < s.WorkerCount {
count++
woker := &worker{
Quit: make(chan bool, 1),
id: count,
}
s.workers = append(s.workers, woker)
go func(w *worker) {
for {
select {
case item := <-s.jobChan:
if item != nil {
s.processJob(item)
}
case <-w.Quit:
w.wg.Done()
s.Debugf("Worker %d has been quit.", w.id)
return
}
}
}(woker)
}
}
func (s *Queue) startDispatcher() {
go func() {
for {
select {
case <-s.stopChan:
close(s.jobChan)
s.Debugf("The task dispatcher has been stop.")
return
default:
s.Lock()
if s.pstore != nil && s.pstore.Length() > 0 && s.mstore.Length() < s.MaxItemsInMem {
s.mstore.Push(s.pstore.Shift())
}
jobBytes := s.mstore.Shift()
s.Unlock()
if jobBytes != nil {
item := job.NewJobFromJSON(jobBytes)
s.jobChan <- item
} else {
time.Sleep(100 * time.Millisecond)
}
}
}
}()
}
func (s *Queue) processJob(job *job.Job) {
for key, handler := range s.handlers {
if key == job.Name {
handler(job)
// job process finished.
job = nil
return
}
}
}
func (s *Queue) Stop() {
// stop the dispatcher
s.stopChan <- true
// stop all worker
var wg sync.WaitGroup
go func() {
for _, worker := range s.workers {
wg.Add(1)
worker.wg = &wg
worker.Quit <- true
close(worker.Quit)
}
}()
wg.Wait()
close(s.stopChan)
s.persistJobs()
}
func (s *Queue) persistJobs() {
// jobs channel buffer should persist into file
if s.pstore != nil {
s.Debugf("The persist storage length is %d \n", s.pstore.Length())
for len(s.jobChan) > 0 {
s.Debugf("The job chan buffer length is %d \n", len(s.jobChan))
item := <-s.jobChan
s.pstore.Push(item.ToJson())
}
s.Debugf("The persist storage length is %d \n", s.pstore.Length())
s.Debugf("The memory storage length is %d \n", s.mstore.Length())
// jobs in memory not processed should persist into file
for s.mstore.Length() > 0 {
s.pstore.Push(s.mstore.Shift())
}
s.Debugf("The persist storage length is %d \n", s.pstore.Length())
s.pstore.Close()
}
s.Debugf("The memory storage length is %d \n", s.mstore.Length())
if len(s.jobChan) > 0 {
s.Debugf("The %d jobs in buffer channel lose. \n", len(s.jobChan))
}
if s.mstore.Length() > 0 {
s.Debugf("The %d jobs in memory storage lose. \n", s.mstore.Length())
}
s.mstore.Close()
}
func (s *Queue) Handler(name string, fn func(job *job.Job)) {
s.Lock()
s.handlers[name] = fn
s.Unlock()
}
func (s *Queue) CountPendingJobs() (num uint64) {
s.RLock()
defer s.RUnlock()
if s.pstore != nil {
num = num + s.pstore.Length()
}
num = num + uint64(len(s.jobChan))
num = num + uint64(s.mstore.Length())
return
}
func (s *Queue) Debugf(format string, v ...interface{}) {
if s.Logger != nil {
s.Logger.Debugf(format, v...)
} else {
log.Printf(format, v...)
}
}