-
Notifications
You must be signed in to change notification settings - Fork 477
/
init.go
136 lines (115 loc) · 3.47 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
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
//
// Copyright (c) 2018 Tencent
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package logging
import (
"context"
"fmt"
"sync"
"time"
"github.com/edgexfoundry/edgex-go/internal/support/logging/config"
"github.com/edgexfoundry/edgex-go/internal/support/logging/container"
"github.com/edgexfoundry/edgex-go/internal/support/logging/interfaces"
"github.com/edgexfoundry/edgex-go/internal/support/logging/logger/file"
"github.com/edgexfoundry/edgex-go/internal/support/logging/logger/mongo"
bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/bootstrap/logging"
"github.com/edgexfoundry/go-mod-bootstrap/bootstrap/startup"
bootstrapConfig "github.com/edgexfoundry/go-mod-bootstrap/config"
"github.com/edgexfoundry/go-mod-bootstrap/di"
"github.com/gorilla/mux"
)
const (
PersistenceDB = "database"
PersistenceFile = "file"
)
type server interface {
IsRunning() bool
}
type ServiceInit struct {
router *mux.Router
server server
serviceKey string
}
func NewServiceInit(router *mux.Router, server server, serviceKey string) ServiceInit {
return ServiceInit{
router: router,
server: server,
serviceKey: serviceKey,
}
}
func getPersistence(
credentials *bootstrapConfig.Credentials,
configuration *config.ConfigurationStruct) (interfaces.Persistence, error) {
switch configuration.Writable.Persistence {
case PersistenceFile:
return file.NewLogger(configuration.Logging.File), nil
case PersistenceDB:
return mongo.NewLogger(credentials, configuration)
default:
return nil, fmt.Errorf("unrecognized value Configuration.Persistence: %s", configuration.Writable.Persistence)
}
}
func (s ServiceInit) BootstrapHandler(
ctx context.Context,
wg *sync.WaitGroup,
startupTimer startup.Timer,
dic *di.Container) bool {
loadRestRoutes(s.router, dic)
lc := logging.FactoryToStdout(s.serviceKey)
configuration := container.ConfigurationFrom(dic.Get)
// get database credentials.
credentialsProvider := bootstrapContainer.CredentialsProviderFrom(dic.Get)
var credentials bootstrapConfig.Credentials
for startupTimer.HasNotElapsed() {
var err error
credentials, err = credentialsProvider.GetDatabaseCredentials(configuration.Databases["Primary"])
if err == nil {
break
}
lc.Warn(fmt.Sprintf("couldn't retrieve database credentials: %v", err.Error()))
startupTimer.SleepForInterval()
}
// initialize database.
var persistenceClient interfaces.Persistence
var err error
for startupTimer.HasNotElapsed() {
persistenceClient, err = getPersistence(&credentials, configuration)
if err == nil {
break
}
lc.Warn(fmt.Sprintf("couldn't create database client: %v", err.Error()))
startupTimer.SleepForInterval()
}
if err != nil {
lc.Error(fmt.Sprintf("unable to create database client in allotted time"))
return false
}
dic.Update(di.ServiceConstructorMap{
bootstrapContainer.LoggingClientInterfaceName: func(get di.Get) interface{} {
return lc
},
container.PersistenceInterfaceName: func(get di.Get) interface{} {
return persistenceClient
},
})
lc.Info("Database connected")
wg.Add(1)
go func() {
defer wg.Done()
<-ctx.Done()
for {
// wait for httpServer to stop running (e.g. handling requests) before closing the database connection.
if s.server.IsRunning() == false {
lc.Info("Database disconnecting")
persistenceClient.CloseSession()
break
}
time.Sleep(time.Second)
}
}()
return true
}