forked from cherry-game/cherry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource_file.go
167 lines (137 loc) · 3.47 KB
/
source_file.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
package cherryDataConfig
import (
"os"
"regexp"
"time"
cerr "github.com/cherry-game/cherry/error"
cfile "github.com/cherry-game/cherry/extend/file"
clog "github.com/cherry-game/cherry/logger"
cprofile "github.com/cherry-game/cherry/profile"
"github.com/radovskyb/watcher"
)
type (
// SourceFile 本地读取数据配置文件
SourceFile struct {
fileConfig
watcher *watcher.Watcher
changeFn ConfigChangeFn
monitorPath string
}
fileConfig struct {
FilePath string `json:"file_path"` // 配置文件路径
ExtName string `json:"ext_name"` // 文件扩展名
ReloadTime int64 `json:"reload_time"` // 定时重载扫描(毫秒)
}
)
func (f *SourceFile) Name() string {
return "file"
}
func (f *SourceFile) Init(_ IDataConfig) {
err := f.unmarshalFileConfig()
if err != nil {
clog.Panicf("Unmarshal fileConfig fail. err = %v", err)
return
}
f.watcher = watcher.New()
f.watcher.FilterOps(watcher.Write)
var regexpFilter *regexp.Regexp
regexpFilter, err = regexp.Compile(`.*\` + f.ExtName + `$`)
if err != nil {
clog.Panicf("AddFilterHook extName fail. err = %v", err)
return
}
f.watcher.AddFilterHook(watcher.RegexFilterHook(regexpFilter, false))
f.monitorPath, err = cfile.JoinPath(cprofile.Path(), f.FilePath)
if err != nil {
clog.Panicf("[name = %s] join path fail. err = %v.", f.Name(), err)
return
}
err = f.watcher.Add(f.monitorPath)
if err != nil {
clog.Panicf("New watcher error. path=%s, err = %v", f.monitorPath, err)
return
}
// new watcher
go f.newWatcher()
}
func (f *SourceFile) ReadBytes(configName string) ([]byte, error) {
if configName == "" {
return nil, cerr.Error("Config name is empty.")
}
fullPath, err := cfile.JoinPath(f.monitorPath, configName+f.ExtName)
if err != nil {
return nil, cerr.Errorf("Config file not found. err = %v", err)
}
data, err := os.ReadFile(fullPath)
if err != nil {
return nil, cerr.Errorf("Read file error. [path = %s, err = %v]", fullPath, err)
}
if len(data) < 1 {
return nil, cerr.Errorf("Data is empty. [configName = %s]", configName)
}
return data, err
}
func (f *SourceFile) OnChange(fn ConfigChangeFn) {
f.changeFn = fn
}
func (f *SourceFile) newWatcher() {
go func() {
for {
select {
case ev := <-f.watcher.Event:
{
if ev.IsDir() {
continue
}
configName := cfile.GetFileName(ev.FileInfo.Name(), true)
clog.Infof("Trigger file change. [name = %s]", configName)
data, err := f.ReadBytes(configName)
if err != nil {
clog.Warn("Read data fail. [name = %s, err = %s]", configName, err)
continue
}
if f.changeFn != nil {
f.changeFn(configName, data)
}
}
case err := <-f.watcher.Error:
{
clog.Error(err)
continue
}
case <-f.watcher.Closed:
return
}
}
}()
err := f.watcher.Start(time.Duration(f.ReloadTime) * time.Millisecond)
if err != nil {
clog.Panic(err)
}
}
func (f *SourceFile) Stop() {
if f.watcher == nil {
return
}
err := f.watcher.Remove(f.monitorPath)
clog.Infof("Remote watcher [path = %s, err = %v]", f.monitorPath, err)
f.watcher.Close()
}
func (f *SourceFile) unmarshalFileConfig() error {
//read data_config->file node
dataConfig := cprofile.GetConfig("data_config").GetConfig(f.Name())
err := dataConfig.Unmarshal(&f.fileConfig)
if err != nil {
return err
}
if f.ReloadTime < 1 {
f.ReloadTime = 3000
}
if len(f.ExtName) < 1 {
f.ExtName = ".json"
}
if len(f.FilePath) < 1 {
f.FilePath = "data/"
}
return nil
}