-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.go
More file actions
162 lines (139 loc) · 3.29 KB
/
init.go
File metadata and controls
162 lines (139 loc) · 3.29 KB
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
package config
import (
"context"
"sync"
"time"
"github.com/k0kubun/pp"
"github.com/rai-project/utils"
"github.com/sirupsen/logrus"
)
var (
log *logrus.Entry
once sync.Once
beforeInitFunctions struct {
funcs []func() `json:"funcs"`
mutex sync.Mutex `json:"mutex"`
}
onInitFunctions struct {
funcs []func() `json:"funcs"`
mutex sync.Mutex `json:"mutex"`
}
afterInitFunctions struct {
funcs []func() `json:"funcs"`
mutex sync.Mutex `json:"mutex"`
}
)
// BeforeInit ...
func BeforeInit(f func()) {
beforeInitFunctions.mutex.Lock()
defer beforeInitFunctions.mutex.Unlock()
beforeInitFunctions.funcs = append(beforeInitFunctions.funcs, f)
}
// OnInit ...
func OnInit(f func()) {
onInitFunctions.mutex.Lock()
defer onInitFunctions.mutex.Unlock()
onInitFunctions.funcs = append(onInitFunctions.funcs, f)
}
// AfterInit ...
func AfterInit(f func()) {
afterInitFunctions.mutex.Lock()
defer afterInitFunctions.mutex.Unlock()
afterInitFunctions.funcs = append(afterInitFunctions.funcs, f)
}
// Init ...
func Init(opts ...Option) {
once.Do(func() {
if beforeInitFunsLength := len(beforeInitFunctions.funcs); beforeInitFunsLength > 0 {
var wg sync.WaitGroup
wg.Add(beforeInitFunsLength)
for ii := range beforeInitFunctions.funcs {
f := beforeInitFunctions.funcs[ii]
go func() {
defer wg.Done()
f()
}()
}
wg.Wait()
}
modeInfo()
options := NewOptions()
for _, o := range opts {
o(options)
}
if options.AppSecret != "" {
defer func() {
App.Secret = options.AppSecret
}()
}
if options.AppName != "" {
defer func() {
App.Name = options.AppName
}()
}
log = logrus.WithField("pkg", "config")
if options.IsDebug || options.IsVerbose {
pp.WithLineInfo = true
log.Level = logrus.DebugLevel
}
load(options)
if initFunsLength := len(onInitFunctions.funcs); initFunsLength > 0 {
var wg sync.WaitGroup
wg.Add(initFunsLength)
for ii := range onInitFunctions.funcs {
f := onInitFunctions.funcs[ii]
go func() {
defer wg.Done()
f()
}()
}
wg.Wait()
}
if initFunsLength := len(afterInitFunctions.funcs); initFunsLength > 0 {
var wg sync.WaitGroup
for ii := range afterInitFunctions.funcs {
wg.Add(1)
f := afterInitFunctions.funcs[ii]
go func() {
defer wg.Done()
done := make(chan bool, 1)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
go func() {
//tic := time.Now()
f()
//elapsed := time.Since(tic)
//pp.Println(utils.GetFunctionName(f), " took = ", elapsed)
done <- true
}()
select {
case <-done:
return
case <-ctx.Done():
funName := utils.GetFunctionName(f)
panic("time limit reached while evaluating config function " + funName)
}
}()
}
wg.Wait()
}
// if IsVerbose {
// if viper.ConfigFileUsed() != "" {
// fmt.Print("[" + viper.ConfigFileUsed() + "]")
// }
// fmt.Println("Finished setting configuration...")
// }
})
}
func init() {
log = logrus.WithField("pkg", "config")
opts := NewOptions()
if opts.IsVerbose || opts.IsDebug {
log.Level = logrus.DebugLevel
}
initEnv(opts)
isVerbose, isDebug := modeInfo()
if isVerbose || isDebug {
log.Level = logrus.DebugLevel
}
}