-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.go
61 lines (50 loc) · 1.68 KB
/
manager.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
package main
import (
"context"
"fmt"
"gualogger/handlers"
"gualogger/logging"
)
type ExportManager struct {
exporters map[string]handlers.Exporter
}
// Initializes a new manager instance
func NewManager(e *Exporters, emap *map[string]interface{}) *ExportManager {
m := new(ExportManager)
m.exporters = make(map[string]handlers.Exporter, 0)
m.RegisterExporters(e, emap)
return m
}
// Adds exporters to the manager based on entries in the exporter config structure
func (m *ExportManager) RegisterExporters(e *Exporters, emap *map[string]interface{}) {
reg := e.GetExporterRegister()
for k := range *emap {
h, exists := reg[k]
if exists {
logging.Logger.Info(fmt.Sprintf("registered exporter: %s", k), "func", "RegisterExporters")
m.exporters[k] = h
}
}
}
// Setup exporter by calling the Initialize() function of each exporters interface
// If the initialization of one exporter fails, the first error gets returned
func (m *ExportManager) SetupPubHandlers(ctx context.Context) error {
callback := func(c context.Context) []handlers.Payload {
return Read(c)
}
for n, e := range m.exporters {
if err := e.Initialize(ctx, callback); err != nil {
return fmt.Errorf("error while initializing exporter %s - %s", n, err.Error())
}
logging.Logger.Info(fmt.Sprintf("successfully initialized exporter: %s", n), "func", "SetupPubHandlers")
}
return nil
}
func (m *ExportManager) Publish(ctx context.Context, p handlers.Payload) {
for n, e := range m.exporters {
p.Server = conf.Opcua.Connection.Endpoint
if err := e.Publish(ctx, p); err != nil {
logging.Logger.Error(fmt.Sprintf("failed to publish payload for exporter %s: %s", n, err.Error()), "func", "Publish")
}
}
}