/
init.go
104 lines (88 loc) · 2.31 KB
/
init.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
//
// Copyright (c) 2018 Tencent
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package logging
import (
"errors"
"fmt"
"sync"
"time"
"github.com/Circutor/edgex/pkg/clients/logger"
"github.com/Circutor/edgex/internal/pkg/config"
"github.com/Circutor/edgex/internal/pkg/telemetry"
)
var Configuration *ConfigurationStruct
var dbClient persistence
var LoggingClient logger.LoggingClient
func Retry(useProfile string, timeout int, wait *sync.WaitGroup, ch chan error) {
LoggingClient = newPrivateLogger()
until := time.Now().Add(time.Millisecond * time.Duration(timeout))
for time.Now().Before(until) {
var err error
// When looping, only handle configuration if it hasn't already been set.
if Configuration == nil {
Configuration, err = initializeConfiguration(useProfile)
if err != nil {
ch <- err
// Error occurred when attempting to read from local filesystem. Fail fast.
close(ch)
wait.Done()
return
}
}
// Only attempt to connect to database if configuration has been populated
if Configuration != nil {
err = getPersistence()
if err != nil {
ch <- err
} else {
break
}
}
time.Sleep(time.Second * time.Duration(1))
}
close(ch)
wait.Done()
return
}
func Init() bool {
if Configuration == nil || dbClient == nil {
return false
}
go telemetry.StartCpuUsageAverage()
return true
}
func Destruct() {
if dbClient != nil {
dbClient.closeSession()
dbClient = nil
}
}
func initializeConfiguration(useProfile string) (*ConfigurationStruct, error) {
configuration := &ConfigurationStruct{}
err := config.LoadFromFile(useProfile, configuration)
if err != nil {
return nil, err
}
return configuration, nil
}
func getPersistence() error {
switch Configuration.Writable.Persistence {
case PersistenceFile:
dbClient = &fileLog{filename: Configuration.Logging.File, maxBytes: Configuration.Logging.MaxBytes, logsCount: Configuration.Logging.LogFiles}
case PersistenceDB:
// TODO: Integrate db layer with internal/pkg/db/ types so we can support other databases
ms, err := connectToMongo()
if err != nil {
return err
} else {
dbClient = &mongoLog{session: ms}
}
default:
return errors.New(fmt.Sprintf("unrecognized value Configuration.Persistence: %s", Configuration.Writable.Persistence))
}
return nil
}