@@ -2,6 +2,7 @@ package event
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
6
7
"os"
7
8
"strconv"
@@ -18,11 +19,13 @@ import (
18
19
)
19
20
20
21
// cache with go cache
21
- var brokersConnectionCache = gocache .New (10 * time .Minute , 6 * time .Hour )
22
- var publicBrokersConnectionCache = []Broker {}
23
- var hostname , cdsname string
24
- var brokers []Broker
25
- var subscribers []chan <- sdk.Event
22
+ var (
23
+ brokersConnectionCache = gocache .New (10 * time .Minute , 6 * time .Hour )
24
+ hostname , cdsname string
25
+ brokers []Broker
26
+ globalBroker Broker
27
+ subscribers []chan <- sdk.Event
28
+ )
26
29
27
30
func init () {
28
31
subscribers = make ([]chan <- sdk.Event , 0 )
@@ -45,30 +48,6 @@ func getBroker(ctx context.Context, t string, option interface{}) (Broker, error
45
48
return nil , fmt .Errorf ("invalid Broker Type %s" , t )
46
49
}
47
50
48
- // ResetPublicIntegrations load all integration of type Event and creates kafka brokers
49
- func ResetPublicIntegrations (ctx context.Context , db * gorp.DbMap ) error {
50
- publicBrokersConnectionCache = []Broker {}
51
- filterType := sdk .IntegrationTypeEvent
52
- integrations , err := integration .LoadPublicModelsByTypeWithDecryption (db , & filterType )
53
- if err != nil {
54
- return sdk .WrapError (err , "cannot load public models for event type" )
55
- }
56
-
57
- for _ , integration := range integrations {
58
- for _ , cfg := range integration .PublicConfigurations {
59
- kafkaCfg := getKafkaConfig (cfg )
60
- kafkaBroker , err := getBroker (ctx , "kafka" , kafkaCfg )
61
- if err != nil {
62
- return sdk .WrapError (err , "cannot get broker for %s and user %s" , cfg ["broker url" ].Value , cfg ["username" ].Value )
63
- }
64
-
65
- publicBrokersConnectionCache = append (publicBrokersConnectionCache , kafkaBroker )
66
- }
67
- }
68
-
69
- return nil
70
- }
71
-
72
51
func getKafkaConfig (cfg sdk.IntegrationConfig ) KafkaConfig {
73
52
kafkaCfg := KafkaConfig {
74
53
Enabled : true ,
@@ -121,7 +100,11 @@ func ResetEventIntegration(ctx context.Context, db gorp.SqlExecutor, eventIntegr
121
100
}
122
101
123
102
// Initialize initializes event system
124
- func Initialize (ctx context.Context , db * gorp.DbMap , cache Store ) error {
103
+ func Initialize (ctx context.Context , db * gorp.DbMap , cache Store , glolbalKafkaConfigs ... KafkaConfig ) error {
104
+ if len (glolbalKafkaConfigs ) > 1 {
105
+ return errors .New ("only one global kafka global config is supported" )
106
+ }
107
+
125
108
store = cache
126
109
var err error
127
110
hostname , err = os .Hostname ()
@@ -138,7 +121,15 @@ func Initialize(ctx context.Context, db *gorp.DbMap, cache Store) error {
138
121
}
139
122
}
140
123
141
- return ResetPublicIntegrations (ctx , db )
124
+ if len (glolbalKafkaConfigs ) == 1 && glolbalKafkaConfigs [0 ].BrokerAddresses != "" {
125
+ globalBroker , err = getBroker (ctx , "kafka" , glolbalKafkaConfigs [0 ])
126
+ if err != nil {
127
+ ctx = log .ContextWithStackTrace (ctx , err )
128
+ log .Error (ctx , "unable to init builtin kafka broker from config: %v" , err )
129
+ }
130
+ }
131
+
132
+ return nil
142
133
}
143
134
144
135
// Subscribe to CDS events
@@ -163,15 +154,17 @@ func DequeueEvent(ctx context.Context, db *gorp.DbMap) {
163
154
s <- e
164
155
}
165
156
166
- // Send into public brokers
167
- for _ , b := range publicBrokersConnectionCache {
168
- if err := b .sendEvent (& e ); err != nil {
157
+ if globalBroker != nil {
158
+ log . Info ( ctx , "sending event %q to global broker" , e . EventType )
159
+ if err := globalBroker .sendEvent (& e ); err != nil {
169
160
log .Warn (ctx , "Error while sending message [%s: %s/%s/%s/%s/%s]: %s" , e .EventType , e .ProjectKey , e .WorkflowName , e .ApplicationName , e .PipelineName , e .EnvironmentName , err )
170
161
}
171
162
}
163
+
172
164
for _ , eventIntegrationID := range e .EventIntegrationsID {
173
165
brokerConnectionKey := strconv .FormatInt (eventIntegrationID , 10 )
174
166
brokerConnection , ok := brokersConnectionCache .Get (brokerConnectionKey )
167
+ var brokerConfig KafkaConfig
175
168
if ! ok {
176
169
projInt , err := integration .LoadProjectIntegrationByIDWithClearPassword (ctx , db , eventIntegrationID )
177
170
if err != nil {
@@ -194,6 +187,7 @@ func DequeueEvent(ctx context.Context, db *gorp.DbMap) {
194
187
continue
195
188
}
196
189
brokerConnection = kafkaBroker
190
+ brokerConfig = kafkaCfg
197
191
}
198
192
199
193
broker , ok := brokerConnection .(Broker )
@@ -203,6 +197,7 @@ func DequeueEvent(ctx context.Context, db *gorp.DbMap) {
203
197
}
204
198
205
199
// Send into external brokers
200
+ log .Info (ctx , "sending event %q to %s" , e .EventType , brokerConfig .BrokerAddresses )
206
201
if err := broker .sendEvent (& e ); err != nil {
207
202
log .Warn (ctx , "Error while sending message [%s: %s/%s/%s/%s/%s]: %s" , e .EventType , e .ProjectKey , e .WorkflowName , e .ApplicationName , e .PipelineName , e .EnvironmentName , err )
208
203
}
0 commit comments