-
Notifications
You must be signed in to change notification settings - Fork 13
/
consumer.go
122 lines (104 loc) · 3.63 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
package queue
import (
"context"
"log"
"github.com/coretrix/hitrix/service"
"github.com/latolukasz/orm"
)
type ConsumerOneByModulo interface {
GetMaxModulo() int
Consume(event orm.Event) error
GetQueueName(moduloID int) string
GetGroupName(moduloID int, suffix *string) string
}
type ConsumerManyByModulo interface {
GetMaxModulo() int
Consume(events []orm.Event) error
GetQueueName(moduloID int) string
GetGroupName(moduloID int, suffix *string) string
}
type ConsumerOne interface {
Consume(event orm.Event) error
GetQueueName() string
GetGroupName(suffix *string) string
}
type ConsumerMany interface {
Consume(events []orm.Event) error
GetQueueName() string
GetGroupName(suffix *string) string
}
var consumerErrorHandler = func(err interface{}, event orm.Event) error {
defer func() {
if newError := recover(); newError != nil {
log.Printf("Error: %v\nNew Error: %v", err, newError)
}
}()
errorLoggerService, has := service.DI().ErrorLogger()
if has {
errorLoggerService.LogError(err)
}
return nil
}
type ConsumerRunner struct {
ctx context.Context
ormService *orm.Engine
}
func NewConsumerRunner(ctx context.Context, ormService *orm.Engine) *ConsumerRunner {
return &ConsumerRunner{ctx: ctx, ormService: ormService}
}
func (r *ConsumerRunner) RunConsumerMany(consumer ConsumerMany, groupNameSuffix *string, prefetchCount int, blocking bool) {
eventsConsumer := r.ormService.GetEventBroker().Consumer(consumer.GetQueueName(), consumer.GetGroupName(groupNameSuffix))
eventsConsumer.SetLimit(10)
eventsConsumer.SetErrorHandler(consumerErrorHandler)
eventsConsumer.Consume(r.ctx, prefetchCount, blocking, func(events []orm.Event) {
if err := consumer.Consume(events); err != nil {
panic(err)
}
})
}
func (r *ConsumerRunner) RunConsumerOne(consumer ConsumerOne, groupNameSuffix *string, prefetchCount int, blocking bool) {
eventsConsumer := r.ormService.GetEventBroker().Consumer(consumer.GetQueueName(), consumer.GetGroupName(groupNameSuffix))
eventsConsumer.SetLimit(10)
eventsConsumer.SetErrorHandler(consumerErrorHandler)
eventsConsumer.Consume(r.ctx, prefetchCount, blocking, func(events []orm.Event) {
for _, event := range events {
if err := consumer.Consume(event); err != nil {
panic(err)
}
event.Ack()
}
})
}
func (r *ConsumerRunner) RunConsumerOneByModulo(consumer ConsumerOneByModulo, groupNameSuffix *string, prefetchCount int, blocking bool) {
for moduloID := 1; moduloID <= consumer.GetMaxModulo(); moduloID++ {
currentModulo := moduloID
go func() {
eventsConsumer := r.ormService.GetEventBroker().Consumer(consumer.GetQueueName(currentModulo), consumer.GetGroupName(currentModulo, groupNameSuffix))
eventsConsumer.SetLimit(10)
eventsConsumer.SetErrorHandler(consumerErrorHandler)
eventsConsumer.Consume(r.ctx, prefetchCount, blocking, func(events []orm.Event) {
for _, event := range events {
if err := consumer.Consume(event); err != nil {
panic(err)
}
event.Ack()
}
})
}()
}
}
func (r *ConsumerRunner) RunConsumerManyByModulo(consumer ConsumerManyByModulo, groupNameSuffix *string, prefetchCount int, blocking bool) {
for moduloID := 1; moduloID <= consumer.GetMaxModulo(); moduloID++ {
currentModulo := moduloID
go func() {
eventsConsumer := r.ormService.GetEventBroker().Consumer(consumer.GetQueueName(currentModulo), consumer.GetGroupName(currentModulo, groupNameSuffix))
eventsConsumer.SetLimit(10)
eventsConsumer.SetErrorHandler(consumerErrorHandler)
eventsConsumer.Consume(r.ctx, prefetchCount, blocking, func(events []orm.Event) {
if err := consumer.Consume(events); err != nil {
panic(err)
}
})
}()
}
}