-
Notifications
You must be signed in to change notification settings - Fork 0
/
point3-quman.go
190 lines (141 loc) · 3.95 KB
/
point3-quman.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
package quman
import (
"context"
"sync"
qmanErr "github.com/violetpay-org/point3-quman/errors"
qmanservices "github.com/violetpay-org/point3-quman/services"
)
// Main package for Quman (Queue Manager) service
type MainQueueFactory struct {
queueSet QueueSet
wg *sync.WaitGroup
ctx *context.Context
}
func NewQueueFactory(wg *sync.WaitGroup, ctx *context.Context) *MainQueueFactory {
return &MainQueueFactory{
queueSet: make(QueueSet),
wg: wg,
ctx: ctx,
}
}
func (f *MainQueueFactory) RegisterQueueName(name string, index int) (qmanservices.QueueName, error) {
return qmanservices.RegisterQueueName(name, index)
}
func (f *MainQueueFactory) GetWaitGroup() (*sync.WaitGroup, error) {
if f.wg == nil {
return nil, qmanErr.ErrQueueFactoryWaitGroupNil()
}
return f.wg, nil
}
func (f *MainQueueFactory) GetQueueService(queueName qmanservices.QueueName) (qmanservices.IQueueService, error) {
queue := f.queueSet[queueName]
if queue == nil {
return nil, qmanErr.ErrQueueNotFound(queueName.GetQueueName())
}
return queue.QueueService, nil
}
func (f *MainQueueFactory) RunQueue(queueName qmanservices.QueueName) (*sync.WaitGroup, error) {
if f.wg == nil {
return nil, qmanErr.ErrQueueFactoryWaitGroupNil()
}
queue := f.queueSet[queueName]
if queue == nil {
return f.wg, qmanErr.ErrQueueNotFound(queueName.GetQueueName())
}
queueOperator := queue.LowLevelQueueOperator
queueCallbacks := queue.ConsumeCallback
if (queueOperator == nil) || (queueCallbacks == nil) {
return f.wg, qmanErr.ErrQueueNotPrepared(queueName.GetQueueName())
}
err := queueOperator.StartQueue(
queueCallbacks,
f.wg,
f.ctx,
)
return f.wg, err
}
func (f *MainQueueFactory) StopQueue(queueName qmanservices.QueueName) (*sync.WaitGroup, error) {
if f.wg == nil {
return nil, qmanErr.ErrQueueFactoryWaitGroupNil()
}
queue := f.queueSet[queueName]
if queue == nil {
return f.wg, qmanErr.ErrQueueNotFound(queueName.GetQueueName())
}
queueOperator := queue.LowLevelQueueOperator
queueCallbacks := queue.StopCallback
if queueOperator == nil || queueCallbacks == nil {
return f.wg, qmanErr.ErrQueueNotPrepared(queueName.GetQueueName())
}
err := queueOperator.StopQueue(queueCallbacks)
return f.wg, err
}
func (f *MainQueueFactory) AddQueue(queueName qmanservices.QueueName, queue *Queue) error {
if f.queueSet[queueName] != nil {
return qmanErr.ErrDuplicateQueue()
}
f.queueSet[queueName] = queue
return nil
}
func (f *MainQueueFactory) AddAllQueues(queueSet QueueSet) error {
// Check for duplicates
for queueName := range queueSet {
if f.queueSet[queueName] != nil {
return qmanErr.ErrDuplicateQueue()
}
}
// Add all queues
for queueName, queue := range queueSet {
f.AddQueue(queueName, queue)
}
return nil
}
func (f *MainQueueFactory) UpsertQueue(queueName qmanservices.QueueName, queue *Queue) error {
_, err := f.GetQueueService(queueName)
if err != nil {
return f.AddQueue(queueName, queue)
}
_, err = f.StopQueue(queueName)
if err != nil {
return err
}
err = f.AddQueue(queueName, queue)
if err != nil {
return err
}
return nil
}
func (f *MainQueueFactory) RunAllQueues() (*sync.WaitGroup, error) {
var erroredQueue interface{}
queuesExecuted := []qmanservices.QueueName{}
for queueName := range f.queueSet {
_, err := f.RunQueue(queueName)
if err != nil {
erroredQueue = queueName
break
}
queuesExecuted = append(queuesExecuted, queueName)
}
if erroredQueue != nil {
for _, queueName := range queuesExecuted {
_, _ = f.StopQueue(queueName)
}
return f.wg, qmanErr.ErrQueueNotPrepared(erroredQueue.(qmanservices.QueueName).GetQueueName())
}
return f.wg, nil
}
func (f *MainQueueFactory) StopAllQueues() (*sync.WaitGroup, error) {
var erroredQueue interface{}
var err error
for queueName := range f.queueSet {
_, err = f.StopQueue(queueName)
if err != nil {
erroredQueue = queueName
break
}
}
if erroredQueue != nil {
return f.wg, err
}
return f.wg, nil
}