-
Notifications
You must be signed in to change notification settings - Fork 270
/
service.go
271 lines (226 loc) · 7 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package sources
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/hashicorp/go-multierror"
"github.com/jitsucom/jitsu/server/destinations"
"github.com/jitsucom/jitsu/server/drivers"
driversbase "github.com/jitsucom/jitsu/server/drivers/base"
"github.com/jitsucom/jitsu/server/logging"
"github.com/jitsucom/jitsu/server/meta"
"github.com/jitsucom/jitsu/server/resources"
"github.com/jitsucom/jitsu/server/scheduling"
"github.com/jitsucom/jitsu/server/telemetry"
"github.com/spf13/viper"
)
const serviceName = "sources"
const marshallingErrorMsg = `
Error initializing source: wrong config format: each source must contains one key and config as a value(see https://docs.eventnative.dev/configuration) e.g.
sources:
custom_name:
type: google_play
...
`
//Service keep up-to-date sources
type Service struct {
sync.RWMutex
ctx context.Context
sources map[string]*Unit
destinationsService *destinations.Service
cronScheduler *scheduling.CronScheduler
configured bool
}
//NewTestService is used only for tests
func NewTestService() *Service {
return &Service{}
}
//NewService returns initialized Service instance
//or error if occurred
func NewService(ctx context.Context, sources *viper.Viper, sourcesURL string, destinationsService *destinations.Service, metaStorage meta.Storage,
cronScheduler *scheduling.CronScheduler) (*Service, error) {
service := &Service{
ctx: ctx,
sources: map[string]*Unit{},
destinationsService: destinationsService,
cronScheduler: cronScheduler,
}
if sources == nil && sourcesURL == "" {
logging.Warnf("❌ Sources aren't configured")
return service, nil
}
reloadSec := viper.GetInt("server.sources_reload_sec")
if reloadSec == 0 {
return nil, errors.New("server.sources_reload_sec can't be empty")
}
if metaStorage.Type() == meta.DummyType {
return nil, errors.New("Meta storage is required")
}
service.configured = true
if sources != nil {
sc := map[string]driversbase.SourceConfig{}
if err := sources.Unmarshal(&sc); err != nil {
logging.Error(marshallingErrorMsg, err)
return service, nil
}
service.init(sc)
if len(service.sources) == 0 {
logging.Info("Sources are empty")
}
} else {
if strings.HasPrefix(sourcesURL, "http://") || strings.HasPrefix(sourcesURL, "https://") {
resources.Watch(serviceName, sourcesURL, resources.LoadFromHTTP, service.updateSources, time.Duration(reloadSec)*time.Second)
} else if strings.Contains(sourcesURL, "file://") || strings.HasPrefix(sourcesURL, "/") {
resources.Watch(serviceName, strings.Replace(sourcesURL, "file://", "", 1), resources.LoadFromFile, service.updateSources, time.Duration(reloadSec)*time.Second)
} else if strings.HasPrefix(sourcesURL, "{") && strings.HasSuffix(sourcesURL, "}") {
service.updateSources([]byte(sourcesURL))
} else {
return nil, errors.New("Unknown sources configuration: " + sourcesURL)
}
}
return service, nil
}
func (s *Service) updateSources(payload []byte) {
dc, err := parseFromBytes(payload)
if err != nil {
logging.Error(marshallingErrorMsg, err)
return
}
s.init(dc)
if len(s.sources) == 0 {
logging.Info("Sources are empty")
}
}
func (s *Service) init(sc map[string]driversbase.SourceConfig) {
StatusInstance.Reloading = true
//close and remove non-existent (in new config)
toDelete := map[string]*Unit{}
s.RLock()
for sourceID, unit := range s.sources {
_, ok := sc[sourceID]
if !ok {
toDelete[sourceID] = unit
}
}
s.RUnlock()
if len(toDelete) > 0 {
s.Lock()
for sourceIDToDel, unit := range toDelete {
s.remove(sourceIDToDel, unit)
}
s.Unlock()
}
for sourceName, config := range sc {
//common case
sourceConfig := config
name := sourceName
if len(sourceConfig.Destinations) == 0 {
logging.Warnf("[%s] Skipping source: no destinations mapped to the source", name)
continue
}
hash, err := resources.GetHash(config)
if err != nil {
logging.SystemErrorf("Error getting hash from [%s] source: %v. Source will be skipped!", name, err)
continue
}
s.RLock()
unit, ok := s.sources[name]
s.RUnlock()
if ok {
if unit.hash == hash {
//source wasn't changed
continue
}
//remove old (for recreation)
s.Lock()
s.remove(name, unit)
s.Unlock()
}
driverPerCollection, err := drivers.Create(s.ctx, name, &sourceConfig, s.cronScheduler)
if err != nil {
logging.Errorf("[%s] Error initializing source of type %s: %v", name, sourceConfig.Type, err)
continue
}
s.Lock()
s.sources[name] = &Unit{
SourceID: sourceConfig.SourceID,
SourceType: sourceConfig.Type,
DriverPerCollection: driverPerCollection,
DestinationIDs: sourceConfig.Destinations,
PostHandleDestinationIDs: sourceConfig.PostHandleDestinations,
Notifications: sourceConfig.Notifications,
ProjectName: sourceConfig.ProjectName,
hash: hash,
}
s.Unlock()
logging.Infof("[%s] source has been initialized!", name)
//telemetry
var streams int
var sourceType, connectorOrigin, connectorVersion string
for _, driver := range driverPerCollection {
streams += driver.GetDriversInfo().Streams
sourceType = driver.GetDriversInfo().SourceType
connectorOrigin = driver.GetDriversInfo().ConnectorOrigin
connectorVersion = driver.GetDriversInfo().ConnectorVersion
}
telemetry.Source(name, sourceType, connectorOrigin, connectorVersion, sourceConfig.Schedule, streams)
}
}
func (s *Service) IsConfigured() bool {
return s.configured
}
func (s *Service) GetSource(sourceID string) (*Unit, error) {
s.RLock()
defer s.RUnlock()
unit, ok := s.sources[sourceID]
if !ok {
return nil, fmt.Errorf("Source [%s] doesn't exist", sourceID)
}
return unit, nil
}
func (s *Service) GetCollections(sourceID string) ([]string, error) {
s.RLock()
defer s.RUnlock()
unit, ok := s.sources[sourceID]
if !ok {
return nil, fmt.Errorf("Source [%s] doesn't exist", sourceID)
}
collections := []string{}
for collection := range unit.DriverPerCollection {
collections = append(collections, collection)
}
return collections, nil
}
//remove closes and removes source instance from Service
//method must be called with locks
func (s *Service) remove(sourceID string, unit *Unit) {
for collection, driver := range unit.DriverPerCollection {
err := s.cronScheduler.Remove(sourceID, collection)
if err != nil {
logging.Errorf("[%s] Error stopping scheduling collection [%s] sync: %v", sourceID, collection, err)
}
err = driver.Delete()
if err != nil {
logging.Errorf("[%s] Failed to cleanup driver for collection [%s]: %v", sourceID, collection, err)
}
}
if err := unit.Close(); err != nil {
logging.Errorf("[%s] Error closing source unit: %v", sourceID, err)
}
delete(s.sources, sourceID)
logging.Infof("[%s] source has been removed!", sourceID)
}
func (s *Service) Close() (multiErr error) {
s.RLock()
for _, unit := range s.sources {
err := unit.Close()
if err != nil {
multiErr = multierror.Append(multiErr, err)
}
}
s.RUnlock()
return
}