-
Notifications
You must be signed in to change notification settings - Fork 0
/
fx.go
286 lines (231 loc) · 4.79 KB
/
fx.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package tail
import (
"bufio"
cond "github.com/vela-ssoc/vela-cond"
"github.com/vela-ssoc/vela-kit/audit"
"github.com/vela-ssoc/vela-kit/auxlib"
"github.com/vela-ssoc/vela-kit/lua"
"github.com/vela-ssoc/vela-kit/pipe"
"gopkg.in/tomb.v2"
"io"
"os"
"path/filepath"
"time"
)
const (
WTPoll modeT = iota + 1
WTInotify
)
type modeT uint
type Watcher struct {
wait time.Duration
watcher bool
mode modeT
after time.Duration
interval time.Duration
timeout time.Duration
onEOF *pipe.Chains
}
type Fx struct {
path string
bkt []string
delim byte
err error
fd *os.File
rd *bufio.Reader
buffer int
seek int64
stat os.FileInfo
stime time.Time
enc func([]byte) []byte
add func([]byte) []byte
handle func(line, error)
watcher *Watcher
co *lua.LState
pipe *pipe.Chains
cnd *cond.Cond
tom *tomb.Tomb
tag map[string]lua.LValue
}
func (fx *Fx) openFile() error {
fd, err := openFile(fx.path)
if err != nil {
fx.err = err
return err
}
fx.fd = fd
fx.rd = bufio.NewReaderSize(fd, fx.buffer)
fx.offset()
fx.err = nil
fx.stime = time.Now()
xEnv.Spawn(0, fx.readline)
audit.Debug("%s fx open succeed record [%d]", fx.path, fx.seek).From(fx.co.CodeVM()).Put()
return nil
}
func (fx *Fx) open() error {
//是否开启等待
if fx.watcher.wait < 0 {
return fx.openFile()
}
er := fx.openFile()
if er == nil {
return nil
}
tk := time.NewTicker(fx.watcher.wait)
defer tk.Stop()
for {
select {
case <-tk.C:
er = fx.openFile()
if er == nil {
return nil
}
audit.Errorf("%s open file fail %v", fx.path, er).From(fx.co.CodeVM()).Put()
case <-fx.tom.Dying():
audit.Errorf("%s wait exit", fx.path).From(fx.co.CodeVM()).Put()
return nil
}
}
return nil
}
func (fx *Fx) save() {
seek, e := fx.fd.Seek(0, io.SeekCurrent)
if e != nil {
xEnv.Infof("%s current seek error %v", fx.path, e)
return
}
if fx.fd == nil {
xEnv.Errorf("current %s file is nil", fx.path)
return
}
bkt := xEnv.Bucket(fx.bkt...)
if bkt == nil {
xEnv.Errorf("%s fx current bucket empty", fx.path)
return
}
err := bkt.Store(fx.path, seek, 0)
if err != nil {
xEnv.Errorf("save %s seek record error %v", fx.path, err)
return
}
xEnv.Infof("%s save seek record [%d]", fx.path, seek)
}
func (fx *Fx) offset() {
if fx.fd == nil {
xEnv.Infof("tail %s fd not found", fx.path)
return
}
bkt := xEnv.Bucket(fx.bkt...)
if bkt == nil {
xEnv.Errorf("%s fx current bucket empty", fx.path)
return
}
seek := bkt.Int64(fx.path)
stat, _ := fx.fd.Stat()
size := stat.Size()
if seek > size {
xEnv.Infof("tail offset record [%d] > [%d]", seek, size)
fx.seek = 0
} else {
fx.seek = seek
}
fx.fd.Seek(fx.seek, 0)
xEnv.Infof("%s tail position of %d", fx.path, fx.seek)
return
}
func (fx *Fx) onEOF(raw []byte) {
if fx.watcher.onEOF.Len() == 0 {
fx.doWatcher(newTx(fx, raw))
return
}
fx.watcher.onEOF.Do(newTx(fx, raw), fx.co, func(err error) {
audit.Errorf("tail %s fx on eof pipe call fail %v", fx.path, err).From(fx.co.CodeVM()).High().Put()
})
}
func (fx *Fx) onRead(raw []byte) {
if len(raw) == 0 {
return
}
fx.pipe.Do(raw, fx.co, func(err error) {
audit.Errorf("tail %s fx on read pipe call fail %v", fx.path, err).From(fx.co.CodeVM()).High().Put()
})
}
func (fx *Fx) filter(raw []byte) bool {
if fx.cnd == nil {
return true
}
return fx.cnd.Match(auxlib.B2S(raw))
}
func (fx *Fx) Handle(raw []byte) {
rn := len(raw)
if rn <= 1 {
return
}
if raw[rn-1] == fx.delim {
raw = raw[:rn-1]
}
if !fx.filter(raw) {
return
}
fx.onRead(raw)
fx.handle(line{raw, fx.enc, fx.add}, nil)
}
func (fx *Fx) close() {
_ = fx.fd.Close()
fx.cnd = nil
}
func (fx *Fx) exit() {
fx.watcher.onEOF = nil
fx.cnd = nil
xEnv.Errorf("%s tx exit when eof", fx.path)
}
func (fx *Fx) readline() {
defer fx.close()
for {
select {
case <-fx.tom.Dying():
fx.save()
audit.Errorf("tail %s readline exit", fx.path).From(fx.co.CodeVM()).High().Put()
return
default:
raw, err := fx.rd.ReadBytes(fx.delim)
if err == nil {
fx.Handle(raw)
continue
}
if err.Error() == "EOF" {
fx.Handle(raw)
fx.save()
fx.onEOF(raw)
xEnv.Infof("%s file eof", fx.path)
//轮询监控
return
}
audit.Errorf("tail %s fx read line raw error %v", fx.path, err).
From(fx.co.CodeVM()).High().Put()
//todo
}
}
}
func newFx(tom *tomb.Tomb, value string, handle func(line, error)) *Fx {
path, e := filepath.Abs(filepath.Clean(value))
if e != nil {
path = filepath.Clean(value)
}
fx := &Fx{
delim: '\n',
buffer: 4096,
tom: tom,
path: path,
handle: handle,
pipe: pipe.New(),
}
fx.watcher = &Watcher{
wait: 10 * time.Second,
mode: WTPoll,
interval: 5 * time.Second,
timeout: 20 * 365 * 24 * time.Hour,
onEOF: pipe.New(),
}
return fx
}