-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
182 lines (164 loc) · 3.84 KB
/
service.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
package service
import (
"context"
"errors"
"sync"
"github.com/antonio-alexander/go-bludgeon/changes/data"
"github.com/antonio-alexander/go-bludgeon/changes/logic"
"github.com/antonio-alexander/go-bludgeon/internal"
"github.com/antonio-alexander/go-bludgeon/internal/config"
"github.com/antonio-alexander/go-bludgeon/internal/kafka"
"github.com/antonio-alexander/go-bludgeon/internal/logger"
)
type kafkaService struct {
sync.RWMutex
sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
logger.Logger
kafka.Client
logic logic.Logic
muHandlers sync.RWMutex
handlers map[string]string
initialized bool
configured bool
config *Configuration
}
func New() interface {
internal.Configurer
internal.Initializer
internal.Parameterizer
} {
return &kafkaService{
handlers: make(map[string]string),
Logger: logger.NewNullLogger(),
}
}
func (k *kafkaService) readTopics() []string {
k.muHandlers.RLock()
defer k.muHandlers.RUnlock()
var topics []string
for topic := range k.handlers {
topics = append(topics, topic)
}
return topics
}
func (k *kafkaService) readHandler(topic string) (string, bool) {
k.muHandlers.RLock()
defer k.muHandlers.RUnlock()
topic, ok := k.handlers[topic]
return topic, ok
}
func (k *kafkaService) writeHandler(topic, handlerId string) {
k.muHandlers.Lock()
defer k.muHandlers.Unlock()
if handlerId != "" {
k.handlers[topic] = handlerId
}
}
func (k *kafkaService) deleteHandler(topic string) {
k.muHandlers.Lock()
defer k.muHandlers.Unlock()
delete(k.handlers, topic)
}
func (k *kafkaService) handleFx(topic string) logic.HandlerFx {
return func(ctx context.Context, handlerId string, changes []*data.Change) error {
if err := k.Client.Publish(topic, data.ToWrapper(
&data.ChangeDigest{Changes: changes},
)); err != nil {
k.Error(logAlias, "error while publishing to topic \"%s\", handler \"%s\": %s", topic, handlerId, err)
return err
}
k.Trace("published change(s) to topic \"%s\", handler \"%s\"", topic, handlerId)
return nil
}
}
func (k *kafkaService) SetUtilities(parameters ...interface{}) {
for _, parameter := range parameters {
switch p := parameter.(type) {
case logger.Logger:
k.Logger = p
}
}
}
func (k *kafkaService) SetParameters(parameters ...interface{}) {
for _, parameter := range parameters {
switch p := parameter.(type) {
case logic.Logic:
k.logic = p
case kafka.Client:
k.Client = p
}
}
switch {
case k.logic == nil:
panic("logic not set")
case k.Client == nil:
panic("kafka client not set")
}
}
func (k *kafkaService) Configure(items ...interface{}) error {
k.Lock()
defer k.Unlock()
var envs map[string]string
var c *Configuration
for _, item := range items {
switch v := item.(type) {
case config.Envs:
envs = v
case *Configuration:
c = v
}
}
if c == nil {
c = new(Configuration)
c.Default()
c.FromEnv(envs)
}
if err := c.Validate(); err != nil {
return err
}
k.config = c
k.configured = true
return nil
}
func (k *kafkaService) Initialize() error {
k.Lock()
defer k.Unlock()
if k.initialized {
return errors.New("already initialized")
}
if !k.configured {
return errors.New("not configured")
}
k.ctx, k.cancel = context.WithCancel(context.Background())
topic := k.config.Topic
handlerId, err := k.logic.HandlerCreate(k.ctx, k.handleFx(topic))
if err != nil {
return err
}
k.writeHandler(topic, handlerId)
k.Info("created handler \"%s\" for topic \"%s\"", handlerId, topic)
k.initialized = true
k.Info(logAlias + "initialized")
return nil
}
func (k *kafkaService) Shutdown() {
k.Lock()
defer k.Unlock()
if !k.initialized {
return
}
k.cancel()
for _, topic := range k.readTopics() {
handlerId, ok := k.readHandler(topic)
if !ok {
continue
}
k.Unsubscribe(topic, handlerId)
k.deleteHandler(topic)
}
k.Wait()
k.initialized = false
k.Info(logAlias + "shutdown")
}