-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
209 lines (183 loc) · 4.79 KB
/
main.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
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"context"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/oklog/run"
"github.com/prometheus/common/promlog"
promlogflag "github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
"stree-index/pkg"
"stree-index/pkg/config"
"stree-index/pkg/mem-index"
"stree-index/pkg/statistics"
"stree-index/pkg/web"
)
func main() {
var (
app = kingpin.New(filepath.Base(os.Args[0]), "The stree-index")
//configFile = kingpin.Flag("config.file", "docker-mon configuration file path.").Default("docker-mon.yml").String()
configFile = app.Flag("config.file", "docker-mon configuration file path.").Default("stree-index.yml").String()
)
promlogConfig := promlog.Config{}
app.Version(version.Print("stree-index"))
app.HelpFlag.Short('h')
promlogflag.AddFlags(app, &promlogConfig)
kingpin.MustParse(app.Parse(os.Args[1:]))
var logger log.Logger
logger = func(config *promlog.Config) log.Logger {
var (
l log.Logger
le level.Option
)
if config.Format.String() == "logfmt" {
l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
} else {
l = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
}
switch config.Level.String() {
case "debug":
le = level.AllowDebug()
case "info":
le = level.AllowInfo()
case "warn":
le = level.AllowWarn()
case "error":
le = level.AllowError()
}
l = level.NewFilter(l, le)
l = log.With(l, "ts", log.TimestampFormat(
func() time.Time { return time.Now().Local() },
"2006-01-02T15:04:05.000Z07:00",
), "caller", log.DefaultCaller)
return l
}(&promlogConfig)
// new ctxall
ctxAll, cancelAll := context.WithCancel(context.Background())
// init config
sc, err := config.LoadFile(*configFile, logger)
if err != nil {
level.Error(logger).Log("msg", "Load Config File Failed ....")
return
}
// 为gin handlefunc 用到的变量设置一下
config.SetDefaultVar(sc)
// Init db && redis
pkg.InitStore(sc)
level.Info(logger).Log("msg", "Init Db and Redis Success....")
pkg.NewMetrics()
// index flush chan
// 在索引刷新完成前不要启动web
//indexUpdateChan := make(chan struct{}, 1)
mem_index.InitIdx()
err = mem_index.FlushAllIdx(logger)
if err != nil {
level.Error(logger).Log("msg", "Flush Index Failed And Exiting....")
return
}
level.Info(logger).Log("msg", "Flush All Index Successfully ")
var g run.Group
{
// Termination handler.
term := make(chan os.Signal, 1)
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
cancel := make(chan struct{})
g.Add(
func() error {
select {
case <-term:
level.Warn(logger).Log("msg", "Received SIGTERM, exiting gracefully...")
cancelAll()
return nil
//TODO clean work here
case <-cancel:
level.Warn(logger).Log("msg", "server finally exit...")
return nil
}
},
func(err error) {
close(cancel)
},
)
}
{
// metrics web handler.
g.Add(func() error {
//<-indexFlushDoneChan
level.Info(logger).Log("msg", "start web service Listening on address", "address", sc.Http.ListenAddr)
//gin.SetMode(gin.ReleaseMode)
//routes := gin.Default()
//routes := gin.New()
errchan := make(chan error)
go func() {
errchan <- web.StartGin(sc.Http.ListenAddr)
}()
select {
case err := <-errchan:
level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
return err
case <-ctxAll.Done():
level.Info(logger).Log("msg", "Web service Exit..")
return nil
}
}, func(err error) {
cancelAll()
})
}
{
// RegisterHeartBeatToCache manager.
g.Add(func() error {
err := pkg.RegisterHeartBeatToCache(ctxAll, sc.RedisServer.Keys, logger)
if err != nil {
level.Error(logger).Log("msg", "RegisterHeartBeatToCache error", "error", err)
}
return err
}, func(err error) {
cancelAll()
})
}
{
// IndexUpdate manager.
g.Add(func() error {
err := mem_index.UpdateIndex(ctxAll, logger, sc.RedisServer.Keys)
if err != nil {
level.Error(logger).Log("msg", "IndexUpdateManager error", "error", err)
}
return err
}, func(err error) {
cancelAll()
})
}
{
// Flush ALl IndexUpdate manager.
// cron flush all index in case of data Inconsistent
g.Add(func() error {
err := mem_index.FlushAllIndex(ctxAll, logger)
if err != nil {
level.Error(logger).Log("msg", "FluashALlIndexUpdateManager error", "error", err)
}
return err
}, func(err error) {
cancelAll()
})
}
{
// TreeNodeStatistics manager.
g.Add(func() error {
err := statistics.TreeNodeStatistics(ctxAll, logger)
if err != nil {
level.Error(logger).Log("msg", "TreeNodeStatistics error", "error", err)
}
return err
}, func(err error) {
cancelAll()
})
}
g.Run()
}