-
Notifications
You must be signed in to change notification settings - Fork 96
/
config_parse.go
160 lines (142 loc) · 4.26 KB
/
config_parse.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
package confgo
import (
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/douyu/juno/internal/pkg/invoker"
"github.com/douyu/juno/internal/pkg/service/confgov2"
"github.com/douyu/juno/internal/pkg/service/resource"
"github.com/douyu/juno/internal/pkg/service/system"
"github.com/douyu/juno/pkg/model/db"
"github.com/douyu/juno/pkg/model/view"
"github.com/douyu/jupiter/pkg/xlog"
"go.uber.org/zap"
)
type configTime struct {
Interval string `json:"interval"`
}
// GetConfigParseWorkerTime 获取配置依赖解析间隔时间
func (c *confu) GetConfigParseWorkerTime() (interval int) {
interval = 0 // one day default
settings, err := system.System.Setting.GetAll()
if err != nil {
xlog.Error("GetConfigParseWorkerTime", xlog.String("step", "system.System.Setting.GetAll()"), xlog.Any("error", err))
return interval
}
configDep, ok := settings["config_dep"]
if !ok {
xlog.Error("GetConfigParseWorkerTime", xlog.String("step", "settings"), xlog.Any("error", err))
return interval
}
timeStruct := configTime{}
if err := json.Unmarshal([]byte(configDep), &timeStruct); err != nil {
xlog.Error("GetConfigParseWorkerTime", xlog.String("step", "Unmarshal"), xlog.Any("error", err))
return interval
}
if timeStruct.Interval == "" {
xlog.Error("GetConfigParseWorkerTime", xlog.String("step", "timeStruct"), xlog.Any("error", err))
return interval
}
if timeInt, err := strconv.ParseInt(timeStruct.Interval, 10, 32); err != nil {
xlog.Error("GetConfigParseWorkerTime", xlog.String("step", "ParseInt"), xlog.Any("error", err))
return interval
} else {
interval = int(timeInt)
}
return
}
// ConfigParseWorker ...
func (c *confu) ConfigParseWorker() (err error) {
// 记录更新时间,清理数据库时以该时间为基准
updateTime := time.Now().Unix()
xlog.Info("ConfigParseWorker", zap.String("run", "start"), zap.Any("updateTime", updateTime))
// 拿到所有配置文件
list, err := confgov2.GetAllConfigText()
if err != nil {
xlog.Error("GetAllConfigText", zap.Error(err))
return err
}
for _, config := range list {
// 拿到app信息
appInfo, err := resource.Resource.GetApp(config.AID)
if err != nil {
xlog.Error("GetApp", zap.Error(err), zap.Int("aid", int(config.AID)))
continue
}
// 配置文件为空
if config.Content == "" {
xlog.Error("GetApp", zap.String("msg", "content is nil"), zap.Int("id", int(config.ID)), zap.Int("aid", int(config.AID)))
continue
}
var parseConfig CmcParse
parseConfig = InitCmcToml(invoker.JunoMysql, view.RespOneConfig{
Env: config.Env,
ZoneCode: config.Zone,
Content: config.Content,
AppName: appInfo.AppName,
Aid: int(config.AID),
Format: config.Format,
FileName: fmt.Sprintf("%s.%s", config.Name, config.Format),
})
// 配置解析
resp, err := parseConfig.ParseConfig()
if err != nil {
xlog.Error("parseConfig.ParseConfig", zap.Error(err))
continue
}
// 写入mysql
err = c.ParseToMysql(appInfo.Aid, appInfo.AppName, resp, updateTime)
if err != nil {
xlog.Error("ParseToMysql", zap.Error(err))
continue
}
}
xlog.Info("ConfigParseWorker", zap.String("run", "end"), zap.Any("updateTime", updateTime))
return
}
func (c *confu) ParseToMysql(aid int, appName string, items []*CmcInfo, updateTime int64) (err error) {
// 删除该服务的全部配置
tx := c.DB.Begin()
err = tx.Model(db.AppTopology{}).Where("aid = ? AND update_time < ?", aid, updateTime).Delete(&db.AppTopology{}).Error
if err != nil {
tx.Rollback()
return
}
for _, value := range items {
zoneInfo, _ := resource.Resource.GetZoneInfo(db.Zone{
ZoneCode: value.ZoneCode,
Env: value.Env,
})
// 为了兼容grpc的逻辑
var addr string
if value.Port != "" {
addr = value.Ip + ":" + value.Port
} else {
addr = value.Ip
}
info := &db.AppTopology{
Aid: aid,
RegionCode: zoneInfo.RegionCode,
ZoneCode: value.ZoneCode,
Env: value.Env,
Addr: addr,
IP: value.Ip,
Port: value.Port,
Name: value.Key,
Type: value.Type,
Info: "",
UpdateTime: updateTime,
UpdatedBy: 0,
Extra: "",
AppName: appName,
FileName: value.FileName,
}
if err := tx.Create(info).Error; err != nil {
tx.Rollback()
return err
}
}
tx.Commit()
return
}