-
Notifications
You must be signed in to change notification settings - Fork 13
/
consumer.go
252 lines (196 loc) · 8.16 KB
/
consumer.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package queue
import (
"context"
"log"
"strings"
"sync"
"time"
"github.com/latolukasz/beeorm"
"github.com/coretrix/hitrix"
"github.com/coretrix/hitrix/service"
)
const (
obtainLockRetryDuration = time.Second
)
type ConsumerOneByModulo interface {
GetMaxModulo() int
Consume(ormService *beeorm.Engine, event beeorm.Event) error
GetQueueName(moduloID int) string
GetGroupName(moduloID int, suffix *string) string
}
type ConsumerManyByModulo interface {
GetMaxModulo() int
Consume(ormService *beeorm.Engine, events []beeorm.Event) error
GetQueueName(moduloID int) string
GetGroupName(moduloID int, suffix *string) string
}
type ConsumerOne interface {
Consume(ormService *beeorm.Engine, event beeorm.Event) error
GetQueueName() string
GetGroupName(suffix *string) string
}
type ConsumerMany interface {
Consume(ormService *beeorm.Engine, events []beeorm.Event) error
GetQueueName() string
GetGroupName(suffix *string) string
}
type ConsumerRunner struct {
ctx context.Context
}
func NewConsumerRunner(ctx context.Context) *ConsumerRunner {
return &ConsumerRunner{ctx: ctx}
}
func (r *ConsumerRunner) RunConsumerMany(consumer ConsumerMany, groupNameSuffix *string, prefetchCount int) {
queueName := consumer.GetQueueName()
log.Printf("RunConsumerMany initialized (%s)", queueName)
ormService := service.DI().OrmEngine().Clone()
eventsConsumer := ormService.GetEventBroker().Consumer(consumer.GetGroupName(groupNameSuffix))
for {
// eventsConsumer.Consume should block and not return anything
// if it returns true => this consumer is exited with no errors, but still not consuming
// if it returns false => this consumer is exited with error "could not obtain lock", so we should retry
if exitedWithNoErrors := eventsConsumer.Consume(r.ctx, prefetchCount, func(events []beeorm.Event) {
log.Printf("We have %d new dirty events in %s", len(events), queueName)
if err := consumer.Consume(ormService, events); err != nil {
panic(err)
}
log.Printf("We consumed %d dirty events in %s", len(events), queueName)
}); !exitedWithNoErrors {
log.Printf("RunConsumerMany failed to start (%s) - retrying in %.1f seconds", queueName, obtainLockRetryDuration.Seconds())
time.Sleep(obtainLockRetryDuration)
continue
} else {
log.Println("eventsConsumer.Consume returned true")
break
}
}
log.Printf("RunConsumerMany exited (%s)", queueName)
}
func (r *ConsumerRunner) RunConsumerOne(consumer ConsumerOne, groupNameSuffix *string, prefetchCount int) {
queueName := consumer.GetQueueName()
log.Printf("RunConsumerOne initialized (%s)", queueName)
ormService := service.DI().OrmEngine().Clone()
eventsConsumer := ormService.GetEventBroker().Consumer(consumer.GetGroupName(groupNameSuffix))
for {
// eventsConsumer.Consume should block and not return anything
// if it returns true => this consumer is exited with no errors, but still not consuming
// if it returns false => this consumer is exited with error "could not obtain lock", so we should retry
if exitedWithNoErrors := eventsConsumer.Consume(r.ctx, prefetchCount, func(events []beeorm.Event) {
log.Printf("We have %d new dirty events in %s", len(events), queueName)
for _, event := range events {
if err := consumer.Consume(ormService, event); err != nil {
panic(err)
}
event.Ack()
}
log.Printf("We consumed %d dirty events in %s", len(events), queueName)
}); !exitedWithNoErrors {
log.Printf("RunConsumerOne failed to start (%s) - retrying in %.1f seconds", queueName, obtainLockRetryDuration.Seconds())
time.Sleep(obtainLockRetryDuration)
continue
} else {
log.Println("eventsConsumer.Consume returned true")
break
}
}
log.Printf("RunConsumerOne exited (%s)", queueName)
}
func (r *ConsumerRunner) RunConsumerOneByModulo(consumer ConsumerOneByModulo, groupNameSuffix *string, prefetchCount int) {
maxModulo := consumer.GetMaxModulo()
baseQueueName := ""
queueNameParts := strings.Split(consumer.GetQueueName(maxModulo), "_")
if len(queueNameParts) > 0 {
baseQueueName = queueNameParts[0]
}
log.Printf("RunConsumerOneByModulo initialized (%s)", baseQueueName)
outerWG := sync.WaitGroup{}
outerWG.Add(maxModulo)
for moduloID := 1; moduloID <= maxModulo; moduloID++ {
innerWG := sync.WaitGroup{}
innerWG.Add(1)
hitrix.GoroutineWithRestart(func() {
currentModulo := moduloID
queueName := consumer.GetQueueName(currentModulo)
consumerGroupName := consumer.GetGroupName(currentModulo, groupNameSuffix)
log.Printf("RunConsumerOneByModulo started goroutine %d (%s)", currentModulo, queueName)
ormService := service.DI().OrmEngine().Clone()
eventsConsumer := ormService.GetEventBroker().Consumer(consumerGroupName)
innerWG.Done()
for {
// eventsConsumer.Consume should block and not return anything
// if it returns true => this consumer is exited with no errors, but still not consuming
// if it returns false => this consumer is exited with error "could not obtain lock", so we should retry
if exitedWithNoErrors := eventsConsumer.Consume(r.ctx, prefetchCount, func(events []beeorm.Event) {
log.Printf("We have %d new dirty events in %s", len(events), consumerGroupName)
for _, event := range events {
if err := consumer.Consume(ormService, event); err != nil {
panic(err)
}
event.Ack()
}
log.Printf("We consumed %d dirty events in %s", len(events), consumerGroupName)
}); !exitedWithNoErrors {
log.Printf("RunConsumerOneByModulo failed to start for goroutine %d (%s) - retrying in %.1f seconds", currentModulo, queueName, obtainLockRetryDuration.Seconds())
time.Sleep(obtainLockRetryDuration)
continue
} else {
log.Printf("eventsConsumer.Consume returned true for goroutine %d (%s)", currentModulo, queueName)
outerWG.Done()
break
}
}
log.Printf("RunConsumerOneByModulo goroutine %d (%s) exited", currentModulo, queueName)
})
innerWG.Wait()
}
outerWG.Wait()
log.Printf("RunConsumerOneByModulo exited (%s)", baseQueueName)
}
func (r *ConsumerRunner) RunConsumerManyByModulo(consumer ConsumerManyByModulo, groupNameSuffix *string, prefetchCount int) {
maxModulo := consumer.GetMaxModulo()
baseQueueName := ""
queueNameParts := strings.Split(consumer.GetQueueName(maxModulo), "_")
if len(queueNameParts) > 0 {
baseQueueName = queueNameParts[0]
}
log.Printf("RunConsumerManyByModulo initialized (%s)", baseQueueName)
outerWG := sync.WaitGroup{}
outerWG.Add(maxModulo)
for moduloID := 1; moduloID <= maxModulo; moduloID++ {
innerWG := sync.WaitGroup{}
innerWG.Add(1)
hitrix.GoroutineWithRestart(func() {
currentModulo := moduloID
queueName := consumer.GetQueueName(currentModulo)
consumerGroupName := consumer.GetGroupName(currentModulo, groupNameSuffix)
log.Printf("RunConsumerManyByModulo started goroutine %d (%s)", currentModulo, queueName)
ormService := service.DI().OrmEngine().Clone()
eventsConsumer := ormService.GetEventBroker().Consumer(consumerGroupName)
innerWG.Done()
for {
// eventsConsumer.Consume should block and not return anything
// if it returns true => this consumer is exited with no errors, but still not consuming
// if it returns false => this consumer is exited with error "could not obtain lock", so we should retry
if exitedWithNoErrors := eventsConsumer.Consume(r.ctx, prefetchCount, func(events []beeorm.Event) {
log.Printf("We have %d new dirty events in %s", len(events), consumerGroupName)
if err := consumer.Consume(ormService, events); err != nil {
panic(err)
}
log.Printf("We consumed %d dirty events in %s", len(events), consumerGroupName)
}); !exitedWithNoErrors {
log.Printf("RunConsumerManyByModulo failed to start for goroutine %d (%s) - retrying in %.1f seconds", currentModulo, queueName, obtainLockRetryDuration.Seconds())
time.Sleep(obtainLockRetryDuration)
continue
} else {
log.Printf("eventsConsumer.Consume returned true for goroutine %d (%s)", currentModulo, queueName)
outerWG.Done()
break
}
}
log.Printf("RunConsumerManyByModulo goroutine %d (%s) exited", currentModulo, queueName)
})
innerWG.Wait()
}
outerWG.Wait()
log.Printf("RunConsumerManyByModulo exited (%s)", baseQueueName)
}