-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance.go
72 lines (60 loc) · 1.87 KB
/
instance.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
package coordinator
import (
"context"
"fmt"
"github.com/myrteametrics/myrtea-sdk/v4/elasticsearch"
"github.com/myrteametrics/myrtea-sdk/v4/modeler"
"go.uber.org/zap"
)
// Instance represents a functional Myrtea instance
type Instance struct {
Initialized bool
Name string
Urls []string
Executor *elasticsearch.EsExecutor
LogicalIndices map[string]*LogicalIndex
}
func (instance *Instance) initialize(models map[int64]modeler.Model) error {
err := instance.InitLogicalIndices(models)
if err != nil {
zap.L().Error("instance.InitLogicalIndices()", zap.Error(err))
return err
}
instance.Initialized = true
return nil
}
func (instance *Instance) initElasticClient(urls []string) error {
// TODO: Multiple elasticsearch URL Support
executor, err := elasticsearch.NewEsExecutor(context.Background(), urls)
if err != nil {
zap.L().Error("elasticsearch.NewEsExecutor()", zap.Error(err))
return err
}
instance.Executor = executor
return nil
}
// InitLogicalIndices initialize an ensemble of logical indices (each based on a specific elasticsearch model)
func (instance *Instance) InitLogicalIndices(models map[int64]modeler.Model) error {
for _, m := range models {
zap.L().Info("Initialize model indices", zap.String("model", m.Name), zap.Any("options", m.ElasticsearchOptions))
executor, err := elasticsearch.NewEsExecutor(context.Background(), instance.Urls)
if err != nil {
zap.L().Error("elasticsearch.NewEsExecutor()", zap.Error(err))
return err
}
logicalIndex := &LogicalIndex{
Initialized: false,
Name: fmt.Sprintf("%s-%s", instance.Name, m.Name),
Cron: nil,
Executor: executor,
Model: m,
}
err = logicalIndex.initialize()
if err != nil {
zap.L().Error("logicalIndex.initialize()", zap.Error(err))
return err
}
instance.LogicalIndices[m.Name] = logicalIndex
}
return nil
}