-
Notifications
You must be signed in to change notification settings - Fork 54
/
bootstrap.go
196 lines (170 loc) · 6.21 KB
/
bootstrap.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
/*******************************************************************************
* Copyright 2019 Dell Inc.
* Copyright 2020 Intel Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*******************************************************************************/
package bootstrap
import (
"context"
"os"
"os/signal"
"sync"
"syscall"
"github.com/edgexfoundry/go-mod-bootstrap/bootstrap/config"
"github.com/edgexfoundry/go-mod-bootstrap/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/bootstrap/environment"
"github.com/edgexfoundry/go-mod-bootstrap/bootstrap/flags"
"github.com/edgexfoundry/go-mod-bootstrap/bootstrap/interfaces"
"github.com/edgexfoundry/go-mod-bootstrap/bootstrap/registration"
"github.com/edgexfoundry/go-mod-bootstrap/bootstrap/startup"
"github.com/edgexfoundry/go-mod-bootstrap/di"
"github.com/edgexfoundry/go-mod-registry/registry"
"github.com/edgexfoundry/go-mod-core-contracts/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/models"
)
// Deferred defines the signature of a function returned by RunAndReturnWaitGroup that should be executed via defer.
type Deferred func()
// fatalError logs an error and exits the application. It's intended to be used only within the bootstrap prior to
// any go routines being spawned.
func fatalError(err error, lc logger.LoggingClient) {
lc.Error(err.Error())
os.Exit(1)
}
// translateInterruptToCancel spawns a go routine to translate the receipt of a SIGTERM signal to a call to cancel
// the context used by the bootstrap implementation.
func translateInterruptToCancel(ctx context.Context, wg *sync.WaitGroup, cancel context.CancelFunc) {
wg.Add(1)
go func() {
defer wg.Done()
signalStream := make(chan os.Signal)
defer func() {
signal.Stop(signalStream)
close(signalStream)
}()
signal.Notify(signalStream, os.Interrupt, syscall.SIGTERM)
select {
case <-signalStream:
cancel()
return
case <-ctx.Done():
return
}
}()
}
// RunAndReturnWaitGroup bootstraps an application. It loads configuration and calls the provided list of handlers.
// Any long-running process should be spawned as a go routine in a handler. Handlers are expected to return
// immediately. Once all of the handlers are called this function will return a sync.WaitGroup reference to the caller.
// It is intended that the caller take whatever additional action makes sense before calling Wait() on the returned
// reference to wait for the application to be signaled to stop (and the corresponding goroutines spawned in the
// various handlers to be stopped cleanly).
func RunAndReturnWaitGroup(
ctx context.Context,
cancel context.CancelFunc,
commonFlags flags.Common,
serviceKey string,
configStem string,
serviceConfig interfaces.Configuration,
configUpdated config.UpdatedStream,
startupTimer startup.Timer,
dic *di.Container,
handlers []interfaces.BootstrapHandler) (*sync.WaitGroup, Deferred, bool) {
var err error
var wg sync.WaitGroup
deferred := func() {}
// Check if service provided an initial Logging Client to use. If not create one.
lc := container.LoggingClientFrom(dic.Get)
if lc == nil {
lc = logger.NewClient(serviceKey, models.InfoLog)
}
translateInterruptToCancel(ctx, &wg, cancel)
envVars := environment.NewVariables(lc)
configProcessor := config.NewProcessor(lc, commonFlags, envVars, startupTimer, ctx, &wg, configUpdated, dic)
if err := configProcessor.Process(serviceKey, configStem, serviceConfig); err != nil {
fatalError(err, lc)
}
// Now the the configuration has been processed the logger has been created based on configuration.
lc = configProcessor.Logger
var registryClient registry.Client
envUseRegistry, wasOverridden := envVars.UseRegistry()
if envUseRegistry || (commonFlags.UseRegistry() && !wasOverridden) {
registryClient, err = registration.RegisterWithRegistry(
ctx,
startupTimer,
serviceConfig,
lc,
serviceKey)
if err != nil {
fatalError(err, lc)
}
deferred = func() {
lc.Info("Un-Registering service from the Registry")
err := registryClient.Unregister()
if err != nil {
lc.Error("Unable to Un-Register service from the Registry", "error", err.Error())
}
}
}
dic.Update(di.ServiceConstructorMap{
container.ConfigurationInterfaceName: func(get di.Get) interface{} {
return serviceConfig
},
container.LoggingClientInterfaceName: func(get di.Get) interface{} {
return lc
},
container.RegistryClientInterfaceName: func(get di.Get) interface{} {
return registryClient
},
container.CancelFuncName: func(get di.Get) interface{} {
return cancel
},
})
// call individual bootstrap handlers.
startedSuccessfully := true
for i := range handlers {
if handlers[i](ctx, &wg, startupTimer, dic) == false {
cancel()
startedSuccessfully = false
break
}
}
return &wg, deferred, startedSuccessfully
}
// Run bootstraps an application. It loads configuration and calls the provided list of handlers. Any long-running
// process should be spawned as a go routine in a handler. Handlers are expected to return immediately. Once all of
// the handlers are called this function will wait for any go routines spawned inside the handlers to exit before
// returning to the caller. It is intended that the caller stop executing on the return of this function.
func Run(
ctx context.Context,
cancel context.CancelFunc,
commonFlags flags.Common,
serviceKey string,
configStem string,
serviceConfig interfaces.Configuration,
startupTimer startup.Timer,
dic *di.Container,
handlers []interfaces.BootstrapHandler) {
wg, deferred, _ := RunAndReturnWaitGroup(
ctx,
cancel,
commonFlags,
serviceKey,
configStem,
serviceConfig,
nil,
startupTimer,
dic,
handlers,
)
defer deferred()
// wait for go routines to stop executing.
wg.Wait()
}