/
starter.go
66 lines (55 loc) · 1.63 KB
/
starter.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
package XEtcd
import (
"context"
"fmt"
"github.com/bb-orz/goinfras"
)
type starter struct {
goinfras.BaseStarter
cfg *Config
}
func NewStarter() *starter {
starter := new(starter)
starter.cfg = &Config{}
return starter
}
func (s *starter) Name() string {
return "XEtcd"
}
func (s *starter) Init(sctx *goinfras.StarterContext) {
var err error
var define Config
viper := sctx.Configs()
if viper != nil {
err = viper.UnmarshalKey("Etcd", &define)
sctx.PassWarning(s.Name(), goinfras.StepInit, err)
}
s.cfg = &define
sctx.Logger().Debug(s.Name(), goinfras.StepInit, fmt.Sprintf("Config: %+v ", define))
}
func (s *starter) Setup(sctx *goinfras.StarterContext) {
var err error
client, err = NewEtcdClient(context.TODO(), s.cfg, nil)
if sctx.PassError(s.Name(), goinfras.StepSetup, err) {
sctx.Logger().Info(s.Name(), goinfras.StepSetup, "Etcd V3 Client Setuped! ")
}
}
func (s *starter) Check(sctx *goinfras.StarterContext) bool {
var err error
err = goinfras.Check(client)
if sctx.PassError(s.Name(), goinfras.StepCheck, err) {
sctx.Logger().OK(s.Name(), goinfras.StepCheck, "Etcd V3 Client Setup Successful! ")
}
status, err := client.Status(context.TODO(), s.cfg.Endpoints[0])
if sctx.PassError(s.Name(), goinfras.StepCheck, err) {
sctx.Logger().OK(s.Name(), goinfras.StepCheck, "Etcd V3 Client Setup Successful! ")
sctx.Logger().OK(s.Name(), goinfras.StepCheck, fmt.Sprintf("Etcd V3 Client Status: %+v ", *status))
return true
}
return false
}
func (s *starter) Stop() error {
return client.Close()
}
// 设置启动组级别
func (s *starter) PriorityGroup() goinfras.PriorityGroup { return goinfras.BasicGroup }