-
Notifications
You must be signed in to change notification settings - Fork 13
/
consumer.go
117 lines (99 loc) · 3.27 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
package queue
import (
"context"
"log"
"github.com/coretrix/hitrix/service"
"github.com/latolukasz/beeorm"
)
type ConsumerOneByModulo interface {
GetMaxModulo() int
Consume(event beeorm.Event) error
GetQueueName(moduloID int) string
GetGroupName(moduloID int, suffix *string) string
}
type ConsumerManyByModulo interface {
GetMaxModulo() int
Consume(events []beeorm.Event) error
GetQueueName(moduloID int) string
GetGroupName(moduloID int, suffix *string) string
}
type ConsumerOne interface {
Consume(event beeorm.Event) error
GetQueueName() string
GetGroupName(suffix *string) string
}
type ConsumerMany interface {
Consume(events []beeorm.Event) error
GetQueueName() string
GetGroupName(suffix *string) string
}
var consumerErrorHandler = func(err error, event beeorm.Event) {
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)
}
}
type ConsumerRunner struct {
ctx context.Context
ormService *beeorm.Engine
}
func NewConsumerRunner(ctx context.Context, ormService *beeorm.Engine) *ConsumerRunner {
return &ConsumerRunner{ctx: ctx, ormService: ormService}
}
func (r *ConsumerRunner) RunConsumerMany(consumer ConsumerMany, groupNameSuffix *string, prefetchCount int) {
eventsConsumer := r.ormService.GetEventBroker().Consumer(consumer.GetGroupName(groupNameSuffix))
eventsConsumer.SetErrorHandler(consumerErrorHandler)
eventsConsumer.Consume(prefetchCount, func(events []beeorm.Event) {
if err := consumer.Consume(events); err != nil {
panic(err)
}
})
}
func (r *ConsumerRunner) RunConsumerOne(consumer ConsumerOne, groupNameSuffix *string, prefetchCount int) {
eventsConsumer := r.ormService.GetEventBroker().Consumer(consumer.GetGroupName(groupNameSuffix))
eventsConsumer.SetErrorHandler(consumerErrorHandler)
eventsConsumer.Consume(prefetchCount, func(events []beeorm.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) {
for moduloID := 1; moduloID <= consumer.GetMaxModulo(); moduloID++ {
currentModulo := moduloID
go func() {
eventsConsumer := r.ormService.GetEventBroker().Consumer(consumer.GetGroupName(currentModulo, groupNameSuffix))
eventsConsumer.SetErrorHandler(consumerErrorHandler)
eventsConsumer.Consume(prefetchCount, func(events []beeorm.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) {
for moduloID := 1; moduloID <= consumer.GetMaxModulo(); moduloID++ {
currentModulo := moduloID
go func() {
eventsConsumer := r.ormService.GetEventBroker().Consumer(consumer.GetGroupName(currentModulo, groupNameSuffix))
eventsConsumer.SetErrorHandler(consumerErrorHandler)
eventsConsumer.Consume(prefetchCount, func(events []beeorm.Event) {
if err := consumer.Consume(events); err != nil {
panic(err)
}
})
}()
}
}